Using recursion to create navigation

I create navigation for the site and for life I can not understand recursion. I have all my data stored through MySQL using this design:

enter image description here

I read some links about how recursion works, and I have to be slow because I find it hard to understand. I tried to write something, and I know that it is not even close to what I really need, but this is the beginning:

PDO

public function viewCategories() { $viewSQL = "SELECT * FROM categories"; try { $pdo = new PDO('mysql:host=localhost;dbname=store','root',''); $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); $categoryVIEW = $pdo->prepare($viewSQL); $categoryVIEW->execute(); $array = $categoryVIEW->fetchAll(PDO::FETCH_ASSOC); $categoryVIEW->closeCursor(); $json = json_encode($array); return $json; } catch(PDOexception $e) { return $e->getMessage(); exit(); } } 

recursion

 $return = json_decode($category->viewCategories(),true); function buildNavigation($json) { foreach($json as $item) { if($item['category_id'] === $item['parent']) { print('<li>'.$item['category_name'].'</li>'); if($item['category_id'] === $item['parent']) { print('<li>match'.$item['category_name'].'</li>'); buildNavigation($json); } } } buildNavigation($return); 

as expected, this will never enter into a condition. I tried to figure it out on my own because it is a good thing to know, but I guess that goes beyond my mental capacity :(

Thanks for watching :)

UPDATE

I know that an answer has already been given, but is there a way to do this to create an associative array? I played with a function that ALMOST works for me, which I got from HERE , but adds an extra array that I DO NOT want.

Method

 private function buildCategories($array,$parent) { $result = array(); foreach($array as $row) { if($row['parent'] == $parent) { $result[$row['category_name']] = $this->buildCategories($array,$row['category_id']); } } return $result; } $json = json_encode($this->buildCategories($array,NULL)); return $json; 

I want it:

 {"reloading":{"components","presses and dies","tumblers & scales","tools & accessories","shotshell reloading"} 

but I get the following:

 {"reloading":{"components":[],"presses and dies":[],"tumblers & scales":[],"tools & accessories":[],"shotshell reloading":[]} 
+4
source share
2 answers

Here is an example with recursion.

 function buildNavigation($items, $parent = NULL) { $hasChildren = false; $outputHtml = '<ul>%s</ul>'; $childrenHtml = ''; foreach($items as $item) { if ($item['parent'] == $parent) { $hasChildren = true; $childrenHtml .= '<li>'.$item['category_name']; $childrenHtml .= buildNavigation($items, $item['category_id']); $childrenHtml .= '</li>'; } } // Without children, we do not need the <ul> tag. if (!$hasChildren) { $outputHtml = ''; } // Returns the HTML return sprintf($outputHtml, $childrenHtml); } print buildNavigation($items); 

What the script outputs the following result:

 <ul> <li>Menu 1</li> <li>Menu 2 <ul> <li>Sub Menu 2.1</li> <li>Sub Menu 2.2</li> <li>Sub Menu 2.3 <ul> <li>Sub Menu 2.2.1</li> <li>Sub Menu 2.2.2</li> <li>Sub Menu 2.2.3</li> </ul> </li> </ul> </li> <li>Menu 3</li> </ul> 
+14
source

I have the same code with a little modification, so the user can apply different css at each menu level, now it shows submenu classes such as child-class1 when it is in the first submenu and it will show child-class2 when it is in the second submenu, etc.

  <?php function buildNavigation($items, $parent = NULL, $n=NULL) { $hasChildren = false; if ($parent == NULL) { $level=0; $outputHtml = '<ul class="parent-class">%s</ul>'; } else { if($n==NULL) { $level=1; } else { $level=$n; } $outputHtml = '<ul class="child-class'.$level.'">%s</ul>'; } $childrenHtml = ''; foreach($items as $item) { if ($item['parent'] == $parent) { $hasChildren = true; $childrenHtml .= '<li><a href="/'.$item['slug'].'">'.$item['ptitle'].'</a>'; $next = ++$level; $childrenHtml .= buildNavigation($items, $item['pageid'],$next); $childrenHtml .= '</li>'; } } // Without children, we do not need the <ul> tag. if (!$hasChildren) { $outputHtml = ''; } // Returns the HTML return sprintf($outputHtml, $childrenHtml); } echo buildNavigation($ppages); ?> 

it will be displayed as follows

  <ul class="parent-class"> <li> <a href="http://example.com/page-1">page 1</a> <ul class="child-class1"> <li> <a href="http://example.com/this-is-child-page">this is child page</a> <ul class="child-class2"> <li> <a href="http://example.com/child-of-child">child of child</a> </li> </ul> </li> </ul> </li> </ul> 

I would like to thank Mr. @Maxime Morin.

0
source

Source: https://habr.com/ru/post/1441155/


All Articles