[英]PHP MySQL Loop on two-level data
I am trying to extract two layer data from mysql database with a single mysql query and then display it grouped by top-layer with a php while loop.我正在尝试使用单个 mysql 查询从 mysql 数据库中提取两层数据,然后使用 php while 循环按顶层分组显示它。
<?php $org = "1";
$row1 = mysqli_prepare($db,"SELECT * FROM menu WHERE org = ? ORDER BY mid DESC");
$row1->bind_param("s", $org);
mysqli_stmt_execute($row1); $row2 = mysqli_stmt_get_result($row1);
while($row = mysqli_fetch_array($row2)){ ?>
<div class="category"><?php echo $row["item"]; /* GROUPED*/?>
<div class="item"><?php echo $row["item"]; /* INDIVIDUAL ITEM*/?></div>
</div>
<?php }; ?>
mid![]() |
parent![]() |
org![]() |
item![]() |
price![]() |
---|---|---|---|---|
1 ![]() |
1 ![]() |
Category 1![]() |
||
2 ![]() |
1 ![]() |
1 ![]() |
Item 1![]() |
$10 ![]() |
3 ![]() |
1 ![]() |
1 ![]() |
Item 2![]() |
$12 ![]() |
4 ![]() |
1 ![]() |
Category 2![]() |
||
5 ![]() |
4 ![]() |
1 ![]() |
Item 3![]() |
$12 ![]() |
6 ![]() |
4 ![]() |
1 ![]() |
Item 4![]() |
$90 ![]() |
Obviously each item will appear only once but also the category will appear as many times as the underlying items.显然,每个项目只会出现一次,但类别将与基础项目出现的次数一样多。 In the code however i need to group all items under the same parent category.
但是,在代码中,我需要将所有项目分组到同一父类别下。 Any ideas?
有任何想法吗?
Desired Result想要的结果
List![]() |
---|
Category 1![]() |
- Item 1 ![]() |
- Item 2 ![]() |
Category 2![]() |
- Item 3 ![]() |
- Item 4 ![]() |
Or in terms of the HTML code output:或者就 HTML 代码输出而言:
<div class="category">Parent Category 1<br>
<div class="item">Item 1</div>
<div class="item">Item 2</div>
</div>
<div class="category">Parent Category 3<br>
<div class="item">Item 3</div>
<div class="item">Item 4</div>
</div>
This should do it;应该这样做;
SELECT T.*,
IF(T.parent IS NULL, T.mid, T.parent) AS ParentGroup
FROM mytable as T
WHERE T.org = 1 # Comment out here to get for all org
ORDER BY T.org ASC, ParentGroup ASC, T.parent IS NULL DESC, T.mid ASC
Note: Please read the SQL injection policies carefully before using SQL statements with your code.注意:在您的代码中使用 SQL 语句之前,请仔细阅读 SQL 注入策略。
I hope you already know how to fetch data with PHP from MySQL;我希望你已经知道如何用 PHP 从 MySQL 中获取数据; once all records are fetched, you can loop through like below;
获取所有记录后,您可以像下面这样循环;
... Here you already fetched all the records from database into $Recordset array
foreach($Recordset as $Data){ // Loop through the record set
if($Data["parent"]){
print "<li>{$Data["item"]}</li>"; // Item
}
else{
print "<div class=\"Category\"><h1>{$Data["item"]}</h1><ul class=\"Item\">"; // Category
}
}
print "</ul></div>";
Add flow control to your code将流控制添加到您的代码中
<?php
$org = "1";
$row1 = mysqli_prepare($mysqli,"SELECT * FROM menu WHERE org = ? ORDER BY mid ASC");
$row1->bind_param("s", $org);
mysqli_stmt_execute($row1);
$row2 = mysqli_stmt_get_result($row1);
$controlfirst = 0;
while( $row = mysqli_fetch_array($row2)) {
if ( !isset($row["parent"])) {
if ( $controlfirst == 1) { ?>
</div>
<?php } ?>
<div class="category"><?php echo $row["item"]; /* GROUPED*/
$controlfirst = 1;
}
else {
?>
<div class="item"><?php echo $row["item"];?> </div>/* INDIVIDUAL ITEM*/
<?php }
}
if ( $controlfirst == 1) { ?>
</div>
<?php } ?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.