Jump to content

Recommended Posts

I have a table that I want to create into a horizontal menu using lists/CSS in my header. I have the code that creates the menu, I just can't figure out the foreach loops to actually generate the menu.

 

$refs = array();
$list = array();

$result = full_query("SELECT id,title,slug,parent FROM `page`");

while($data = mysql_fetch_assoc($result))
{
$thisref = &$refs[$data['id']];

$thisref['parent'] = $data['parent'];
$thisref['title'] = unserialize($data['title']);
$thisref['slug'] = $data['slug'];

if($data['parent'] == 0)
{
$list[$data['id']] = &$thisref;
}
else
{
$refs[$data['parent']]['children'][$data['id']] = &$thisref;
}
}

// can't figure this part out
foreach($list as $keys => $var)
{
foreach($var as $vkey => $vvar)
{
if(isset($vkey['children']))
{
print $vkey;
}
}
}

 

Which, with my current data, results in this array:

Array
(
[4] => Array
 (
	 [children] => Array
		 (
			 [1] => Array
				 (
					 [parent] => 4
					 [title] => Child1
					 [id] => 1
					 [slug] => child1
				 )

		 )

	 [parent] =>
	 [title] => Parent1
	 [id] => 4
	 [slug] => parent1
 )

[6] => Array
 (
	 [children] => Array
		 (
			 [2] => Array
				 (
					 [parent] => 6
					 [title] => child2
					 [id] => 2
					 [slug] => child2
				 )

			 [3] => Array
				 (
					 [parent] => 6
					 [title] => child3
					 [id] => 3
					 [slug] => child3
				 )

			 [8] => Array
				 (
					 [parent] => 6
					 [title] => child4
					 [id] => 8
					 [slug] => child4
				 )

		 )

	 [parent] =>
	 [title] => Parent2
	 [id] => 6
	 [slug] => parent2
 )

[7] => Array
 (
	 [parent] =>
	 [title] => Parent3
	 [id] => 7
	 [slug] => parent3
 )

[9] => Array
 (
	 [parent] =>
	 [title] => Parent4
	 [id] => 9
	 [slug] => parent4
 )

)

 

Am trying to get the foreach to do this:

 

Parent1

  • Child1

Parent2

  • Child2
  • Child3
  • Child4

Parent3

Parent4

 

But horizontally. Any help is greatly appreciated!

Edited by Hybride
Link to comment
https://forums.phpfreaks.com/topic/270371-horizontal-menu-with-php-array/
Share on other sites

I figured it out, not sure if this is a bit brutish, so I would love a more elegant answer if possible:

// and published = '1' //true
                   $refs = array();
                   $list = array();

                   $result = full_query("SELECT id,title,slug,parent FROM `pages` WHERE title!='404' AND slug!='404'");

                   while($data = mysql_fetch_assoc($result))
                   {
                       $thisref = &$refs[$data['id']];

                       $thisref['parent'] = $data['parent'];
                       $thisref['title'] = $data['title'] = pages_data_lang2(unserialize($data['title']));
                       $thisref['slug'] = $data['slug'];
                       $thisref['id'] = $data['id'];

                       if($data['parent'] == 0)
                       {
                           $list[$data['title']] = &$thisref;
                       }
                       else
                       {
                           $refs[$data['parent']]['children'][$data['id']] = &$thisref;
                       }
                   }

                   print "<div id='topmenu'><ul>";
                   foreach($list as $keys => $var)
                   {
                       if(($var['id'] == 4) || ($var['id'] == 6) || ($var['id'] == 7) || ($var['id'] == 11))
                       {
                           print "<li><a href='".$var['slug']."'>$keys</a><ul>";

                           foreach($var['children'] as $vkey=>$vvar)
                           {
                               print "<li><a href='".$vvar['slug']."'>".$vvar['title']."</a></li>";
                           }
                           print "</ul>";
                       }

                   }
                   echo "</ul></div>";

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.