Results 1 to 10 of 10

Thread: Unlimited levels in drop-down

  1. #1
    Join Date
    May 2006
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Unlimited levels in drop-down

    What I need is something which will create a drop-down menu with unlimited levels.
    It will need to grab category information from the database, list the category (<option></option>) and search for sub-categories. It will need to do this an unlimited amount of times until there are no more categories.

    In effect it will create a <select> menu kind of like the one below (with value being the id of the category):

    HTML Code:
    <select name="parent">
    <option value="1">Root</option>
    <option value="3">- Sub </option>
    <option value="4">- - Sub</option>
    <option value="2">Root</option>
    </select>
    Any help appreciated

  2. #2
    Join Date
    Jan 2007
    Location
    The stage
    Posts
    568
    Thanks
    23
    Thanked 6 Times in 6 Posts

    Default

    Are looking for something like a loop?
    if so then this will help

    PHP Code:
    <html>
    <body>
    <select name="parent">
    <?php 
    $i
    =0;
    //leave the variable line above alone
    while($i<=100)
    //Where the number 100 is replace your own number...
      
    {
      echo 
    "<option value='" $i "'>" .$i ." Choice</option>";
      
    $i++;
    // Leave this be, unless you want to take the word choice out
      
    }
    ?>
    </select>
    </body>
    </html>
    I would not recommend doing more than 500 or 600... that could lead for some serious problems

  3. #3
    Join Date
    Oct 2006
    Location
    Shanghai, China
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by jonnyynnoj View Post
    What I need is something which will create a drop-down menu with unlimited levels.
    It will need to grab category information from the database, list the category (<option></option>) and search for sub-categories. It will need to do this an unlimited amount of times until there are no more categories.

    In effect it will create a <select> menu kind of like the one below (with value being the id of the category):

    HTML Code:
    <select name="parent">
    <option value="1">Root</option>
    <option value="3">- Sub </option>
    <option value="4">- - Sub</option>
    <option value="2">Root</option>
    </select>
    Any help appreciated
    I don't know what kind of database you're using but if you were using, for example, a text file with tab separation, you could do it like this:

    Code:
    <select name='form'>
    <?
    $lines = file("./database.txt");
    foreach($lines as $key => $value) {
    $piece = explode("\t", trim($value));
    echo "<option value='$piece[0]'>$piece[1]</option>\n";
    }
    ?>
    </select>
    *Edit: you should explode the $value, not the $line

  4. #4
    Join Date
    May 2007
    Location
    England, UK
    Posts
    235
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default

    if your trying to create the select options straight from a database (e.g. MySQL) then you can do the following:

    First you need some PHP code to connect to your database then use something like:

    HTML Code:
    <form>
    <select name="parent">
    PHP Code:
    <?php

    $query
    "SELECT * FROM categorytable ORDER BY id ASC";
    $result mysql_query($query) or die(mysql_error()); 

    while (
    $info mysql_fetch_array($result))
          {
          print 
    "<option value=\"" $info['id'] . "\">" $info['other'] . "</option>"
          

    ?>
    HTML Code:
    </select>
    </form>
    Whereby the variable "$info['id']" is pulling the data from a database field called "id" and the "$info['other']" variable from a field called, suprisingly enough, "other".

    Then everytime you add a category to your database it automatically appears in your form without the need to edit any code!

  5. #5
    Join Date
    Jul 2007
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    i agree with jc_gmk's solution except i do not see where it accounts for sub categories and subcategories of sub categories....
    firstly to really be able to solve this, we would have to know how your categories are stored. assuming you are using a database i think you should have a separate id for each category. and each one should have a parentID column. so that we know what category it falls under

    eg.

    ID parentID name
    1 0 first category
    2 1 subcategory of id1
    3 1 another subcategory of id1
    4 2 subcategory of id2 (which is a subcat of id1)
    5 0 second category
    5 1 subcategory of id 5
    etc...

    to add that functionality, you should use recursion (as you do not know how many sub categories u will use and how deep they will go)


    here is a brief draft of what i think u will need to do (i left out some code to make it clearer to see each step)

    HTML Code:
    <form>
    <select name="parent">

    PHP Code:
    //find all categories without a parent
    "SELECT id FROM categorytable WHERE parentID=0 ORDER BY id ASC";  
    {

            
    printoptions($id,"");
    }




    function 
    printoptions($id,$level)
    //this function print the category you send it and finds all its sub categories
    //$level is how deep subcategories is from the highest parent. it is represented by a "-"
    {

        
    //print the 'parent' category
        
    "SELECT * FROM categorytable WHERE id=$id
        print "
    <option value=\"" $info['id'] . "\">$level$info['other'] . "</option>"


        
    //now query to find all subcatagories of the parent
        
    "SELECT * FROM categorytable WHERE parentID=$id ORDER BY id ASC";
        while
        {
            
    $level.="-"//increase the level it is on
            
    printoptions($id,$level);    //now we send this category and see if it has subcategories.  Each category will get checked to see if it has any lower level categories.
        
    }

    HTML Code:
    </select>
    </form>
    please note that i left out the little things in the code...some quotation marks etc.

    hopefully this is what you were looking for

  6. #6
    Join Date
    May 2006
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thats the exactly want I want to do, however the code doesn't want to work for some reason. Here is what I have now:

    PHP Code:
    echo <<<HTML
    ....
    <select name="cats">
    HTML;

    function 
    printoptions($p_id,$level)
    //this function print the category you send it and finds all its sub categories
    //$level is how deep subcategories is from the highest parent. it is represented by a "-"
    {

    echo 
    "id = $p_id"// I added this for testing, and it echos the correct value..

        //print the 'parent' category
        
    $query2 "SELECT title FROM {$table_prefix}_articles_cats WHERE id='2' LIMIT 1";
        
    $result2 mysql_query($query2);
        
    $row2 mysql_fetch_array($result2);
        
            
    $p_title $row2['title'];
            
    echo 
    "p title = $p_title"// Again for testing, however outputs no value..
        
        
    echo '<option value="'.$p_id.'">'.$level.$p_title.'</option>';


        
    //now query to find all subcatagories of the parent
        
    $query3 "SELECT id FROM {$table_prefix}_articles_cats WHERE parent='$p_id' ORDER BY title ASC";
        
    $result3 mysql_query($query3);
        
        while (
    $row3 mysql_fetch_array($result3)){
        
            
    $s_id $row3['id'];
            
    $level.="- "//increase the level it is on
            
            
    printoptions($s_id,$level);    //now we send this category and see if it has subcategories.  Each category will get checked to see if it has any lower level categories.
        
    }
    }

    //find all categories without a parent
    $query "SELECT id FROM {$table_prefix}_articles_cats WHERE parent='0' ORDER BY title ASC";
    $result mysql_query($query);

    while (
    $row mysql_fetch_array($result)){
        
    $p_id $row['id'];
        
        
    printoptions($p_id,"");
    }

    echo <<<HTML
    </select>
    .....
    HTML; 
    However below are the errors i get:
    HTML Code:
    <select name="cats">id = 2<br />
    <b>Warning</b>:  mysql_fetch_array(): supplied argument is not a valid MySQL result resource in <b>/home/duffy92/public_html/admin/articles2.php</b> on line <b>1566</b><br />
    
    p id = 2<option value="2"></option><br />
    <b>Warning</b>:  mysql_fetch_array(): supplied argument is not a valid MySQL result resource in <b>/home/duffy92/public_html/admin/articles2.php</b> on line <b>1579</b><br />
    id = 1<br />
    <b>Warning</b>:  mysql_fetch_array(): supplied argument is not a valid MySQL result resource in <b>/home/duffy92/public_html/admin/articles2.php</b> on line <b>1566</b><br />
    
    p id = 1<option value="1"></option><br />
    <b>Warning</b>:  mysql_fetch_array(): supplied argument is not a valid MySQL result resource in <b>/home/duffy92/public_html/admin/articles2.php</b> on line <b>1579</b><br />
    </select>
    Line 1566 is: $row2 = mysql_fetch_array($result2);
    Line 1579 is: while ($row3 = mysql_fetch_array($result3)){

    Ive changed the table name, and the values selected from the table, but no sucess. Any help?

  7. #7
    Join Date
    Jan 2007
    Location
    The stage
    Posts
    568
    Thanks
    23
    Thanked 6 Times in 6 Posts

    Default

    Ok, so you want something like this I'm seeing...

    Option 1 - Root
    Option 2 - Sub of Root
    Option 3 - Sub of Root
    Option 4 - Root
    If so I think this code will work...
    Code:
    <html>
    <body>
    <select name="parent">
    <option value="Root">Root</option>
    <?php 
    $SubofRoot="0";
    //leave the variable line above alone
    while($SubofRoot<=100)
    //Where the number 100 is replace your own number...
      {
      echo "<option value='" . $SubofRoot . "'>" .$SubofRoot ." Choice</option>";
      $SubofRoot++;
    // Leave this be, unless you want to take the word choice out
      }
    ?>
    <option value="Root">Root2</option>
    <?php 
    $SubofRoot2="0";
    //leave the variable line above alone
    while($SubofRoot2<=100)
    //Where the number 100 is replace your own number...
      {
      echo "<option value='" . $SubofRoot2 . "'>" .$SubofRoot2 ." Choice</option>";
      $SubofRoot2++;
    // Leave this be, unless you want to take the word choice out
      }
    ?>
    </select>
    </body>
    </html>
    I hope thats what your looking for...

  8. #8
    Join Date
    Jul 2007
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by jonnyynnoj View Post

    Line 1566 is: $row2 = mysql_fetch_array($result2);
    Line 1579 is: while ($row3 = mysql_fetch_array($result3)){

    Ive changed the table name, and the values selected from the table, but no sucess. Any help?
    well its definately the query string eg. "SELECT id FROM {$table_prefix}_articles_cats WHERE parent='$p_id' ORDER BY title ASC"

    echo the query string and see if the syntax is right.

    maybe you want to remove the { and } from it... or why dont u just type the name of the table directly in there.

  9. #9
    Join Date
    May 2006
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by littleEd View Post
    well its definately the query string eg. "SELECT id FROM {$table_prefix}_articles_cats WHERE parent='$p_id' ORDER BY title ASC"

    echo the query string and see if the syntax is right.

    maybe you want to remove the { and } from it... or why dont u just type the name of the table directly in there.
    Yep, the { } was the problem. For some reason they work outside of the function but not inside?

    EDIT: Forgot to use global $table_prefix; inside the function.

    Working well now, thanks very much

  10. #10
    Join Date
    Jul 2007
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    glad i can help and glad it worked... i was starting to doubt myself lol

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •