I have this table structure
sites
id | name | etc
1 | test | etc
2 | test2 | etc
3 | test3 | etc
comments
id | site_id | comment
1 | 2 | test comment
2 | 3 | test2 comment
3 | 2 | test3 comment
I want make a Select to list all sites eg.
test2
this is a test description
this is a test link
comments NR (2)
test3
this is a test description
this is a test link
comments NR (1)
I have this code to display the sites. Can anybody tell me how i can count the rows of the comment table?
$subcat_id = $_GET["id"];
$stmt = $handler->prepare("SELECT *
FROM sites sites
LEFT JOIN comments comments ON ( sites.id = comments.site_id )
WHERE sites.subcat_id = '$subcat_id' AND sites.status = 1
GROUP BY sites.id
ORDER BY RAND()");
$stmt->execute();
$no=$stmt->rowCount();
while($row = $stmt->fetch(PDO::FETCH_OBJ)){
$sites[]=$row;
}
$smarty->assign('site',$sites);
$smarty->assign('anzahl',$no);
$smarty->display('subcat.tpl');
Rewrite your sql slightly:
SELECT *
FROM sites sites
LEFT JOIN comments comments ON ( sites.id = comments.site_id )
WHERE sites.subcat_id = '$subcat_id' AND sites.status = 1
ORDER BY sites.id
Now you know you will have consecutive comments for each site and you can check in php if there is a new site for which you should print out a header.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments