php - 如何使用 PHP/SQL 构建树形 View ?

标签 php sql treeview hierarchical-data

最好的方法是什么:

  1. 使用单个查询从数据库获取数据
  2. 循环构建结果,例如嵌套的无序列表

我的表有 idnameparent_id 列。


这是我上一个答案的更新,其中包含一个为每个 ul 提供嵌套“级别”类的计数器,以及一些注释。

任何人都可以建议如何调整它以使用表行,无需嵌套,但使用某种用于 css/js Hook 的类编号层次结构吗?

<?

//
// Get the data
//
include_once("inc/config.php");

$query = "SELECT c.* 
          FROM categories AS c
          ORDER BY c.id
          LIMIT 1000";          

$result = pg_query($db, $query);

//
// Load all the results into the row array
//
while ($row = pg_fetch_array($result, NULL, PGSQL_ASSOC))
{
  //
  // Wrap the row array in a parent array, using the id as they key
  // Load the row values into the new parent array
  //
  $categories[$row['id']] = array(
    'id' => $row['id'], 
    'description' => $row['description'], 
    'parent_id' => $row['parent_id']
  );
}


// print '<pre>';
// print_r($category_array);

// ----------------------------------------------------------------

//
// Create a function to generate a nested view of an array (looping through each array item)
// From: http://68kb.googlecode.com/svn-history/r172/trunk/upload/includes/application/controllers/admin/utility.php
//
function generate_tree_list($array, $parent = 0, $level = 0)
{

  //
  // Reset the flag each time the function is called
  //
  $has_children = false;

  //
  // Loop through each item of the list array
  //
  foreach($array as $key => $value)
  {
    //
    // For the first run, get the first item with a parent_id of 0 (= root category)
    // (or whatever id is passed to the function)
    //
    // For every subsequent run, look for items with a parent_id matching the current item's key (id)
    // (eg. get all items with a parent_id of 2)
    //
    // This will return false (stop) when it find no more matching items/children
    //
    // If this array item's parent_id value is the same as that passed to the function
    // eg. [parent_id] => 0   == $parent = 0 (true)
    // eg. [parent_id] => 20  == $parent = 0 (false)
    //
    if ($value['parent_id'] == $parent) 
    {                   

      //
      // Only print the wrapper ('<ul>') if this is the first child (otherwise just print the item)      
      // Will be false each time the function is called again
      //
      if ($has_children === false)
      {
        //
        // Switch the flag, start the list wrapper, increase the level count
        //
        $has_children = true;  

        echo '<ul class="level-' . $level . '">';

        $level++;
      }

      //
      // Print the list item
      //
      echo '<li><a href="?id=' . $value['id'] . '">' . $value['description'] . '</a>';

      //
      // Repeat function, using the current item's key (id) as the parent_id argument
      // Gives us a nested list of subcategories
      //
      generate_tree_list($array, $key, $level); 

      //
      // Close the item
      //
      echo '</li>';


    }

  }

  //
  // If we opened the wrapper above, close it.
  //
  if ($has_children === true) echo '</ul>';


}

// ----------------------------------------------------------------

//
// generate list
//
generate_tree_list($categories);


?>

最佳答案

function generate_list($array,$parent,$level)
{

  foreach ($array as $value)
  {
    $has_children=false;

    if ($value['parent_id']==$parent)
    {

      if ($has_children==false)
      {
        $has_children=true;
        echo '<ul>';
      }

      echo '<li>'.$value['member_name'].' -- '.$value['id'].' -- '.$value['parent_id'];

      generate_list($array,$value['id'],$level);

      echo '</li>';
    }

    if ($has_children==true) echo '</ul>';

    echo $value['parent_id'];
  }

}

关于php - 如何使用 PHP/SQL 构建树形 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/333735/

相关文章:

php - 我们在我们的服务器上看到 "corrupted"opcache。知道如何解决吗?

sql - 将值从 SQL Server 导出到 txt 文件

mysql - 执行关联的正确方法

javafx 8 : How to update TreeCell in TreeView on SlectedItemProperty Change

php - 如何在 VS Code 中将 PHP 代码花括号自动格式化为新行

php - 使用php从html页面中提取图像url

php - 访问 json 对象中的数字属性

mysql - 将行交换为列

c# - 如何动态填充 TreeView (C#)

asp.net-mvc-4 - 如何获得 child 的 child ? Umbraco CMS