Monday, 15 March 2010

3 Tables 1 Query for Nested Result - Mysql - Php -



3 Tables 1 Query for Nested Result - Mysql - Php -

i have 3 tables.

product table

id name 1 tv 2 phone

category table

id name 1 electronics 2 gift

relation table

pid cid 1 1 2 1 2 2

what want?

i want show products categories likes using sql query , php.

<h1>tv</h1> <span>electronics</span> <h1>phone</h1> <span>electronics</span> <span>gift</span>

what can do?

using bring together , foreach result below. not want that.

<h1>tv</h1> <span>electronics</span> <h1>phone</h1> <span>electronics</span> <h1>phone</h1> <span>gift</span>

what efficient query , loop method reach result?

if order product can this

while($row=$result->fetch_object()){ $product=$row->product; $category=$row->category; if($product==$pcheck){ echo "<span>$category</span>"; } else{ echo "<h1>$product</h1><span>$category</span>"; } $pcheck=$product; }

depending on info can this

while($row=$result->fetch_object()){ $x[$row->product][]="<span>$row->category</span>"; } foreach ($x $product=>$categories){ echo "<h1>$product</h1>".implode("",$categories); }

php mysql sql

No comments:

Post a Comment