H
H
HeX-20002014-06-14 19:26:25
PHP
HeX-2000, 2014-06-14 19:26:25

Draw menu recursively php mysql JSON

Good time.
Available Od

id  name   parenid
1   Server   0
2   Client   0
4   PHP      1
5   HTML     2
6   Css      5

Menu selection in php
PHP class 

class Menu{

private $items=array();


  function getMenu(){
    $sql="select * from category " ;
    $ret=mysql_query($sql) ;
    while($row=mysql_fetch_assoc($ret)){
      $q[$row['id']]=$row;
    }
    return $q;
  }


  function mapTree($dataset) {

    $tree = array();

    foreach ($dataset as $id=>$node) {
      if ($node['parentid']==0) {
          $tree[$node['id']]['root'] = $node;
      }else{
      $tree[$node['parentid']]['childs'][$id] = $node;
    }
    }
    return $tree;

}

$menus=new Menu();

echo json_encode($menus->mapTree($menus->getMenu()));

The question is, how is it possible to draw a tree menu on the client using recursion?
$(function(){
    $.getJSON('main.php?j=get', function(json) {
      rec(json);
    });
});    

function rec(obj){
   if(obj instanceof Object){
     list='<ul>';
      $.each(obj,function(k,v){
      if(v['root']){
        list+='<li>'+v['root'].cat_name+'</li>';
        
      }
      
      if(v['childs']){
        
        $.each(v['childs'],function(f,vv){
          list+='<li>'vv.cat_name+'</li>';
        });
      }
      $('.menu').html(list);
      rec(v['childs']);
      list+='</ul>'
      
    });
      
  }
}

Everything would be nothing but only the main menu or submenu is displayed.
How can such an idea be realized?
PS
Thank you all for your time.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikita Markelov, 2014-06-14
@Entii

mysql_query

NOOOOOOO

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question