doucao1066 2014-08-06 07:37
浏览 10
已采纳

php阻止特定的重复行

I have a sample PHP code

  <?php 
   for ($x=0; $x<=3; $x++) {
    echo '<tr><td>Column $x</td>';
    $result = mysql_query(" SELECT id, `Nickname`,`hours` FROM `employee` ");

    echo '<table border=1>';
    while ($row = mysql_fetch_array($result)) {
      echo '<tr>';
      echo "<td>Nickname:".$row{'Nickname'}."</td><td>Hours: ".$row{'hours'}."</td>";
      echo '</tr>';
    }
    echo '</table>';    
    echo '</tr>';
   } 
?>

and the result is something like this.

                Column 1                    Column 2                   Column 3   
Nickname: John  Hours: 1  |  Nickname: John Hours:  1 | Nickname: John Hours: 1
Nickname: Foo   Hours: 2  |  Nickname: Foo  Hours:  2 | Nickname: Foo  Hours: 2 

I need to eliminate specific repeating rows. I dont need to repeat the Nickname Row.

the output should be like this Column 1 Column 2 Column 3 Nickname: John Hours: 1 | Hours: 1 | Hours: 1 Nickname: Foo Hours: 2 | Hours: 2 | Hours: 2

  • 写回答

5条回答 默认 最新

  • douwei7976 2014-08-06 07:41
    关注

    My suggestion is that you solve this easily in PHP.

    <?php 
    for ($x=0; $x<=3; $x++) {
    
    $result = mysql_query(" SELECT id, `Nickname`,`hours` FROM `employee` ORDER BY `Nickname`");
    
    echo '<table border=1>';
    $nick = "";
    while ($row = mysql_fetch_array($result)) {
      if($nick != $row{'Nickname'} {
           if($nick != "") { echo '</tr>'; }
           echo '<tr>';
           echo "<td>Nickname:".$row{'Nickname'}."</td>";
      }
      echo "<td>Hours: ".$row{'hours'}."</td>";
      $nick = $row{'Nickname'};
    }
    echo '</table>'; 
    ?>
    

    In order for this to work as intended, you have to have all nicknames next to eachother, which you easily do with ORDER BY Nickname.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数