...

View Full Version : while .... Please help



HormonX
08-02-2002, 05:50 PM
here is my question .... this might be a bit confusing so please be patient :)

what am doing is using while loop to display data from the database, in this case they are categories. the code for this is below...

<?
include ('connect.php');
$query = "SELECT DISTINCT cat FROM product";
$result = mysql_query( $query, $link );

while($r=mysql_fetch_array($result))
{
$cat=$r['cat'];
printf ("<span class='body'><b> $cat </b><br><br></span>");
}
mysql_free_result($result);
mysql_close($link);
?>

this works fine ... it lists the categories in this fasion;

Category1

Category2

Category3 and so on ...

what i want to do is to list also subcategories in in this fasion;

Category1
SubCategory1-1, Subcategory1-2

Category2
SubCategory2-1, Subcategory2-2

Category3
SubCategory3-1, Subcategory3-2

Do i have to use another while loop to list the subcategories ?
if so where and how do i put there ?

Please help ... :)

thanx in advance .

HormonX

Íkii
08-03-2002, 08:58 AM
Where are the subcategories listed? - what is the table structure like in the db?

while($r=mysql_fetch_array($result)) {
$cat=$r['cat'];
printf ("<span class='body'><b> $cat </b><br><br></span>");

$result2 = "SELECT subcategorylist FROM `product` WHERE cat='$cat'";
while($s = mysql_fetch_array($result2)) {
$subcat = $s['subcat'];
echo '<span class="subcat">'.$subcat.'<br>';
}

}

does that help with getting you on track?

HormonX
08-03-2002, 04:16 PM
this helped a whole lot ... i have tried this before .. but i was making a bit of a mistake .. that's why it wasn't working right.

thanx againg for your help



:thumbsup:

Íkii
08-03-2002, 05:03 PM
No prob - am glad you found that helpful :D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum