Ajax Search PHP through MySQL record


var searchRequest = null;

$(function () {
    var minlength = 3;

    $("#spedostech_search").keyup(function () {
        var that = this,
        value = $(this).val();

        if (value.length >= minlength ) {
            if (searchRequest != null) 
                searchRequest.abort();

            console.log( );
            // return;
            
            searchRequest = $.ajax({
                type: "GET",
                url: BASE_URL + "spedostech/search",
                data: {
                    'search_keyword' : value
                },
                dataType: "json",
                success: function(msg){
                    // var obj = JSON.stringify(msg);
                    console.log(msg);
                    //we need to check if the value is the same
                    if (value==$(that).val()) {
                        var container = $('<ul />');
                        $.each( msg, function( section, List ) {
                          var li = $('<li>Nalezeno v '+section+' (3)</li>');

                          // container.append('<li>Nalezeno v '+section+' (3)</li>');
                          // console.log( section + " : ");
                          var ul = $('<ul />');
                          $.each( List, function( key, item ) {
                            ul.append('<li><a href="#'+item.id+'" title="">'+item.name+'</a></li>');
                            // console.log("Name : " + item.name);
                          });
                          li.append(ul);
                          container.append(li);  
                        });

                    // console.log(container);
                        // $('#tt').html(container);
                        $('#search-whisperer').html(container);

                    //Receiving the result of search here
                    }
                }
            });
        }
    });
});



Make a menu as tree by associative array PHP

Make a menu as tree by associative array PHP

    function menu($arr) {
        echo "<ul>";
        foreach ($arr as $val) {

            if (!empty($val['children'])) {
                echo "<li>" . $val['name'];
                menu($val['children']);
                echo "</li>";
            } else {
                echo "<li>|</li>";
                echo "<li>-&gt;" . $val['name'] . "</li>";
            }
        }
        echo "</ul>";
    }

    $arr = array(
	  array('id'=&gt;100, 'parentid'=&gt;0, 'name'=&gt;'a'),
	  array('id'=&gt;101, 'parentid'=&gt;100, 'name'=&gt;'a'),
	  array('id'=&gt;102, 'parentid'=&gt;101, 'name'=&gt;'a'),
	  array('id'=&gt;103, 'parentid'=&gt;101, 'name'=&gt;'a'),
	);

	$new = array();
	foreach ($arr as $a){
	    $new[$a['parentid']][] = $a;
	}
	
	$tree = createTree($new, array($arr[0]));
	echo('<pre>');
	//print_r($tree);
	echo('</pre>');
	menu($tree);

	function createTree(&amp;$list, $parent){
	    $tree = array();
	    foreach ($parent as $k=&gt;$l){
	        if(isset($list[$l['id']])){
	            $l['children'] = createTree($list, $list[$l['id']]);
	        }
	        $tree[] = $l;
	    } 
	    return $tree;
	}