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条)

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改