Nov 8, 2009

Dynamic Drop Down List <select>

free web hosting
Open Discussion > MODERATED AREA > Computers > Programming Languages > PHP Programming

Dynamic Drop Down List <select>

kvarnerexpress
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:

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="&nbsp;&nbsp;&nbsp;&nbsp;";
       
      if ($width==72)
        $display="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
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>  



 

 

 


Comment/Reply (w/o sign-up)

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

CODE
<?php

function 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);
}

?>

 

 

 


Comment/Reply (w/o sign-up)

silentwind
QUOTE(Spectre @ Feb 3 2006, 03:54 PM) *

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.

CODE
<?php

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

Comment/Reply (w/o sign-up)

Spectre
Hmm, I didn't even pick up on that. Nice spotting. I just assumed the spaces where being added along with the option's text (as I noted, I didn't test anything).

Comment/Reply (w/o sign-up)



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)

Similar Topics

Keywords : dynamic, drop, list

  1. Getting An Array Value Of A Dynamic Variable
    (9)
  2. Trap17 Board Status Dynamic Image
    (13)
    I was really board a few days ago and decided to make a trap17 board status image for my sig
    /tongue.gif" style="vertical-align:middle" emoid=":P" border="0" alt="tongue.gif" /> That is the
    current beta. I'm still working on it so that it might display the most recent post or
    somthing, i'm not sure. After I finish it ill give out the source code but if you want one
    right now you can pm me and ill set one up to get the info on your account ^.^ So I was wondering
    how you members liked it? You'll probably think that its a waste of time, but alas as I sai....
  3. Can I Make Dynamic Menu In Php
    is it posible to make dynamic menu in php without javascript (7)
    As there are many java script by which we can have event based interaction like,message on mouse
    over etc,that we can create dynamic menu in javascripts to make navigation bar ,but is it possible
    in php to have this acomplished without javascripts,i am new to php,is it possible?. Thanks....
  4. More Dynamic ?id=browsing With Php (associative Array)
    Just create array and watch php code do the rest (1)
    The thing that has been bugging me for a while was that switch statement that we use to create ID
    browsing (some use If-Ifelse but results are the same for both). I wanted to figure out a way to use
    more dynamic switch statement so that i only need to update my links array in order to create links
    for template. With use of foreach, array_keys, and in_array functions finally i managed to do so.
    Also i'm planing on changing foreach with array_walk but i'll do that later. Now for the
    code.. First we create an associative array something like this CODE $glavni_....
  5. Dynamic Php Pages
    Nice tutorial (5)
    This is a really good tutorial on making php pages that normally appear as
    www.domainname.com/links.php appear as www.domainname.com/index.php?page=links
    http://nuwen.com/tutorials/php-dynamic-pages ....
  6. Dynamic Image / Signature Generator
    a simple code to change text on an image (12)
    In search of dynamically changing quote, saying or all other types of text on an image I came across
    a code that I have modified to fit my initial usage. This procedure requires two files and short
    knowledge of PHP. If you are familiar with Trap17's sig rotation code you will understand this
    procedure very fast. Code 1: dynamic_sig.php (you can rename this to index.php and you'll see
    at the end why) Code 2: a simple text file named anything (I will call it name.txt ) Code 1
    CODE header("Content-type: image/png"); $image = imagecreatefrompng("../i....
  7. Dynamic Included Menus
    path specification (8)
    I am looking for a way to make a dynamic menu using php and include, basicly I want to make a file
    that contains html formating source and links and include it somewhere in all my pages, the problem
    is that links are taken literaly and refered to as to where the html is running from. So is there a
    way I can make a file and include it in my pages, then it will determin where the page is running
    from, and generate the links with an approperate amount of ../../'s so if my index is at root,
    but every section has it's own folder, I want to be able to include this fil....
  8. Php Dynamic News Updating Using Mysql
    changing a sites news using MySQL and PHP (3)
    Hey everyone. It appears to me that everyone really know what they are doing in this forum and im a
    pretty new to this PHP and MySQL combo. I know some PHP but not MySQL....I am trying to make a new
    website but i want to be able to go to a certain update page and change the news on the main index.
    For example: - The main page show news updates of the last 10 post. - I go to the update page
    and then fill in the form and it will add this post to the top of the list and still only print the
    top 10. I have no clue how to do this with MySQL. At the moment i am doing it u....
  9. Calling A Variable From A Dynamic List In A Sql Statement
    (2)
    I would be greatful if anyone could help me out, I have been stuck on this for a while. The problem
    is that I have a drop down menu which I get the contents of from a database. Then when I press the
    get details button I want to use the value selected in my drop down menu and pass it into an SQL
    statement to recieve all te relevent info. Everything works if I specify a known variable, but the
    problem seems to be passing a variable in which is part of the array. Below is the code for the drop
    down menu and the Sql statement i'm trying to execute. drop down menu: $qu....

    1. Looking for dynamic, drop, list

Searching Video's for dynamic, drop, list
See Also,
advertisement


Dynamic Drop Down List <select>

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com