Jump to content
xisto Community
Sign in to follow this  
kvarnerexpress

Dynamic Drop Down List <select>

Recommended Posts

Hi,

What I am trying to achieve is when you have a form drop down menu, it displays all the categories from the directory, with all the indentations. Something like when you submit your site to a directory.

Now I've got it almost working, but I can figure out how to indent it. The code below, I've used a spacer gif, but obviously that won't work!

I've had a go at this, as you can see, but that does do the indentations, unless I'm doing something wrong here.

Any help appreciated - thanks.


PHP Code:

function maketree($rootcatid,$level) { $sql="select catid,cat_name from tbl_name where parentid=$rootcatid order by cat_name"; $result=mysql_query($sql); while (list($DBcatid,$DBcatname)=mysql_fetch_row($result)) {        $width=($level+1)*24;           if ($width==48)          $display="    ";                if ($width==72)          $display="      "; if ($DBcatid==1) { $display.="<option value=$DBcatid>"; } else { $display.="<option value=$DBcatid>"; } $display.="$DBcatname</option>"; echo $display; maketree($DBcatid,$level+1); } } ?> <table cellpadding="2" cellspacing="2" border="0" width="width="100%""> <tr> <td><select name=""> <? maketree(0,0);?> </select></td> </tr> </table>  

Share this post


Link to post
Share on other sites

As far as indentation goes, it seems to work fine for me under Opera, FireFox, and Internet Explorer.

 

I hope you don't mind, but I've 'cleaned up' your function slightly. Note that it is quickly written and untested, and sctructured it in the way which I normally write PHP code, which may be a bit annoying for others. Obviously, you're free to simply discard it.

 

<?phpfunction maketree( $rootcatid, $level ) {   if( !is_numeric($rootcatid) || !is_numeric($level) ) {	  return;   }   $sql = 'SELECT catid, cat_name FROM tbl_name WHERE parentid = ' . $rootcatid . ' ORDER BY cat_name';   $result = @mysql_query($sql);   if( !$result || @mysql_num_rows($result) < 1 ) {	  return;   }   $level++;   while( $sql_data = @mysql_fetch_assoc($result) ){ 	  $db_catid = $sql_data['catid'];	  $db_catname = $sql_data['cat_name'];	  $display = '';	  for( $i=0;$i<($level*2);$i++ ) {		 $display .= '?';	  }	  $display .= '<option value="' . $db_catid . '">' . $db_catname . '</option>';	  echo "$display\n";	  maketree($db_catid,$level);   }   @mysql_free_result($result);}?>

Share this post


Link to post
Share on other sites

As far as indentation goes, it seems to work fine for me under Opera, FireFox, and Internet Explorer.

 

I hope you don't mind, but I've 'cleaned up' your function slightly. Note that it is quickly written and untested, and sctructured it in the way which I normally write PHP code, which may be a bit annoying for others. Obviously, you're free to simply discard it.

 

<?phpfunction maketree( $rootcatid, $level ) {   if( !is_numeric($rootcatid) || !is_numeric($level) ) {	  return;   }   $sql = 'SELECT catid, cat_name FROM tbl_name WHERE parentid = ' . $rootcatid . ' ORDER BY cat_name';   $result = @mysql_query($sql);   if( !$result || @mysql_num_rows($result) < 1 ) {	  return;   }   $level++;   while( $sql_data = @mysql_fetch_assoc($result) ){ 	  $db_catid = $sql_data['catid'];	  $db_catname = $sql_data['cat_name'];	  $display = '';	  for( $i=0;$i<($level*2);$i++ ) {		 $display .= ' ';	  }	  $display .= '<option value="' . $db_catid . '">' . $db_catname . '</option>';	  echo "$display\n";	  maketree($db_catid,$level);   }   @mysql_free_result($result);}?>

You put it in a wrong order dude.

Jut change che script from :

$display .= '<option value="' . $db_catid . '">' . $db_catname . '</option>';

echo "$display\n";

 

to :

 

$diaplay = '<option value="' . $db_catid . '"> . $display' . $db_catname . '</option>';

echo "$display\n";

And that should fix your problem.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.