duanjuelu8874 2014-02-12 14:06
浏览 59
已采纳

如何显示同名的多个记录?

I have a database tables containing four fields.

id is my primary key and the rest of the fields are: name, address and phone number of the restaurant accordingly. Now, I have many restaurants belonging to different chains, so I saved them using same name and their address and phone no fields look like this:

This is how my database looks like:

id |     name    | address  | Phone_no |

1  | restaurant1 | address1 | XXXXXX1  |
2  | restaurant1 | address2 | XXXXXX2  |
3  | restaurant1 | address3 | XXXXXX3  |
4  | restaurant2 | address1 | XXXXXX1  |
5  | restaurant2 | address2 | XXXXXX2  |
6  | restaurant3 | address1 | XXXXXX1  |
7  | restaurant4 | address1 | XXXXXX1  |

How I want to show it In my page :

 |     name    | address  | Phone_no |

 | restaurant1 | address1 | XXXXXX1  |
 |             | address2 | XXXXXX2  |
 |             | address3 | XXXXXX3  |
 | restaurant2 | address1 | XXXXXX1  |
 |             | address2 | XXXXXX2  |
 | restaurant3 | address1 | XXXXXX1  |
 | restaurant4 | address1 | XXXXXX1  |


Now I want to show it using PHP, with name, their address and phone number, but name should be displayed only once.

this is my PHP code:

    <?php
    //this is my query.
    $query = "SELECT * FROM details WHERE cat_id = $id GROUP BY name";
    $result = mysqli_query($con,$query);

    while($row = mysqli_fetch_array($result)){ 
    ?>
      <table>
         <tr>
           <td><?php echo cfirst($row['name'] ?></td>
           <td><?php echo row['address'] ?></td>
           <td><?php echo row['phone'] ?></td>
         </tr>
       </table>
   <?php } ?>


The result of this query:

 |     name    | address  | Phone_no |

 | restaurant1 | address1 | XXXXXX1  |
 | restaurant2 | address1 | XXXXXX1  |
 | restaurant3 | address1 | XXXXXX1  |
 | restaurant4 | address1 | XXXXXX1  |
  • 写回答

4条回答 默认 最新

  • dsaj20411 2017-10-28 14:26
    关注

    First You have to Remove:

      $query = "SELECT * FROM details WHERE cat_id = $id";
    

    This group by is just removing address of restaurant with multiple address.you have to do it in you code in my view.

        <?php
        $last_name = '';
        while($row = mysqli_fetch_array($result)){ 
        ?>
          <table>
             <tr>
             <?php if ($last_name == ''){?>
               <td><?php echo cfirst($row['name'] ?></td>
              <?php 
                    $last_name = $row['name']; 
                    } elseif($last_name != $row['name']){
              ?>
               <td><?php echo cfirst($row['name'] ?></td>
              <?php }else{ ?>
               <td></td>
              <?php } ?>
               <td><?php echo row['address'] ?></td>
               <td><?php echo row['phone'] ?></td>
             </tr>
           </table>
       <?php } ?>
    

    This will solve your problem just adjust your html accordingly.

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

报告相同问题?

悬赏问题

  • ¥15 xshell无法连接提示ssh服务器拒绝密码
  • ¥15 AT89C52单片机C语言关于串口通信的位操作
  • ¥20 需要步骤截图(标签-服务器|关键词-map)
  • ¥50 gki vendor hook
  • ¥15 灰狼算法和蚁群算法如何结合
  • ¥15 这是一个利用ESP32自带按键和LED控制的录像代码,编译过程出现问题,请解决并且指出错误,指导如何处理 ,协助完成代码并上传代码
  • ¥20 stm32f103,hal库 hal_usart_receive函数接收不到数据。
  • ¥20 求结果和代码,sas利用OPTEX程序和D-efficiency生成正交集
  • ¥50 adb连接不到手机是怎么回事?
  • ¥20 抓取数据时发生错误: get_mooncake_data() missing 1 required positional argument: 'driver'的问题,怎么改出正确的爬虫代码?