【问题标题】:How to echo a tree select option list PHP如何回显一个树选择选项列表 PHP
【发布时间】:2013-05-11 17:36:02
【问题描述】:

你好 5 天我真的一直在搜索和尝试,但我想我不能排列树或者我真的不明白如何用树格式回显选择框

<option>Workstations</option>
<option>--Macs</option>
<option>--Windows</option>
<option>Software</option>
<option>--Image editing</option>
<option>----Pro</option>
<option>----Semi pro</option>

我的数据库结构是

标识 |父ID |名字

我已经非常接近了,例如,我只是缺少一个 3er 级别的符号,等等。

https://stackoverflow.com/questions/16474767/asign-symbol-to-subcategory-inside-subcategory-in-selectbox-php

编辑

你如何从我的数据库结构中构建一个这样的数组,我正在学习,我没有学习数组

$datas = array(
    array('id' => 1, 'parent' => 0, 'name' => 'Page 1'),
    array('id' => 2, 'parent' => 1, 'name' => 'Page 1.1'),
    array('id' => 3, 'parent' => 2, 'name' => 'Page 1.1.1'),
    array('id' => 4, 'parent' => 3, 'name' => 'Page 1.1.1.1'),
    array('id' => 5, 'parent' => 3, 'name' => 'Page 1.1.1.2'),
    array('id' => 6, 'parent' => 1, 'name' => 'Page 1.2'),
    array('id' => 7, 'parent' => 6, 'name' => 'Page 1.2.1'),
    array('id' => 8, 'parent' => 0, 'name' => 'Page 2'),
    array('id' => 9, 'parent' => 0, 'name' => 'Page 3'),
    array('id' => 10, 'parent' => 9, 'name' => 'Page 3.1'),
    array('id' => 11, 'parent' => 9, 'name' => 'Page 3.2'),
    array('id' => 12, 'parent' => 11, 'name' => 'Page 3.2.1'),
    );

这个数组会让这段代码工作,=(,我认为

<?PHP 

$datas = mysql_query("SELECT id, parent_id, name FROM category");

function generatePageTree($datas, $depth = 0, $parent = 0){
    if($depth > 1000) return ''; // Make sure not to have an endless recursion
    $tree = '';
    for($i=0, $ni=count($datas); $i < $ni; $i++){
        if($datas[$i]['parent'] == $parent){
            $tree .= str_repeat('-', $depth);
            $tree .= $datas[$i]['name'] . '<br/>';
            $tree .= generatePageTree($datas, $depth+1, $datas[$i]['id']);
        }
    }
    return $tree;
}

echo(generatePageTree($datas));


?>

【问题讨论】:

    标签: php sql select option


    【解决方案1】:

    我会使用一些 for 循环和递归函数来做到这一点。它所做的几乎就是从您的查询中获取每个结果,并在结果中搜索孩子。使用获取所有行的 id 和 parent_id 的查询。您还需要一个将结果转换为二维数组的函数,其中第一层是行,然后列即$result[0]['ID']; 将是第一行的 id。

    您可以使用类似的查询

    SELECT id, parent_id, name from YOUR_TABLE_HERE;
    
      $num=count($results);
      for($i=0;$i<$num;$i++) {
           $id=$results[$i]['id'];
           if($results[$i]['parent_id']===null) {
                 echo "<option>".$results[$i]['name']."</option>";
            }
           for($j=0;$j<$num;$j++) {
               if($results[$j]['parent_id']==$id) {
                    search($results[$j][$id],$results,"-");
                }
           }
        }
     //the level_delim is just the string that signifies the level i.e. ---
      function search($id, $results,$level_delim) {
         $num=count($results);
         for($i=0;$i<$num;$i++) {
               if($results[$i]['parent_id']==$id) {
                    echo "<option>".$level_delim."-".$results[$i]['name']."</option>"
                    search($results[$i]['id],$results,$level_delim."-");
                }
          }
     }
      function allResults(mysqli_result $result) {
       $array=array();
       for($row=0;$row<mysqli_num_rows($result);$row++) {       //get all the rows and gett array
           $array[$row]=  mysqli_fetch_assoc($result);
       }
       return $array;
    }
    

    【讨论】:

    • 谢谢你! ,正如我所说,我在设置查询时遇到了麻烦,原因可能是我做错了什么。这就是为什么我把我的数据库结构,如你所见..如果你能给出一个查询你给我的代码的例子,那就太好了。
    • 真的吗???用那个简单的查询?? jaja 也许这是我在其他帖子中的问题,我看到了非常复杂的查询,所以我没有理解这一点.. 这非常清楚,实际上.. 我会马上测试
    • 解析错误:语法错误,意外的“搜索”(T_STRING),在这部分搜索中需要“,”($results[$i]['id']
    • 这有点可疑,没有冒犯。假设 id 7 的 parent_id 为 5。当 $id=5 时,它会调用 search(...) 你输入 $results[$j][$id],它不存在。你的意思可能是 $results[$j]['id'],所以你调用 search(7,result...)。搜索中的 if 语句永远不会为真,因为没有其他元素具有 parent_id =7,因此不会发生任何事情。还是我弄错了?
    • 给我们你的答案!! :D 很高兴看到你会做什么。 $results[$j]['id'] yes send 致命错误:第 32 行 /Applications/MAMP/htdocs/Catalog/admin/regproducto.php 中允许的内存大小为 33554432 字节已用尽(试图分配 7205 字节)跨度>
    猜你喜欢
    • 2014-06-20
    • 1970-01-01
    • 1970-01-01
    • 2021-03-14
    • 2012-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多