开发者

Tree to generate for jsTree

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; }

   $cur开发者_如何转开发rLevel++; 

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

    $currLevel--;      
  } 

 }

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


Is there a reason that json_encode won't work for this?

http://us3.php.net/json_encode


You can generate a tree like this:

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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜