【问题标题】:php mysql result add html tagsphp mysql 结果添加html标签
【发布时间】:2012-03-07 18:17:28
【问题描述】:

这是一些 php mysql 查询代码。我想用分页进行一些查询。每页有 10 个结果。

$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">';
   }
   //what about these code here? 
   if($num%5==0){
       echo '</ul>';
   }
}

我想把 html 像这样:

<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标签呢?谢谢。

【问题讨论】:

  • 你试过什么?您是否通过任何方式研究过这个主题?我的意思是分页永远存在,有很多教程可以帮助你。
  • @Boo,我正在尝试将单数结果换成&lt;ul class="left"&gt; ,其他结果换成&lt;ul class="right"&gt;

标签: php mysql


【解决方案1】:

你没有增加$num 试试这个

$num = 1;
while($row = mysql_fetch_array($sql)){
   if($num ==1){
       echo '<ul class="left">';
   }
   if($num ==5){
       echo '<ul class="right">';
   }
   //what about these code here? 
   if($num%5==0){
       echo '</ul>';
   }
   $num++ ;

}

【讨论】:

    【解决方案2】:

    试试这个代码:-

    $num = 1;
    $ul_first= '<ul class="left">';
    $ul_second= '<ul class="right">';
    while($row = mysql_fetch_array($sql)){
       if(($num%2)==1){
           $ul_first.= '<li>'.$row['result'].'</li>';
       }
       if(($num%2)==0){
           $ul_second.='<li>'.$row['result'].'</li>';
       }
    
       if(($num%10)==0){
           $ul_first.= '</ul>';
           $ul_second.= '</ul>';
       }
       $num++ ;
    
    }
    echo $ul_first;
    echo $ul_second;
    

    【讨论】:

    • @fish_man 如果您觉得我的回答有帮助并且解决了您的问题,请接受。所以它会帮助别人
    猜你喜欢
    • 2016-03-17
    • 2018-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-29
    相关资源
    最近更新 更多