Jump to content

outputting blog comments for each blog post


tim967

Recommended Posts

hey, i need some help if possible.

 

I basicly have 2 tables, one has the blog posts and one has the blog comments. I'm trying to display the latest 5 blog posts (which works) but underneath each of the blog posts i need to display about 3 of the latest comments for that post but it isn't showing any.

 

any ideas?

 

here's my code:

<?
session_start();
$myusername = $_SESSION['myusername'];

$db_host = 'localhost';
$db_user = 'HIDDEN';
$db_pwd = 'HIDDEN';

$database = 'tjwebsol_dev';
$table  = 'posts';
$table2 = 'blog_comments';
$tab = '&#38;#38;#160;&#38;#38;#160;&#38;#38;#160;&#38;#38;#160;&#38;#38;#160;&#38;#38;#160;&#38;#38;#160;&#38;#38;#160;&#38;#38;#160;&#38;#38;#160;&#38;#38;#160;&#38;#38;#160;';
if (!mysql_connect($db_host, $db_user, $db_pwd))
    die("Can't connect to database");

if (!mysql_select_db($database))
    die("Can't select database");

// sending query
$result = mysql_query("SELECT * FROM {$table} ORDER BY blog_id DESC LIMIT 5");
if (!$result) {
    die("Query to show fields from table failed");
}


$sql = mysql_query("SELECT * FROM {$table2} ORDER BY comment_id ASC LIMIT 5 ");

$fields_num = mysql_num_fields($result);
$fields_num2 = mysql_num_fields($sql);

while($row2 = mysql_fetch_assoc($sql))
{
$blog_comments    = $row2['comment_content'];
$date_commented   = $row2['date_commented'];
$time_commented   = $row2['time_commented'];
$blog_username2   = $row2['username'];
$blogid           = $row2['blogid'];
$comment_id       = $row2['comment_id'];
}

while($row = mysql_fetch_assoc($result))
{
$blog_title      = $row['title'];
$blog_content    = $row['content'];
$blog_username   = $row['username'];
$blog_date       = $row['date_posted'];
$blog_time       = $row['time_posted'];
$blog_id         = $row['blog_id'];
echo "<hr>";
echo "<div class='post'>";
echo "<h2 class='title'><a href='#'>$blog_title</h2></a>";
echo "<p class='byline'>posted by <b>$blog_username</b> on <b>$blog_date</b> ($blog_time)</p>";
echo "<br>";
echo "<div class='entry'>$blog_content</div><br>";
if ($blogid == $blog_id)
{
echo "<b>User Posted Comments:</b><br><br>";
echo "$tab<b>$comment_id</b>.  $blog_comments";
echo "<br><br>";
echo "Commented posted by <b>$blog_username2</b> on <b>$date_commented ($time_commented)</b>";
echo "<br><br><br>";
}
if (!isset($_SESSION['logged']) 
   	 || $_SESSION['logged'] !== true) {

	   // not logged in, move to login page
	   echo "<b><a href='login.php'>Login</a></b> to post your comments!";
}
echo "<br><br><br>";
include('blognewcomment.php');
echo "";
echo "</div>";

}
mysql_free_result($result);

?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.