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.
<?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 |
Answer
Solution:
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 } ?>
See example
Answer
Solution:
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.
I hope you already know how to fetch data with PHP from 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>";
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: composer detected issues in your platform: your composer dependencies require a php version ">= 8.0.2".
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
About the technologies asked in this question
PHP
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
MySQL
DBMS is a database management system. It is designed to change, search, add and delete information in the database. There are many DBMSs designed for similar purposes with different features. One of the most popular is MySQL.
It is a software tool designed to work with relational SQL databases. It is easy to learn even for site owners who are not professional programmers or administrators. MySQL DBMS also allows you to export and import data, which is convenient when moving large amounts of information.
https://www.mysql.com/
Welcome to programmierfrage.com
programmierfrage.com is a question and answer site for professional web developers, programming enthusiasts and website builders. Site created and operated by the community. Together with you, we create a free library of detailed answers to any question on programming, web development, website creation and website administration.
Get answers to specific questions
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Help Others Solve Their Issues
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.