Welcome Guest, Not a member yet? Register   Sign In
Get recursive menu from database
#2

[eluser]Dam1an[/eluser]
Although the code below isn't what you asked for, its some old code I had which done a nested drop down list of categories, so same concept.
Its essentially a recursive call starting with the top level categories, then searching for categories which have that as its parent etc

If you can't make sense of it, I'll adopt it to your situation in the morning, but I'm going bed now

Code:
// Creates a list of all the categories, nested within each other, used when
// adding products, cetegories etc.  Use the folloing code to add the drop down
// list into a page
/*
<?php
                print "<select name='parent_id'>";
                print "<option value='0'> </option>";
                $selcat="SELECT * from categories order by category_name ASC";
                $selcat2=mysql_query($selcat) or die("Could not select category"
);
                parentCategory(0,0,$selcat2);
                print "</select><br>";
                ?&gt;
*/
function parentCategory($root, $depth, $sql)
{
        $row=0;
        while ($acat = mysql_fetch_array($sql))
        {
                if ($acat['parent_id'] == $root)
                {
                        print "<option value='" . $acat['category_id'] . "'>";
                        $j=0;
                        while ($j<$depth)
                        {
                                print "&nbsp;&nbsp;&nbsp;";
                                $j++;
                        }
                        if($depth>0)
                        {
                                print " - ";
                        }
                        print $acat['category_name'] . "</option>";
                        mysql_data_seek($sql,0);
                        parentCategory($acat['category_id'], $depth+1,$sql);
                }
                $row++;
                mysql_data_seek($sql,$row);
        }
} // parentCategory

On a side note, my old (pre CI) code was hideous


Messages In This Thread
Get recursive menu from database - by El Forum - 04-21-2009, 03:56 PM
Get recursive menu from database - by El Forum - 04-21-2009, 04:09 PM
Get recursive menu from database - by El Forum - 04-21-2009, 04:22 PM
Get recursive menu from database - by El Forum - 04-22-2009, 12:52 AM
Get recursive menu from database - by El Forum - 04-22-2009, 07:32 AM
Get recursive menu from database - by El Forum - 04-26-2009, 07:58 AM
Get recursive menu from database - by El Forum - 04-26-2009, 12:43 PM
Get recursive menu from database - by El Forum - 04-26-2009, 01:38 PM
Get recursive menu from database - by El Forum - 04-26-2009, 03:09 PM
Get recursive menu from database - by El Forum - 04-26-2009, 03:21 PM
Get recursive menu from database - by El Forum - 04-26-2009, 04:05 PM
Get recursive menu from database - by El Forum - 10-25-2010, 04:23 AM



Theme © iAndrew 2016 - Forum software by © MyBB