views:

19

answers:

2

Does anyone know how to display MySQL db hierarchical data (Nested Set Model (http://www.phpro.org/tutorials/Managing-Hierarchical-Data-with-PHP-and-MySQL.html)) in a combo box as shown here under the "Category:" comboxbox field:http://dir.globetourism.biz/submit.php

Thanks

A: 

What you need to do is when you print your <option> tags for your combo box you have to check the depth (how to get the depth is in the article that's linked in your question) of each element and print that many "| " strings and another two underscores (__) to give it a nice tree-like look.

jondro
A: 
<?PHP
function GetCats($id='0',$sublev='0',$vname='C_Parent')
{
 $DQ = new MySQLTable;
 $DQ -> TblName = 'cat_categories';
 $WHERE[$vname]['=']=$id;
 $res = $DQ -> Select('C_ID,C_Name',$WHERE,'C_ID');
 if (mysql_num_rows($res)>0)
 {
  while($row = mysql_fetch_assoc($res))
  {
    $ss='';
   if($sublev!=='0')
   {
    for($i=0;$i<=$sublev*10;$i++)
    {
     $ss.='&nbsp;';
    }
    $ss.='|';
    for($i=0;$i<=$sublev;$i++)
    {
     $ss.='-';
    }
     $ss.='&gt;&gt;';
   }
   $sel_s = '';
   if(IsSet($_POST['C_Parent']))
   {
    if($row['C_ID']==$_POST['C_Parent'])
    {
     $sel_s = ' selected';
    }
   } elseif (IsSet($_POST['I_Parent'])) {
    if($row['C_ID']==$_POST['I_Parent'])
    {
     $sel_s = ' selected';
    }
   } else {
    $sel_s = '';
   }
   Echo "<option value=\"".$row['C_ID']."\" ".$sel_s.">".$ss.$row['C_Name']."</option>\r\n";
   GetCats($row['C_ID'],$sublev+1);
  }
 }
}


     Echo "<select name=\"C_Parent\">\r\n";
     Echo "<option value=\"0\">...</option>\r\n";
     GetCats();
     Echo "</select>";
     ?>

Something like this.
But here was my own MySQL class. The query is: SELECT C_ID,C_Name WHERE C_Parent=$id ORDER BY C_ID where $id - php variable (current parent cat).
And there are $_POST variables if error submit to store selected.
This is not the most effective way to di it bcoz many queries. More effectively is to get all data to array to work with.

GOsha