I am trying to print the category name and subcategory name out for each category and subcategory when returning rows from a database search. I can only get it to work for the first category and sub category. I know it is something with the way I'm going through my for loop, but I just can't figure it out. Am I checking for the field value correctly?
THANKS in advance!
<script language="php">
if($citysrch == "Go") {
include "conndb.php";
$srch = "SELECT cp.coupons_id, cp.coupons_business_name, cp.coupon_path,
c.city_name, c.city_town,
cg.cat_name,
sc.sub_name
FROM coupons cp, categories cg, city c, sub_categories sc
WHERE cp.city_id = $city
AND cp.city_id = c.city_id
AND cp.cat_id = cg.cat_id
AND cp.sub_id = sc.sub_id
ORDER BY cp.city_id,
cp.cat_id,
cp.sub_id,
cp.coupons_business_name
LIMIT 0, 30";
$query = mysql_query($srch) or die ("Could not execute search 4: $query. " . mysql_error());
$num=mysql_num_rows($query);
$category = 0;
$subcat = 0;
if ($num <=0){
echo "<font face=\"Arial\" color=\"#800000\" size=\"3\"><strong>There are currently no coupons for the city you selected. Please check back or try another city.</strong>";
return;
}
echo "<table border=\"0\" cellspacing=\"2\" style=\"border-collapse: collapse\" width=\"100%\" id=\"AutoNumber2\">";
for ($i=0; $i<$num; $i++){
$row = mysql_fetch_array($query);
if ($category !== cg.cat_id){
$category = cg.cat_id;
echo "<tr>";
echo "<td width=\"41%\"><strong><font face=\"Arial\" color=\"#800000\" size=\"2\">".stripslashes($row["cat_name"])."</td>";
echo "</tr>";
}
if ($subcat !== sc.sub_id){
$subcat = sc.sub_id;
echo "<tr>";
echo "<td width=\"41%\"><strong><font face=\"Arial\" color=\"#800000\" size=\"2\"> ".stripslashes($row["sub_name"])."</td>";
echo "</tr>";
}
echo "<tr>";
echo "<td width=\"70%\"><strong><font face=\"Arial\" color=\"#0000FF\" size=\"2\"> ".stripslashes($row["coupons_business_name"])."</td>";
echo "<td width=\"30%\"><a href=\"javascript:Start('".stripslashes($row["coupon_path"])."')\">Get Coupon</a></font></strong></td>";
echo "</tr>";
}
echo "</table>";
}
</script>