mysql |嵌套设置为选择框

时间:2014-10-27 06:55:20

标签: php mysql nested

我有嵌套的set table(Joomla categories table):

id parent_id  lft rgt extension title
1  0          0   19  system    ROOT
...
12 1          3   6   com_hi    Category1
13 12         4   5   com_hi    Category2

如何将此数据转换为选择框?

<select>
   <option value="12">Category1</option>
   <option value="13">...Category2</option>
</select>  

2 个答案:

答案 0 :(得分:0)

  $query= mysql_query('select * from table');
   //select your table first

   echo "<select>";
 // and in your while loop do this code.. 
   while($row = mysql_fetch_array($query))
    {
        echo "<option value=".$row['id']. " > ". $row['title'] ."</option>";
    }
   echo "</select>";

答案 1 :(得分:-1)

这是一个正确的答案:

$sql = ' SELECT node.id, CONCAT( REPEAT( "...", (COUNT(parent.title) - 2)), node.title) AS title
                        FROM    `#__categories` AS node,
                                `#__categories` AS parent
                        WHERE node.lft BETWEEN parent.lft AND parent.rgt
                        GROUP BY node.title
                        ORDER BY node.lft';