Log in

View Full Version : Unlimited levels in drop-down



jonnyynnoj
09-12-2007, 09:38 PM
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):


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

Rockonmetal
09-12-2007, 10:02 PM
Are looking for something like a loop?
if so then this will help



<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

kasei
09-14-2007, 11:55 AM
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):


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

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:


<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

jc_gmk
09-14-2007, 03:29 PM
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:



<form>
<select name="parent">



<?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>"
}
?>



</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! :)

littleEd
09-22-2007, 06:23 PM
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)


<form>
<select name="parent">




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




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

jonnyynnoj
09-24-2007, 04:35 PM
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:


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:

<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? :confused:

Rockonmetal
09-24-2007, 09:39 PM
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...

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

littleEd
09-25-2007, 12:29 AM
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? :confused:

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.

jonnyynnoj
09-25-2007, 12:51 PM
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;)

littleEd
09-25-2007, 03:05 PM
glad i can help and glad it worked... i was starting to doubt myself lol