shocker-z Posted September 8, 2006 Share Posted September 8, 2006 Hi all,Just wanting your advice on the best way to create a 2 tiered list/expanding menu.Is it better to use PHP onl;y and do it like that using $_GET to expand and show what page a user is on.. or is it better to use javascript and use PHP to create it before use?Either way has anyone got any tips or good scripts??at the moment i have started creating a PHP ONLY version and code is like this..[code]<?php$cat=mysql_real_escape_string($_GET['cat']);$item=mysql_real_escape_string($_GET['item']);echo '<table width="120" border="0" cellspacing="0" cellpadding="0">';$getcats=mysql_query('SELECT * FROM cat ORDER by cat') or die(mysql_error());while ($cats=mysql_fetch_array($getcats)) { $items=mysql_query("SELECT * FROM items WHERE catid = '$cats[ID]'"); $catscount=mysql_num_rows($items); $itemid=mysql_fetch_array($items); if ($itemid['ID'] == $item) { if ($catscount > 1) { if ($cat == $cats['ID']) { $symbol='-'; } elseif ($cat !== $cats['ID']) { $symbol='+'; } echo '<tr> <td align="left">'.$symbol.$cats['cat'].'</td> </tr>'; } else { echo '<tr> <td align="left">'.$cats['cat'].'</a></td> </tr>'; } } elseif ($itemid['ID'] !== $item) { if ($catscount > 1) { if ($cat == $cats['ID']) { $symbol='-'; } elseif ($cat !== $cats['ID']) { $symbol='+'; } echo '<tr> <td align="left"><a href="?cat='.$cats['ID'].'&item='.$itemid['ID'].'">'.$symbol.$cats['cat'].'</a></td> </tr>'; } else { echo '<tr> <td align="left"><a href="?cat='.$cats['ID'].'&item='.$itemid['ID'].'">'.$cats['cat'].'</a></td> </tr>'; } } if ($cat == $cats['ID']) { $getitems=mysql_query("SELECT * FROM items WHERE catid = '$cat'") or die(mysql_error()); while ($itemdata=mysql_fetch_array($getitems)) { echo '<tr> <td align="left"><a href="?cat='.$cats['ID'].'&item='.$itemdata['ID'].'">--'.$itemdata['item'].'</a></td> </tr>'; } }} echo '</table>';?>[/code]but still needs alot of work... Quote Link to comment https://forums.phpfreaks.com/topic/20092-best-way-to-create-dynamic-dropdown-menus/ Share on other sites More sharing options...
HuggieBear Posted September 8, 2006 Share Posted September 8, 2006 Here's one of the situations where AJAX may actually be appropriate. Lots of people are using it in the wrong place, or just using it for the sake of it, but you could do well to use it here.This will give you an initial list of cats, when you click on a link to one of them, it can create another list next to it with the items in that cat. It will do this without refreshing the page, just going and getting the latest data from the database using an HTTP request object via JavaScript.Check out this very simple tutorial at [url=http://www.ajaxfreaks.com/tutorials/1/0.php]AJAX Freaks[/url]RegardsRich Quote Link to comment https://forums.phpfreaks.com/topic/20092-best-way-to-create-dynamic-dropdown-menus/#findComment-88209 Share on other sites More sharing options...
shocker-z Posted September 8, 2006 Author Share Posted September 8, 2006 Thanks Rich i forgot about AJAX.. that will impress my new company ;) Quote Link to comment https://forums.phpfreaks.com/topic/20092-best-way-to-create-dynamic-dropdown-menus/#findComment-88229 Share on other sites More sharing options...
Jenk Posted September 8, 2006 Share Posted September 8, 2006 However, your nested mysql_query()'s won't impress them at all :pLearn to use JOIN's.. Quote Link to comment https://forums.phpfreaks.com/topic/20092-best-way-to-create-dynamic-dropdown-menus/#findComment-88233 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.