2014-02-12 53 views
0

我有一个包含四个字段的数据库表。如何显示同名多个记录?

id是我的primary key,其余的字段是:餐厅的名称,地址和电话号码。现在,我有很多的餐馆属于不同的链条,所以我把它们使用相同的name及其addressphone no领域保存这个样子:

这是我的数据库看起来像:

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 | 

我怎么想表明它在我的网页:

|  name | address | Phone_no | 

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


现在我想使用PHP显示它,其名称,地址和电话号码为,但名称应仅显示一次

这是我的PHP代码:

<?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 } ?> 


此查询的结果是:

|  name | address | Phone_no | 

| restaurant1 | address1 | XXXXXX1 | 
| restaurant2 | address1 | XXXXXX1 | 
| restaurant3 | address1 | XXXXXX1 | 
| restaurant4 | address1 | XXXXXX1 | 
+1

你在哪里显示?你在while循环中运行它?显示一些代码 –

+0

如果您使用WHERE name ='SALT'N PEPPER',它将会出现错误,因为您在PEPPER之前结束了您的查询。使用双引号.. WHERE name =“SALT'N PEPPER” –

+0

你的php代码是否回应任何结果?因为我在你的代码中看到你为while循环指定了一个条件,但是没有用花括号括住你的结果? “{}”至于给予餐厅的标题,只是从while循环中排除名称作为内容的标题。 –

回答

2

首先,你必须删除:

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

此组是只删除多个address.you餐厅的地址,必须这样做,你的代码在我看来。

<?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 } ?> 

这将解决您的问题只是相应地调整您的html。

+0

它做了很久,但谢谢。它会解决问题。 – Xitas

0

你仍然需要获得所有的行从SQL,所以我认为这是你可以在PHP的一面修复的东西英格斯。因此,也许你有一个名称列跨越多行的表,这样你有唯一的地址,但只显示一次名称?

$command = "select * from table_name"; 
$result = mysqli_query($link,$command); 
$last = ""; 
$count = 0; 
while($data = mysqli_fetch_assoc($result)){ 
    if($data['name'] == $last){ 
     //print table row 
     ++$count; 
    } else { 
     //echo new <TD> cell that has rowspan="$count" 
     //print table row 
     $count = 0; 
    $last = $data['name']; 
    } 
} 

非常粗糙,没有测试。

1

这是我会做什么:

  1. 查询通过的所有记录,并在每个循环结束时,所有的餐馆和秩序的餐馆名称
  2. 循环表,存储$last_restaurant
  3. 在循环中,如果$last_restaurant的名称与下一个餐厅名称相同,则仅回显地址和电话
  4. 在循环中,如果$last_restaurant名称与下一个餐厅名称不同,则会将所有信息和

Here's一个工作示例,尝试在您的代码中实现它。

1
$result = mysqli_query($con,"SELECT * FROM test where cat_id = $id"); 

while($row = mysqli_fetch_array($result)){ 


$name = $row['name']; 
$address = $row['address']; 
$phone = $row['phone']; 
?> 
<doctype! html> 
<head> 
</head> 
<body> 
Name: <?php echo $name ?> <br> 
Address: <?php echo $address ?><br> 
Phone: <?php echo $phone ?><br><br> 
</body> 
</html> 
<?php 
} 
?> 

输出:

名称:rest1

地址:23玫瑰街

电话:123125325

名称:rest2

地址:23银行街

电话:24343532523

名称:rest1

地址:25绿色街道

电话:53425435432

名称:rest2

地址:54流动站街道

电话: 6434532

相关问题