bravo14 Posted February 24, 2009 Share Posted February 24, 2009 Hi Guys Does anybody know if it is possible to populate the Spry menus in Dreamweaver with menu items from a MySQL db. If so would you be able to point me in the right direction. Cheers Mark Quote Link to comment Share on other sites More sharing options...
sasa Posted February 24, 2009 Share Posted February 24, 2009 what is Spry menu? can you post same code Quote Link to comment Share on other sites More sharing options...
bravo14 Posted February 24, 2009 Author Share Posted February 24, 2009 In dreamweaver CS3 there are some Spry items and when they get added to a page the code looks like <ul id="MenuBar1" class="MenuBarVertical"> <li><a class="MenuBarItemSubmenu" href="#">Item 1</a> <ul> <li><a href="#">Item 1.1</a></li> <li><a href="#">Item 1.2</a></li> <li><a href="#">Item 1.3</a></li> </ul> </li> <li><a href="#">Item 2</a></li> <li><a class="MenuBarItemSubmenu" href="#">Item 3</a> <ul> <li><a class="MenuBarItemSubmenu" href="#">Item 3.1</a> <ul> <li><a href="#">Item 3.1.1</a></li> <li><a href="#">Item 3.1.2</a></li> </ul> </li> <li><a href="#">Item 3.2</a></li> <li><a href="#">Item 3.3</a></li> </ul> </li> <li><a href="#">Item 4</a></li> </ul> Quote Link to comment Share on other sites More sharing options...
sasa Posted February 24, 2009 Share Posted February 24, 2009 OK you want to change 'Item blah' and '#' in href with data from your database how you store menu in database? Quote Link to comment Share on other sites More sharing options...
bravo14 Posted February 24, 2009 Author Share Posted February 24, 2009 At present this is how the table looks with the menu options in, if there is a better way then please let me know menu_id menu parent_menu_id 1 Admin 0 2 1st XI 0 3 Fathoms 0 4 Badgers 0 5 Results 2 6 Reports 2 7 Committee 1 8 Selection Policy 1 There may be a better way, may be two tables, but I am not sure, any advice much appreciated Quote Link to comment Share on other sites More sharing options...
sasa Posted February 24, 2009 Share Posted February 24, 2009 try <?php function build_menu($data, $id=0, $pre=' '){ foreach ($data[$id] as $row){ if (isset($data[$row['menu_id']])){ echo "$pre<li><a class=\"MenuBarItemSubmenu\" href=\"#\">".$row[menu]."</a>\n"; echo "$pre <ul>\n"; build_menu($data, $row['menu_id'], $pre.' '); echo "$pre </ul>\n$pre</li>\n"; } else echo "$pre<li><a href=\"#\">$row[menu]</a></li>\n"; } } mysql_connect('localhost', 'root', '') or die(mysql_error()); mysql_select_db('test'); $res = mysql_query('SELECT * FROM menus') or die(mysql_error()); while ($row = mysql_fetch_assoc($res)) $data[$row['parent_menu_id']][] = $row; //print_r($data); echo '<ul id="MenuBar1" class="MenuBarVertical">',"\n"; build_menu($data); echo '</ul>'; ?> Quote Link to comment Share on other sites More sharing options...
bravo14 Posted February 24, 2009 Author Share Posted February 24, 2009 Thank you so much, it works a treat, is there any way in which you can comment the code so that I can se what has been done Quote Link to comment Share on other sites More sharing options...
dietkinnie Posted April 14, 2009 Share Posted April 14, 2009 Thank you so much, it works a treat, is there any way in which you can comment the code so that I can se what has been done Hi, and where shall i put the php code? Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted April 14, 2009 Share Posted April 14, 2009 is there any way in which you can comment the code so that I can se what has been done <?php // create build menu function function build_menu($data, $id=0, $pre=' '){ // for every bit of data, call it $row foreach ($data[$id] as $row){ // if $data[$row['menu_id']] exists... if (isset($data[$row['menu_id']])){ // then do this... // display hyperlink/s in a list echo "$pre<li><a class=\"MenuBarItemSubmenu\" href=\"#\">".$row[menu]."</a>\n"; // build the menu echo "$pre <ul>\n"; build_menu($data, $row['menu_id'], $pre.' '); echo "$pre </ul>\n$pre</li>\n"; // if $data[$row['menu_id']] doesn't exist... // just show the rest of the hyperlinks in a list } else echo "$pre<li><a href=\"#\">$row[menu]</a></li>\n"; } } // connect to database mysql_connect('localhost', 'root', '') or die(mysql_error()); // select the database mysql_select_db('test'); // select menu items from the `menus` table $res = mysql_query('SELECT * FROM menus') or die(mysql_error()); // pull table records until there is none and assign them to arrays while ($row = mysql_fetch_assoc($res)) $data[$row['parent_menu_id']][] = $row; //print_r($data); // build the multi layer menu echo '<ul id="MenuBar1" class="MenuBarVertical">',"\n"; build_menu($data); echo '</ul>'; ?> Quote Link to comment Share on other sites More sharing options...
dietkinnie Posted April 16, 2009 Share Posted April 16, 2009 Hi Everyone, Using the above example, how shall i proceed in adding links to the "nodes" of the menu so that when i click on "admin" i would get redirected to admin.php etc etc...? For example: menu_id menu parent_menu_id LINKS 1 Admin 0 admin.php 2 1st XI 0 1st.php 3 Fathoms 0 fathoms.php 4 Badgers 0 etc.php 5 Results 2 etc.php Thanks in advance! ??? Quote Link to comment Share on other sites More sharing options...
dietkinnie Posted April 16, 2009 Share Posted April 16, 2009 Hi Everyone, Using the above example, how shall i proceed in adding links to the "nodes" of the menu so that when i click on "admin" i would get redirected to admin.php etc etc...? For example: menu_id menu parent_menu_id LINKS 1 Admin 0 admin.php 2 1st XI 0 1st.php 3 Fathoms 0 fathoms.php 4 Badgers 0 etc.php 5 Results 2 etc.php Thanks in advance! ??? Manage to solve this one now. <?php // create build menu function function build_menu($data, $id=0, $pre=' '){ // for every bit of data, call it $row foreach ($data[$id] as $row){ // if $data[$row['menu_id']] exists... if (isset($data[$row['menu_id']])){ // then do this... // display hyperlink/s in a list echo "$pre<li><a class=\"MenuBarItemSubmenu\" href=\"$row[link]\">".$row[menu]."</a>\n"; // build the menu echo "$pre <ul>\n"; build_menu($data, $row['menu_id'], $pre.' '); echo "$pre </ul>\n$pre</li>\n"; // if $data[$row['menu_id']] doesn't exist... // just show the rest of the hyperlinks in a list } else echo "$pre<li><a href=\"$row[link]\">$row[menu]</a></li>\n"; } } // connect to database mysql_connect('localhost', 'root', '') or die(mysql_error()); // select the database mysql_select_db('test'); // select menu items from the `menus` table $res = mysql_query('SELECT * FROM menus') or die(mysql_error()); // pull table records until there is none and assign them to arrays while ($row = mysql_fetch_assoc($res)) $data[$row['parent_menu_id']][] = $row; //print_r($data); // build the multi layer menu echo '<ul id="MenuBar1" class="MenuBarVertical">',"\n"; build_menu($data); echo '</ul>'; ?> If the column added to the menu table was called 'link' then all i had to do was add $row[link] to the href statement. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.