日期:2014-05-17  浏览次数:20376 次

mysql如何按单双数结果并类?
这是一个分页的代码,每页十条记录,按时间倒序排列。

PHP code

$sql = mysql_query("SELECT * FROM article WHERE uid='132828' ORDER BY date LIMIT ".$number.",10 ");
$num = 1;
while($row = mysql_fetch_array($sql)){
   if($num ==1){
       echo '<ul class="left">';
   }
   if($num ==5){
       echo '<ul class="right">';
   }
   //这里的代码如何写,才能按单双数并类?
   if($num%5==0){
       echo '</ul>';
   }
   $num++;
}



最后我想输出的结果是

HTML code

<ul class="left">
  <li> 1st result </li>
  <li> 3rd result </li>
  <li> 5th result </li>
  <li> 7th result </li>
  <li> 9th result </li>
</ul>
<ul class="right">
  <li> 2nd result </li>
  <li> 4rd result </li>
  <li> 6th result </li>
  <li> 8th result </li>
  <li> 10th result </li>
</ul>



HTML 视图
HTML code

     ul            ul
 ------------ ------------
| 1st result | 2nd result |
 ------------ ------------
| 3rd result | 4th result |
 ------------ ------------
| 5th result | 6th result |
 ------------ ------------
| 7th result | 8th result |
 ------------ ------------
| 9th result | 10th result|
 ------------ ------------




------解决方案--------------------
PHP code
$num = 1;
while($row = mysql_fetch_array($sql)){
   if($num % 2)
   {
     $left[] = '<li>'.$row['title'].'</li>';
   }
   else $right[] = '<li>'.$row['title'].'</li>';
}

echo "<ul class='left'>".implode("",$left).'</ul'>;
echo "<ul class='right'>".implode("",$right).'</ul'>;

------解决方案--------------------
也可通过移动指针完成