Hi,
I've got a number of categories, some with subcategories, some not, that I'm trying to display schematically. The problem is the subcat depth is variable, so I find the problem of needing 5 while loops to get data for one category, and 2 in another. I need a way to keep reading from the db as long as there are subcats, then stop and go to the next "parent" category, and do the whole thing over again.
Trouble is, I can't figure out how to do this without getting stuck in the situation where I need a while loop for each iteration, which then limits subcat depth.
Clearly, I'm not grasping the logic of the situation properly, and I'm wondering if anyone can help.
Thanks,
The table looks like this:
CREATE TABLE znews_cats (
ID int(11) NOT NULL auto_increment,
Cat_Name varchar(128) NOT NULL,
Cat_Parent int(11) NOT NULL,
PRIMARY KEY (ID),
UNIQUE ID (ID)
);
Here's where I'm at with the code, just to get the gist of things:
$get_cats = mysql_query("SELECT * FROM $cats_table WHERE Cat_Parent = 1") or die("Error:".mysql_error());
$num_of_subs = mysql_num_rows($get_cats);
while ($row = mysql_fetch_array($get_cats))
{
echo $row["Cat_Name"];
echo "<br>";
$this_parent = $row["ID"];
$these_cats = mysql_query("SELECT * FROM $cats_table WHERE Cat_Parent = $this_parent") or die("Error:".mysql_error());
$num_of_these_subs = mysql_num_rows($these_cats);
if ($num_of_these_subs > 0) {
$more_subs = True;
while ($more_subs) {
echo "<br> has subcats<br>";
while ($newrow = mysql_fetch_array($these_cats)) {
echo "<blockquote>".$newrow["Cat_Name"];
echo "</blockquote>";
}
$new_parent = $newrow["ID"];
$more_cats = mysql_query("SELECT * FROM $cats_table WHERE Cat_Parent = $this_parent") or die("Error:".mysql_error());
$num_of_subs = mysql_num_rows($more_cats);
}
}