foreach in php(menu and sub menu)

I have created a menu and submenu by oop php, I have a problem with foreach loop. what I am trying to achieve is to have my SUBMENU array loop only in the third element of my MENU array!!! I hope I have described it well!
This is the foreach code:
<ul id="navigation">
<?php
$HeaderMenuOBJ = new Headermenu();
$HeaderMenu = $HeaderMenuOBJ->getHeaderMenu();
$SubHeaderMenu = $HeaderMenuOBJ->getHeaderSubMenu();
//var_dump($SubHeaderMenu);
//var_dump($HeaderMenu);
foreach ($HeaderMenu as $row){
?>
<li>
<a class="active"
href="
<?php
echo $row['href'];
?>
">
<?php echo($row['content']);?>
</a>
<ul class="submenu">
<?php
foreach ($SubHeaderMenu as $row){
?>
<li>
<a
href="
<?php
echo $row['href'];
?>
">
<?php
echo $row['content'];
?>
</a>
</li>
<?php
}
?>
</ul>
</li>
<?php
}
?>
</ul>
and this is my DOM (kind of controller to read my data from database) :
class Headermenu extends dbh {
public $main_query = "SELECT * FROM `header_menu` WHERE parent is null";
public $sub_query = "SELECT * FROM `header_menu` WHERE parent = 3";
public function getHeaderMenu(){
$menu = parent::connect()->query($this->main_query);
while($row = $menu->fetch_array()){
$rows[] = $row;
/*$submenu = parent::connect()->query(str_replace("@param",$header->PK_ID,$this->sub_query));
while($sub_header = $submenu-> fetch_array()){
$header->subheaders[]=$sub_header;
}*/
//$headers[] = $header;
}
return $rows;
}
public function getHeaderSubMenu(){
$sub_menu = parent::connect()->query($this->sub_query);
while($row = $sub_menu->fetch_array()){
$rows[] = $row;
}
return $rows;
}
}
Answer
Solution:
Direct answer:
First you need 2 different variables in outer and inner foreach:foreach ($HeaderMenu as $menuRow){
andforeach ($SubHeaderMenu as $subMenuRow){
now both have$row
. You can do something like this:if ($menuRow['id'] == 3) { foreach ($SubHeaderMenu as $subMenuRow){
- so enclose inner foreach in thisif
and do it only if parent id is 3, but it would be better to fetch all header_menu rows, transform that data into nested array and display as is with dynamic structure.
Dynamic structure answer:
Fetch all menu items, create array with menus, and each menu will have its submenu items. Then displaying this is easy. I assume only 2 levels - menu and submenu.
sample data:
+----+-------+
Code:
<?php
$mysqli = new mysqli("localhost", "zz", "zz", "zz");
// fetch all menu items
$query = 'select * from header_menu order by parent_id';
$result = $mysqli->query($query);
$data = $result->fetch_all(MYSQLI_ASSOC);
var_dump($data);
// build menu with menus and their submenus
$menu = [];
foreach ($data as $row) {
if ($row['parent_id'] === null) {
$menu[$row['id']] = $row;
$menu[$row['id']]['submenus'] = [];
} else {
$menu[$row['parent_id']]['submenus'][] = $row;
}
}
var_dump($menu);
// now display it in html or however you want
foreach ($menu as $item) {
echo $item['content'].PHP_EOL;
foreach ($item['submenus'] as $subitem) {
echo ' '.$subitem['content'].PHP_EOL;
}
}
$data:
{-code-8}
$menu:
{-code-9}
Output of the menu structure:
{-code-10}
Now you can output your all menu items with its submenu items, not only for menu id 3.
Answer
Answer
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: err_ossl_pem_no_start_line
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.