I' am creating a hierarchical menu using MySQL Database. I' am currently stuck at this point where I' am not able to convert the multidimensional array into a string.
The Outcome
/media/photo-gallery/Title-1
Var Dump of the array
array(2) {
[0]=>
array(2) {
[0]=>
array(1) {
[1]=>
string(5) "media"
}
[1]=>
string(13) "photo-gallery"
}
[1]=>
string(7) "Title-1"
}
I have tried using Implode function, this is what I get
$path = implode("/", $path);
string(19) "Array/photo-gallery"
Get the parent slug from database
function get_path( $page_id ) {
global $db;
$sql = "SELECT * FROM pages WHERE page_id = $page_id";
$query = $db->SELECT($sql);
$rows = $db->FETCH_OBJECT();
$path = array();
foreach( $rows as $row ){
$page_id = $row->page_id;
$page_parent = $row->page_parent;
$page_slug = $row->page_slug;
$path[] = $page_slug;
$path[] = get_path( $page_parent );
$path = array_reverse( $path );
$path = array_filter( $path );
}
return $path;
}
Displays the HTML Menu
function display_children( $parent = "0", $level = "0" ){
global $db;
echo "<ul>";
// retrieve all children of $parent
$sql = "SELECT * FROM pages WHERE page_parent = $parent";
$query = $db->SELECT($sql);
$rows = $db->FETCH_OBJECT();
foreach( $rows as $row ){
$page_id = $row->page_id;
$page_parent = $row->page_parent;
$page_title = $row->page_title;
$page_slug = $row->page_slug;
$path = get_path($page_parent);
$path = array_unique(array_values($path));
var_dump($path);
echo "<li>";
echo "<a href=\"\">";
echo "$page_title | Parent ID: $page_parent";
echo "</li>";
echo "</a>";
// child's children
display_children($page_id, $level + 1);
}
echo "</ul>";
}
Title-1coming underphoto-gallery? do you mean to merge the[0]part recursive and then append[1]?