whhhhaaaazzzzuuuuuppppppp
New member
I have this statement to pull back all information from my database.
Select * from news;
Within those results I want to say how many comments are held on each news item, but I'm not sure how. At the moment I manage to get the total number of comments held for ALL news entries, but not for each specific news update.
How do I pull back all news updates, then get the correct number of comments from the comments table for each?
$sql = "SELECT COUNT(*) FROM comments";
$result2 = mysql_query($sql);
$num = mysql_result($result2, 0);
$result = mysql_query("SELECT * FROM news ORDER BY Date desc");
while($row = mysql_fetch_array($result))
{
echo "<div id=\"news-feed\"><div id=\"news-title\">" . $row['Title'] . "<br /></div>";
echo "<div id=\"news-date\">" . $row['Date'] . "<br /><br /></div>";
echo "<div id=\"news-story\">" . $row['Story'] . "<br /></div>";
if (isset($_SESSION['Id'])) {
echo '<div id="comment-links">' . $num . ' comments' . '</div>';
echo '<a href="comment.php?news-id=' . $row['Id'] . '" id="comment-links">Leave a comment </a> ';
}
echo "</div>";
}
Select * from news;
Within those results I want to say how many comments are held on each news item, but I'm not sure how. At the moment I manage to get the total number of comments held for ALL news entries, but not for each specific news update.
How do I pull back all news updates, then get the correct number of comments from the comments table for each?
$sql = "SELECT COUNT(*) FROM comments";
$result2 = mysql_query($sql);
$num = mysql_result($result2, 0);
$result = mysql_query("SELECT * FROM news ORDER BY Date desc");
while($row = mysql_fetch_array($result))
{
echo "<div id=\"news-feed\"><div id=\"news-title\">" . $row['Title'] . "<br /></div>";
echo "<div id=\"news-date\">" . $row['Date'] . "<br /><br /></div>";
echo "<div id=\"news-story\">" . $row['Story'] . "<br /></div>";
if (isset($_SESSION['Id'])) {
echo '<div id="comment-links">' . $num . ' comments' . '</div>';
echo '<a href="comment.php?news-id=' . $row['Id'] . '" id="comment-links">Leave a comment </a> ';
}
echo "</div>";
}