Hello everyone. I know this is my first post but I have been at this
for awhile now and cannot seem to get it to work properly. This
is a 2 part question the first part is of the most important however.
Any help would be greatly appreciated it.
Part 1:
I have a table setup with catagories and subcats like so:
id main_id cat_name
1 0 Main
2 1 Catagory 1
3 1 Catagory 2
4 1 Catagory 3
5 2 Cat 1 Sub-Cat 1
6 2 Cat 1 Sub Cat-2
7 5 Cat 1 Sub-Cat 1 Sub-Cat
and so on up to three total sub-cats, could be as little as 1
sub-cat.
Now what I am trying to do in this part is if there are no other
sub-cats I would like to go to query a different table and give
its output. This is for a classified ads system. If we get to the last
sub-cat I will query the ads table and show the ads list. Here is
what I have for displaying the sub-cats for each catagory:
<?php
// we need to print the proper catagories now
$alternate = "1";
$query = "select * from $tblcats where main_id='$catid';";
$results = db_query($db, $dbhost, $dbuser, $dbpass, $query);
while($row = mysql_fetch_array($results)){
// lets print the sub category for this one
$mainid = $row['id'];
if ($alternate == "1") {
echo "<tr>";
}
if($numdays){
echo "<td valign=\"top\"><img src=\"$catimg/square.gif\" border=\"0\" align=\"absmiddle\"> <a href=\"viewsubcats.php?catid=$row[id]\">$row[cat_name]</a><br>";
}else{
echo "<td valign=\"top\"><img src=\"$catimg/square.gif\" border=\"0\" align=\"absmiddle\"> <a href=\"viewsubcats.php?catid=$row[id]\">$row[cat_name]</a><br>";
}
if ($alternate == "1") {
echo "</td>\n";
$alternate = "2";
}else {
echo "</td></tr>\n";
$alternate = "1";
}
$count++;
}
?>
If anyone can see how I can make it where it will stop looking for
catagories and go onto a different query I would greatly appreciate it.
Part 2:
What I am trying to do here is no matter how deep the sub-cats
go I can get them all without haveing to do a different loop for
each. This way there can be unlimited sub-cats like so:
Cat 1
-- Sub Cat 1
---- Sub-Sub Cat 1
------ Sub-Sub-Sub Cat 1
and so on and so on. This would go into a select drop down box. Here is the code I am currently using:
<select name="catid" size="1">
<option value="0" SELECTED>-- Select Category --</option>
<?php
$alternate = "1";
$catquery = "select * from $tblcats where main_id='1';";
$catresults = db_query($db, $dbhost, $dbuser, $dbpass, $catquery);
while($catrow = mysql_fetch_array($catresults)){
// lets print the sub category for this one
$catmainid = $catrow['id'];
echo "<option value=\"$catrow[id]\"><b>$catrow[cat_name]</b></option>\n";
$catsubquery = "select * from $tblcats where main_id='$catmainid' ORDER by cat_name ASC;";
$catsubresult = db_query($db, $dbhost, $dbuser, $dbpass, $catsubquery);
while($catsubinfo = mysql_fetch_array($catsubresult)) {
$catsubid = $catsubinfo['id'];
echo "<option value=\"$catsubinfo[id]\"> >>$catsubinfo[cat_name]</option>\n";
if($catsubid) {
$catsub2query = "select * from $tblcats where main_id='$catsubid' ORDER by cat_name ASC;";
$catsub2result = db_query($db, $dbhost, $dbuser, $dbpass, $catsub2query);
while($catsub2info = mysql_fetch_array($catsub2result)) {
$otcat = $catsub2info['id'];
echo "<option value=\"$catsub2info[id]\"> >>$catsub2info[cat_name]</option>\n";
if($otcat) {
$catsub3query = "select * from $tblcats where main_id='$otcat' ORDER by cat_name ASC;";
$catsub3result = db_query($db, $dbhost, $dbuser, $dbpass, $catsub3query);
while($catsub3info = mysql_fetch_array($catsub3result)) {
echo "<option value=\"$catsub3info[id]\"> >>$catsub3info[cat_name]</option>\n";
}
}
}
}
}
}
?></select>
Again any help would be greatly appreciated. Thanks in advance.
Regards,
Ray