Sunday, 15 September 2013

PHP : print grouped results in tables

PHP : print grouped results in tables

I have a resultset of products which is ordered by an $id. I wrote a code
which separates results with the same id into groups and prints them
separately.
CODE (without tables):
category='';
foreach ($result as $each) {
if($each['id'] != $category) {
$category = $each['id'];
echo "<b>{$each['title']}</b><br/>";
}
echo $each['product'].'<br/>';
}
OUTPUT (without tables):
**title1**
product1
product2
product3
**title2**
product4
product5
product6
Ok now to the problem. What i want is to get all the product into tables
(to include some more information). I want to separate these tables with
div's which include title.
Something like this:
<div> title here </div>
<table> all the products here</table>
<div> second title here </div>
<table> all the products here </table>
etc.
The only problem is that i dont know how / where to properly include the
ending tags </tr></table> to make the structure complete.
What i tried:
category='';
foreach ($result as $each) {
if($each['id'] != $category) {
$category = $each['id'];
echo "<div>{$each['title']}</div>
<table>
<tr>";
}
echo "<td>{$each['product']}</td>
<td>{$each['price']}</td>";
}
# how/where should I include the ending tags?

No comments:

Post a Comment