Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /home/purvgwtw/public_html/tutorials/common.php on line 7
PHP Display Category Subcategory Tree View
Sponsered Links
Categories
Sponsered Links

PHP Display Category Subcategory Tree View

This example illustrates how to display category sub-category in tree view.

<?php
    print "<select name='catId' class='required'>";
    print "<option value=''>Select Category</option>";
    $selcat = "SELECT * from category order by name ASC";
    $selcat2 = mysql_query($selcat) or die("Could not select category");
    traverse(0, 0, $selcat2);
    print "</select><br>";
   
    function traverse($root, $depth, $sql) {
        $row=0;       
        while ($acat = mysql_fetch_array($sql)) {
            if ($acat['parentID'] == $root) {
                print "<option value='" . $acat['id'] . "'>";
                $j=0;
                while ($j<$depth) {    
                    print "&nbsp;&nbsp;&nbsp;&nbsp;";
                    $j++;
                }
                if($depth>0){
                    print ">>&nbsp;";
                }
                print $acat['name'] . "</option>";
                mysql_data_seek($sql,0);
                traverse($acat['id'], $depth+1,$sql);
            }
            $row++;
            mysql_data_seek($sql,$row);
        }
    }
?>

Sponsered Links
Latest Updates
 
All Content of this site is for learning only. We do not warrant the correctness of its content. The risk from using it lies entirely with the user. While using this site, you agree to have read and accepted our terms of use and privacy policy.
Copyright © 2014 PURVACOMPUTERS.COM All Right Reserved