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 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)