【问题标题】:Tree to generate for jsTree为 jsTree 生成的树
【发布时间】:2010-03-05 11:25:00
【问题描述】:
function createJsonTree($array, $currentParent, $currLevel = 0, $prevLevel = -1) {

 foreach ($array as $categoryId => $category) {

  if ($currentParent == $category['parent']) {      

   if ($currLevel > $prevLevel) $output .= ' , "children":[ '; 

   if ($currLevel == $prevLevel) $output .= " }, ";

    $output .= '{ "data" :'.'"'.$category['menu_title'].'"';

   if ($currLevel > $prevLevel) { $prevLevel = $currLevel; }

   $currLevel++; 

    $output .= self::createJsonTree($array, $category['id'], $currLevel, $prevLevel);

    $currLevel--;      
  } 

 }

 if ($currLevel == $prevLevel) $output .= " }] ";
 return $output;
}

【问题讨论】:

  • 您需要更详细地解释您的问题。发布标题和代码墙并不能让我们轻松为您提供帮助。你想做什么,你已经做到了什么,你现在做什么和你想做的不一样?

标签: php jstree


【解决方案1】:

json_encode 不能为此工作有什么原因吗?

http://us3.php.net/json_encode

【讨论】:

    【解决方案2】:

    你可以像这样生成一棵树:

    function createTree()
    {
        $sql = "SELECT id, parent_id, title FROM category order by id ";
        $filas = UtilConexion::$pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC);
        $arbol = '{"data":[{"attr":{"id":"000"},"data":"Nodo raíz","state":"open"' .
        $this->createJsonTree($filas, null) . '}]}';
        return $arbol;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-16
      • 2012-04-18
      • 1970-01-01
      • 1970-01-01
      • 2021-12-23
      相关资源
      最近更新 更多