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 |