Searching and Trees
|
|
|
|
<?php // un tree so to speak this data tree function untree($parent, $level)
{ $parentsql = mysql_query("select id, domain from domains where underid=$parent order by domain"); // if it is a leaf (no data underneath it) then return
if (!mysql_num_rows($parentsql))
{
return;
} //else echo the result, and recurse the function (so to speak)
else
{
while($branch = mysql_fetch_row($parentsql))
{
$echo_this = "<option value=$branch[0]>"; //give it some indents in the select box
for ($x=1; $x<=$level; $x++)
{
$echo_this .= " ";
}
$echo_this.="$branch[1]</option>"; //echo the <option> tag
echo $echo_this; //now run this function and find everthing where the parent is equal to the ID of this entry
$rename_level = $level;
untree($branch[0], ++$rename_level);
}
}
}
/* AND HERE IS HOW YOU CALL THE FUNCTION */
// get ALL the entries that have NO parent $compsql = mysql_query("select * from domains where underid=0 order by domain");
while ($companys = mysql_fetch_array($compsql))
{
echo "<option value=$companys[id]>$companys[domain]</option>"; //call the untree function and find all the entries that hive THIS entry as a parent (therefore those entries are children of this one)
untree($companys[id], 1);
} ?>
|
|
|
Usage Example
|
|
|
Rate This Script
|
|
|
|