[英]displaying php mysql query result with one to many relationship
Heres A Sample Table:这是一个示例表:
info table
info_id name
1 john
2 peter
------------------------
details table
details_id log date
1 test log for john 2013-08-01
1 another log for john 2013-08-02
2 test log for peter 2013-08-02
Here's my Sample Query:这是我的示例查询:
SELECT info.info_id, info.name, details.details_no, details.log, details.date
FROM info JOIN details ON details.details_id = info.info_id
GROUP BY info.info_id
And Here's display i want to achieve:这是我想要实现的显示:
john
1 test log for john 2013-08-01
1 another test log for john 2013-08-02
peter
2 test log for peter 213-08-02
I have tried using foreach loop and then execute another foreach loop inside the first loop.我尝试使用 foreach 循环,然后在第一个循环内执行另一个 foreach 循环。
Please Help Guys请帮助伙计们
Try making your results into a multidimensional array like below尝试将您的结果变成如下所示的多维数组
Note: I am assuming that details.details_no
is a primary key for the details
table and you want results similar to注意:我假设details.details_no
是details
表的主键,并且您希望结果类似于
john
1 test log for john 2013-08-01
2 another test log for john 2013-08-02
peter
3 test log for peter 213-08-02
Which you can retrieve with the following您可以使用以下方法检索
...
$qry = "
SELECT
info.info_id,
info.name,
details.details_no,
details.log,
details.date
FROM
info
JOIN details ON (details.details_id = info.info_id)
";
$result = mysqli_query($your_db_link,$qry)
$data = array();
while($row = mysqli_fetch_array($result)){
$data[$row["info_id"]]["name"] = $row["name"];
$data[$row["info_id"]]["logs"][$row["details_no"]] = array(
"log"=>$row["log"],
"date"=>$row["date"],
);
}
Would result in an array like:将导致一个数组,如:
$data = array(
"1" => array(
"name" => "john",
"logs" => array(
"1" => array(
"log" => "test log for john",
"date" => "2013-08-01",
),
"2" => array(
"log" => "another test log for john",
"date" => "2013-08-02",
),
),
),
"2" => array(
"name" => "peter",
"logs" => array(
"3" => array(
"log" => "test log for peter",
"date" => "2013-08-02",
),
),
),
);
Then you could display like:然后你可以显示如下:
echo "<table>";
foreach($data as $value){
echo "<tr><td colspan='3'>".$value["name"]."</td></tr>";
foreach($value["logs"] as $subkey => $subvalue){
echo "<tr>";
echo "<td>".$subkey."</td>";
echo "<td>".$subvalue["log"]."</td>";
echo "<td>".$subvalue["date"]."</td>";
echo "</tr>";
}
}
echo "</table>";
Which would result similar to:这将导致类似于:
<table>
<tr>
<td colspan='3'>john</td>
</tr>
<tr>
<td>1</td>
<td>test log for john</td>
<td>2013-08-01</td>
</tr>
<tr>
<td>2</td>
<td>another test log for john</td>
<td>2013-08-02</td>
</tr>
<tr>
<td colspan='3'>peter</td>
</tr>
<tr>
<td>3</td>
<td>test log for peter</td>
<td>2013-08-02</td>
</tr>
</table>
You are probably going to have to take the data and make the array that you want.您可能将不得不获取数据并制作您想要的数组。
$data = array();
foreach ($result as $item) {
$key = $item['name']; // or $item['info_id']
if (!isset($data[$key])) {
$data[$key] = array();
}
$data[$key][] = $item;
}
// Build your table with the new $data array
EDIT编辑
This is just an example.这只是一个例子。 As amaster507 points out, if your name
field isn't unique, you will need to build your array on a unique key.正如 amaster507 指出的那样,如果您的name
字段不是唯一的,您将需要在唯一键上构建您的数组。 Not terribly different from this, as you could probably just change instances of $item['name']
to $item['info_id']
.与此没有太大不同,因为您可能只需将$item['name']
实例更改为$item['info_id']
。
Ok, first an explanation of the problem.好的,先解释一下问题。
When you use GROUP BY
, you no longer get every result as a row, but instead, you get 1 row with grouped content, that is, you can use GROUP BY
to get something like the concatenation of both log
cells from john, for example.当您使用GROUP BY
,您不再将每个结果作为一行获得,而是获得 1 行包含分组内容,也就是说,您可以使用GROUP BY
来获得类似于来自 john 的两个log
单元的连接的内容,例如.
To do what you want, remove the GROUP BY
to get all the rows, and then manually process them.要执行您想要的操作,请删除GROUP BY
以获取所有行,然后手动处理它们。
I recommend you to make easy the job, to use underscore.php which has a function called groupBy
which will return an array grouped in subarrays, based on this case, the info_id
column.我建议您groupBy
这项工作,使用underscore.php ,它有一个名为groupBy
的函数,它将返回一个分组在子数组中的数组,基于这种情况, info_id
列。
It will be used something like this:它将被使用如下:
$array_with_all_the_rows = $query->fetchAll(); // Or whatever you use to get all rows in 1 array.
$grouped_array = __::groupBy($array_with_all_the_rows, 'info_id');
That grouped array will be like this:该分组数组将如下所示:
$grouped_array = Array(
"1" => Array(
"info_id"=> "1", "log"=>"test log for john", "date" => "2013-08-01",
"info_id"=> "1", "log"=>"another test log for john", "date" => "2013-08-02"
),
"2" => Array(
"info_id"=> "2", "log"=>"test log for peter", "date" => "2013-08-02"
)
)
To refine Ascherer's answer, you don't need to declare arrays before pushing data into them with []
syntax.要完善 Ascherer 的答案,您无需在使用[]
语法将数据推送到数组之前声明数组。 If you were using array_push()
the array would need to be declared as a variable first.如果您使用array_push()
则需要先将数组声明为变量。
With MySQLi, you can iterate the result set object directly with a foreach()
and unconditionally use the info_id
values as keys to group by.使用 MySQLi,您可以直接使用foreach()
迭代结果集对象,并无条件地使用info_id
值作为分组依据。
$sql = "SELECT info.info_id,
info.name,
details.log,
details.date
FROM info
JOIN details ON details.details_id = info.info_id
ORDER BY info.info_id";
$result = [];
foreach ($mysqli->query($sql) as $row) {
$data[$row['info_id']][] = $row;
}
With PDO, you can use ->fetchAll(PDO::FETCH_GROUP) to create the same output without manually looping.使用 PDO,您可以使用->fetchAll(PDO::FETCH_GROUP)来创建相同的输出,而无需手动循环。
When generating your html markup...生成 html 标记时...
<table>
<?php foreach ($result as $groupId => $rows) { ?>
<tr>
<td colspan='3'><?php echo $rows[0]["name"]; ?></td>
</tr>
<?php foreach($rows as $row) { ?>
<tr>
<td><?php echo $groupId; ?></td>
<td><?php echo $row['log']; ?></td>
<td><?php echo $row['date']; ?></td>
</tr>
<?php } ?>
<?php } ?>
</table>
have you tried a foreach with a key/value pair?您是否尝试过带有键/值对的 foreach? can you show us your for loops currently?你能告诉我们你目前的 for 循环吗? a print_r/var_dump for the resulting array will make it easier for someone to help you.结果数组的 print_r/var_dump 将使某人更容易为您提供帮助。
foreach ($result as $key=>$value) {
print "Name Row\n";
for (each display row) {
That or as Ascherer suggests make a new multidimensional array that will make nested for loops unnecessary.那或者 Ascherer 建议创建一个新的多维数组,这将使嵌套的 for 循环变得不必要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.