简体   繁体   English

PHP for SQL结果循环

[英]PHP for loop on sql results

I'm currently working on a project for school. 我目前正在为一个学校项目工作。 I have 2 SQL tables and one table to connect the two. 我有2个SQL表和一个连接两个表。 Tabel one contains people, table 2 contains card. 表1包含人员,表2包含卡。 The connect table contains a primary key, and 2 values. 连接表包含一个主键和2个值。 On value is the primary key of a person, the other is the primary key of a car. 价值是一个人的主键,另一个是汽车的主键。 This table connects the people to the cars. 这张桌子将人与汽车连接起来。 With this I can make a table in PHP in which i can see who has wich car. 这样,我可以在PHP中创建一个表格,在其中可以看到谁拥有汽车。

This works fine, however every person that has multiple cars is shown more than once. 这可以正常工作,但是每个拥有多辆汽车的人都会显示多次。 So, for example: person one had both car number one and number two. 因此,例如:第一个人同时拥有第一和第二辆汽车。 The table now shows the name of person on 2 times and behind his name the car. 现在,该表显示了2个人的名字,并在其名字后面显示了汽车。 I would like it to show a table with the names of the persons just once, and then one or more cars behind that. 我希望只显示一张表,上面有这些人的名字,然后在后面有一辆或多辆汽车。

Below is an example of the way I have it set up now: 以下是我现在设置方式的示例:

$query = "SELECT firstname, lastname, car FROM people, cars, connecttabel         
WHERE connecttabel.personnumber = people.personnumer
AND connecttabel.carnumber = cars.carnumber";
$result = mysqli_query($database, $query)
or die ("error");
echo "<table>";
while($record=mysqli_fetch_array($result))
{$firstname = $record['firstname'];

and so on. 等等。 With the variables I fill in a table. 使用变量,我填写表格。

Does anyone have any idea how to help me out here? 有谁知道如何在这里帮助我? I think it should be done with a for loop but i've been trying for the last 3 hours and I can't get it done. 我认为应该使用for循环来完成,但是最近3个小时我一直在尝试,但无法完成。

I assume your query returns something like: 我假设您的查询返回的内容如下:

|   first_name   |   last_name   |   car   |
+------------------------------------------+
| John           | Doe           | Mustang |
| John           | Doe           | Camaro  |
| John           | Doe           | SL55    |
| Jane           | Deo           | Prius   |
| Jane           | Deo           | Renegade|

If you want just the number of cars each one have, you might try: 如果您只想要每个人拥有的汽车数量,则可以尝试:

SELECT firstname, lastname, COUNT(car) FROM people, cars, connecttabel         
WHERE connecttabel.personnumber = people.personnumer
AND connecttabel.carnumber = cars.carnumber GROUP BY firstname, lastname;

This will give you: 这将为您提供:

|   first_name   |   last_name   |COUNT(car)|
+-------------------------------------------+
| John           | Doe           | 3        |
| Jane           | Deo           | 2        |

But if you want something like... 但是如果您想要类似...

|   first_name   |   last_name   |       car_list        |
---------------------------------------------------------+
| John           | Doe           | Mustang, Camaro, SL55 |
| Jane           | Deo           | Renegade, Prius       |

You can use Group Concat : 您可以使用Group Concat

SELECT firstname, lastname, GROUP_CONCAT(DISTINCT car ORDER BY car) AS car_list FROM people, cars, connecttabel         
WHERE connecttabel.personnumber = people.personnumer
AND connecttabel.carnumber = cars.carnumber GROUP BY firstname, lastname
$query = "SELECT firstname, lastname, car FROM people, cars, connecttabel         
WHERE connecttabel.personnumber = people.personnumer
AND connecttabel.carnumber = cars.carnumber Group by people.id";
$result = mysqli_query($database, $query)
or die ("error");
echo "<table>";
while($record=mysqli_fetch_array($result))
{$firstname = $record['firstname'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM