Jump to content

Need some help with MySQL and arrays


InsiteFX

Recommended Posts

Hi,

 

I have a menu system that uses a lot of arrays SEE BELOW:

 


$menu = array
(
   array
   (
       'title' => 'Item One',
       'url' => 'item_one.php',
   ),
   array
   (
       'title' => 'Item Two',
       'url' => 'item_two.php',
       'children' => array
       (
           array
           (
               'title' => 'Item Three',
               'url' => 'item_three.php',
           ),
           array
           (
               'title' => 'Item Four',
               'url' => 'item_four.php',
           )
       )
   )
);

$attrs = array('id' => 'navigation', 'class' => 'menu');

 

I need to store these arrays in a MySQL table and then be able to get them back as the same arrays for building the menu.

 

I am not sure how to sturcture this, tables etc.

 

Thank you

InsiteFX

Link to comment
Share on other sites

Hi InsiteFX Here is what you nee. Please reply me if you do not understand the process.

 

<?php 
/*
database script
CREATE TABLE `menu` (
 `id` int(11) NOT NULL auto_increment,
 `title` varchar(100) collate latin1_general_ci default NULL,
 `url` text collate latin1_general_ci,
 `parent` int(11) default NULL,
 `menuorder` int(11) default NULL,
 PRIMARY KEY  (`id`)
)
insert  into `menu`(`id`,`title`,`url`,`parent`,`menuorder`) values (1,'Item1','Item1.php',0,1),(2,'Item2','Item2.php',0,2),(3,'Item3','Item3.php',1,3),(4,'Item4','Item4.php',2,4),(5,'Item5','Item5.php',0,5),(6,'Item6','Item6.php',2,6),(7,'Item7','Item7.php',6,7),(8,'Item8','Item8.php',7,8),(9,'Item9','Item9.php',5,9);
*/

if(mysql_connect("localhost","root",""))
mysql_select_db("test") or die("could not select database");
else
echo "could not connect";

function getChild($id=0)
{
   static $cates = array();
   static $times = 0;
   $times++;
   $result = mysql_query("SELECT * FROM menu WHERE parent=$id ORDER BY menuorder");
   while($row = mysql_fetch_assoc($result))
   {
       $cates['title'][] = str_repeat("_ ",$times-1).$row['title'];
       $cates['url'][]=$row['url'];
       getChild($row['id']);
   }
   $times--;
   return $cates;
}

echo "";
print_r(getChild(0));
echo "";
?>

Edited by bishwadeep
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...