Jump to content


Photo

Best way to create dynamic dropdown menu's


  • Please log in to reply
3 replies to this topic

#1 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 08 September 2006 - 07:07 AM

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..

<?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>';

?>


but still needs alot of work...
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#2 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 08 September 2006 - 08:28 AM

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 AJAX Freaks

Regards
Rich
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#3 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 08 September 2006 - 09:06 AM

Thanks Rich i forgot about AJAX.. that will impress my new company ;)
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#4 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 08 September 2006 - 09:10 AM

However, your nested mysql_query()'s won't impress them at all :P

Learn to use JOIN's..




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users