Jump to content

Best way to create dynamic dropdown menu's


shocker-z

Recommended Posts

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

Regards
Rich

Archived

This topic is now archived and is closed to further replies.

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