I need to display all the comments from the comments database table
where the $msg_id_fk = $msg_id from the messages table and I need it in a while loop

$comment_results = mysql_query("SELECT * FROM comments WHERE msg_id_fk='$msg_id' ORDER BY com_id ASC");
while ($row = mysql_fetch_array($comment_results)){
	
$msg_id_fk=$row['msg_id_fk'];
$comment=$row['comment'];
}

$message_results = mysql_query("SELECT * FROM messages WHERE user_on='$user' ORDER BY msg_id DESC");
while ($row = mysql_fetch_array($message_results)){

$msg_id=$row['msg_id'];
$msg=$row['message'];


$messages .='message ' . $message . ' <br />
comment ' .$comments. '';
}

Example:

___________________________________________________Message 1

Message $msg_id = 234

Comment $msg_id_fk = 234 $com_id = 1

Comment $msg_id_fk = 234 $com_id = 2

Comment $msg_id_fk = 234 $com_id = 3

Comment $msg_id_fk = 234 $com_id = 4

Comment $msg_id_fk = 234 $com_id = 5

Comment $msg_id_fk = 234 $com_id = 6

___________________________________________________Message 2

Message $msg_id = 235

Comment $msg_id_fk = 235 $com_id = 7

Comment $msg_id_fk = 235 $com_id = 8

Comment $msg_id_fk = 235 $com_id = 9

Comment $msg_id_fk = 235 $com_id = 10

Comment $msg_id_fk = 235 $com_id = 11

Comment $msg_id_fk = 235 $com_id = 12

___________________________________________________Message 3

Message $msg_id = 236

Comment $msg_id_fk = 236 $com_id = 13

Comment $msg_id_fk = 236 $com_id = 14

Comment $msg_id_fk = 236 $com_id = 15

Comment $msg_id_fk = 236 $com_id = 16

Comment $msg_id_fk = 236 $com_id = 17

Comment $msg_id_fk = 236 $com_id = 18

Recommended Answers

All 19 Replies

Try the following:

$r=mysql_query('SELECT * FROM `comments`, `messages` WHERE `comments`.`msg_id_fk` = `messages`.`msg_id`') or die(mysql_error());
while ($row = mysql_fetch_assoc($r)) {
echo $row['comment'];
echo '<br><br>';
echo $row['message'];
echo '<hr>';
}

I think that is what you mean but I haven't tested it for bugs.

Try the following:

$r=mysql_query('SELECT * FROM `comments`, `messages` WHERE `comments`.`msg_id_fk` = `messages`.`msg_id`') or die(mysql_error());
while ($row = mysql_fetch_assoc($r)) {
echo $row['comment'];
echo '<br><br>';
echo $row['message'];
echo '<hr>';
}

I think that is what you mean but I haven't tested it for bugs.

So this will display all the comments for each message

Check out the picture below

So this will display all the comments for each message

Yes but when just rereading your first post I found the code should be more like the following:

$r=mysql_query('SELECT * FROM `comments`, `messages` WHERE `comments`.`msg_id_fk` = `messages`.`msg_id` ORDER BY `comments`.`com_id`, `messages`.`msg_id`') or die(mysql_error());
while ($row = mysql_fetch_assoc($r)) {
echo $row['comment'];
echo '<br><br>';
echo $row['message'];
echo '<hr>';
}

I would suggest trying it and see if it works.

Ok it sort of works.
it display the message like:

test #33 message 1

test#33 comment 1
_________________________

test #33 message 1

test#33 comment 2

and i want it like


test #33 message 1

test#33 comment 1

test#33 comment 2

look at the image below

Then try the following:

$r=mysql_query('SELECT * FROM `comments`, `messages` WHERE `comments`.`msg_id_fk` = `messages`.`msg_id` ORDER BY `comments`.`com_id`, `messages`.`msg_id`') or die(mysql_error());
$messagedump='';
while ($row = mysql_fetch_assoc($r)) {
if ($messagedump!=$row['message']) {
$messagedump=$row['message'];
echo '<hr>';
echo $row['message'];
}
echo '<br><br>';
echo $row['comment'];
}

Then try the following:

$r=mysql_query('SELECT * FROM `comments`, `messages` WHERE `comments`.`msg_id_fk` = `messages`.`msg_id` ORDER BY `comments`.`com_id`, `messages`.`msg_id`') or die(mysql_error());
$messagedump='';
while ($row = mysql_fetch_assoc($r)) {
if ($messagedump!=$row['message']) {
$messagedump=$row['message'];
echo '<hr>';
echo $row['message'];
}
echo '<br><br>';
echo $row['comment'];
}

I can't get it to work

Are there any errors or could you explain what is happening so I can fix.

Are there any errors or could you explain what is happening so I can fix.

Do you want me to send you my whole code?

it works
but when i try adding it to my code it don't work

Do you want me to send you my whole code?

it works
but when i try adding it to my code it don't work

Then send a html template of what the result should look like (but with the echoed data) Then I will place in there the php code. Just mark in the template which $row variable goes where.

The comments

$id2=$row['com_id'];
$comment=$row['comment'];
$date2=$row['date'];
$usernameqw=$row['username'];

The message

$idt=$row['msg_id'];
$msg=$row['message'];
$useron=$row['username'];
$date=$row['date'];




<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td>useron</td>
    <td>msg</td>
  </tr>
  <tr>
    <td>date</td>
    <td>nbsp;</td>
  </tr>
</table></td>
  </tr>
  <tr>
    <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td>usernameqw</td>
    <td>comment</td>
  </tr>
  <tr>
    <td>date2</td>
    <td>&nbsp;</td>
  </tr>
</table></td>
  </tr>
</table>

Below is the code and you will need to rename the username and date columns in the comments table to _username and _date

<?php
//Insert mysql connections here

$r=mysql_query('SELECT * FROM `comments`, `messages` WHERE `comments`.`msg_id_fk` = `messages`.`msg_id` ORDER BY `comments`.`com_id`, `messages`.`msg_id`') or die(mysql_error());
$messagedump='';
while ($row = mysql_fetch_assoc($r)) {
if ($messagedump!=$row['message']) {

echo <<< HTML
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td>{$row['username']}</td>
    <td>{$row['message']}</td>
  </tr>
  <tr>
    <td>{$row['date']}</td>
    <td>nbsp;</td>
  </tr>
</table></td>
  </tr>
HTML;
}

echo <<< HTML
  <tr>
    <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td>{$row['_username']}</td>
    <td>{$row['comment']}</td>
  </tr>
  <tr>
    <td>{$row['_date']}</td>
    <td>&nbsp;</td>
  </tr>
</table></td>
  </tr>
</table>
HTML;

}
?>

Below is the code and you will need to rename the username and date columns in the comments table to _username and _date

<?php
//Insert mysql connections here

$r=mysql_query('SELECT * FROM `comments`, `messages` WHERE `comments`.`msg_id_fk` = `messages`.`msg_id` ORDER BY `comments`.`com_id`, `messages`.`msg_id`') or die(mysql_error());
$messagedump='';
while ($row = mysql_fetch_assoc($r)) {
if ($messagedump!=$row['message']) {

echo <<< HTML
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td>{$row['username']}</td>
    <td>{$row['message']}</td>
  </tr>
  <tr>
    <td>{$row['date']}</td>
    <td>nbsp;</td>
  </tr>
</table></td>
  </tr>
HTML;
}

echo <<< HTML
  <tr>
    <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td>{$row['_username']}</td>
    <td>{$row['comment']}</td>
  </tr>
  <tr>
    <td>{$row['_date']}</td>
    <td>&nbsp;</td>
  </tr>
</table></td>
  </tr>
</table>
HTML;

}
?>

The code works with no errors
but it do's not work for me

I need it to display all the messages for that user and inside ever message display every comment for that message
I'm gonna post my code so you can see what i'm talking about

<?php 
include("db.php");//Connect to message Database
include("tolink.php");



$result = mysql_query("SELECT * FROM comments WHERE msg_id_fk='$idt' ORDER BY com_id DESC");
while ($row = mysql_fetch_array($result)){
	
$id2=$row['com_id'];//Comment id
$comment=$row['comment'];//The Comment 
$date2=$row['_date'];//Comment date
$usernameqw=$row['_username'];//User who posted the comment


//Display all comments for message where msg_id_fk = $idt
$comments_view .=' <div class="comment_load" id="comment'.$id2.'" >
 <table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td valign="top" align="left" width="32px">'.$user_pic2post3.'</td>
<td valign="top">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td align="left" valign="top">&nbsp;<b>'.$usernameqw.'</b>&nbsp;'.$comment.'</td>
  </tr>
  <tr>
   <td align="left">&nbsp;<font style=" font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#606060;">'.$date2.'
    &nbsp;-&nbsp;</font><span><a href="javascript: void(0)" id="'.$id2.'" class="cdelete_update"><font style=" font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#333333;">Remove</font></a></span></td>
  </tr>
</table>
</td>
  </tr>
</table>
 </div>';
}


$results = mysql_query("SELECT * FROM messages WHERE user_on='$user2' ORDER BY msg_id DESC");
while ($row = mysql_fetch_array($results)){

$idt=$row['msg_id'];//Message id
$msg=$row['message'];//The Message
$useron=$row['username'];//This is the user where the message is posted on
$date=$row['date'];//Message date
$msg=toLink($msg);

    //Get the picture for the user where the message was posted
	$check_pic = "members/$useron/image01.png";
	$default_pic = "members/0/image01.jpg";
	if (file_exists($check_pic)) {
    $user_picpost = "<img src=\"$check_pic\" width=\"50px\"  alt=\"$user2\" title=\"$useron\"  />"; 
	} else {
	$user_picpost = "<img src=\"$default_pic\" width=\"50px\" alt=\"$user2\" title=\"$useron\"  />"; 
	}
	//Get the session user's picture
	$check_pic = "members/$username/image01.png";
	$default_pic = "members/0/image01.jpg";
	if (file_exists($check_pic)) {
    $user_pic2post = "<img src=\"$check_pic\" width=\"32px\"  alt=\"$username\" title=\"$username\" />"; 
	} else {
	$user_pic2post = "<img src=\"$default_pic\" width=\"32px\" alt=\"$username\" title=\"$username\" />"; 
	}
	//Get the user who posted the comment's picture
	$check_pic = "members/$usernameqw/image01.png";
	$default_pic = "members/0/image01.jpg";
	if (file_exists($check_pic)) {
    $user_pic2post3 = "<img src=\"$check_pic\" width=\"32px\"  alt=\"$usernameqw\" title=\"$usernameqw\" />"; 
	} else {
	$user_pic2post3 = "<img src=\"$default_pic\" width=\"32px\" alt=\"$usernameqw\" title=\"$usernameqw\" />"; 
	}

$msg = wordwrap($msg, 65, "\n", true);//Line break every 65 characters long

//Display the all the message's where user_on = $user2
$message_post .='
<li class="bar'.$idt.'"  onmouseover=\'document.getElementById("'.$idt.'").style.display="";\' onMouseout=\'document.getElementById("'.$idt.'").style.display="none";\'>
<div>
<div align="left" class="post_box">

<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td valign="top" width="60px">'.$user_picpost.'</td>
    <td valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td valign="top"><ahref="#"class="comment">'.$useron.'</a>'.$msg.'</td>
  </tr>
</table>
</td>
    <td valign="top" width="19px" align="right">
<div id="hide_edit_btn" ><span style="display:none;" class="'.$idt.'"  id="'.$idt.'"><a href="javascript: void(0)" id="'.$idt.'"  class="delete_update">'. (($useron == $username || $user2 == $username) ? "<div class=\"hover_delete\">&nbsp;</div>" : "") .'</a></span></div>
	</td>
  </tr>
</table>
</div>
<div id="expand_box">
<div id="expand_url"></div>
</div>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="65px">&nbsp;</td>
    <td align="left"><span class="feed_link">'.$date.'  
	'. (($username !=="") ? "&nbsp;-&nbsp;" : "") .'<a href="#" class="comment" id="'.$idt.'" title="Click here to comment">'. (($username !=="") ? "comment" : "") .'</a></span></td>
  </tr>
</table>

'.$comments_view.'

<div id="fullbox" class="fullbox'.$idt.'">
<div id="commentload'.$idt.'" ></div>
<div class="comment_box" id="c'.$idt.'">
<form method="post" action="" name="'.$idt.'">
<div class="comment_left">'.$user_pic2post.'</div>
<div class="comment_right"><textarea class="text_area" name="comment_value" id="textarea'.$idt.'" onKeyDown="limitText(this,100);" 
onKeyUp="limitText(this,100);">
Write a comment...</textarea><br />
<input type="hidden" name="username" id="'.$idt.'" value="'.$user2.'" /></div>
 <div class="clearer"> </div>
<div class="comment_left">&nbsp;</div>
<div class="comment_right2"><input type="submit" value=" Comment " class="comment_submit" id="'.$idt.'"/></div>
<div class="clearer"> </div>
</form>
</div>
</div>

<hr width="570px">
</div>
</li>';
}
?>
<ol class="timeline2">
<? echo"$message_post"; /*Display the message with all the comments for it*/  ?>
</ol>

I need it to display all the messages for that user and inside ever message display every comment for that message
I'm gonna post my code so you can see what i'm talking about

That's what my previous code does. It displays a message then below that all of the comments for that message then goes to the next message. So could you expand on why it doesn't work as the code I posted should do everything you asked in the above quote.

Perhaps try the following as simular as it might look.

<?php
//Insert mysql connections here

$r=mysql_query('SELECT * FROM `comments`, `messages` WHERE `comments`.`msg_id_fk` = `messages`.`msg_id` ORDER BY `comments`.`com_id`, `messages`.`msg_id`') or die(mysql_error());
$messagedump='';
while ($row = mysql_fetch_assoc($r)) {
if ($messagedump!=$row['message']) {
$messagedump=$row['message'];
echo <<< HTML
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td>{$row['username']}</td>
    <td>{$row['message']}</td>
  </tr>
  <tr>
    <td>{$row['date']}</td>
    <td>nbsp;</td>
  </tr>
</table></td>
  </tr>
HTML;
}

echo <<< HTML
  <tr>
    <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td>{$row['_username']}</td>
    <td>{$row['comment']}</td>
  </tr>
  <tr>
    <td>{$row['_date']}</td>
    <td>&nbsp;</td>
  </tr>
</table></td>
  </tr>
</table>
HTML;

}
?>

The bug was $messagedump wasn't being assigned a value.

Perhaps try the following as simular as it might look.

<?php
//Insert mysql connections here

$r=mysql_query('SELECT * FROM `comments`, `messages` WHERE `comments`.`msg_id_fk` = `messages`.`msg_id` ORDER BY `comments`.`com_id`, `messages`.`msg_id`') or die(mysql_error());
$messagedump='';
while ($row = mysql_fetch_assoc($r)) {
if ($messagedump!=$row['message']) {
$messagedump=$row['message'];
echo <<< HTML
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td>{$row['username']}</td>
    <td>{$row['message']}</td>
  </tr>
  <tr>
    <td>{$row['date']}</td>
    <td>nbsp;</td>
  </tr>
</table></td>
  </tr>
HTML;
}

echo <<< HTML
  <tr>
    <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td>{$row['_username']}</td>
    <td>{$row['comment']}</td>
  </tr>
  <tr>
    <td>{$row['_date']}</td>
    <td>&nbsp;</td>
  </tr>
</table></td>
  </tr>
</table>
HTML;

}
?>

The bug was $messagedump wasn't being assigned a value.

It works thank you very much.
If you want could you incorporate your code into mine without using heredoc.
I tried but it do's not display right.
Here is my current code:

<?php 
include_once "scripts/connect_to_mysql.php";
include("db.php");//Connect to Database
include("tolink.php");

//Insert mysql connections here

$r=mysql_query('SELECT * FROM comments, messages WHERE comments.msg_id_fk = messages.msg_id ORDER BY comments.com_id, messages.msg_id') or die(mysql_error());
$messagedump='';
while ($row = mysql_fetch_assoc($r)) {


$idt=$row['msg_id'];//Message id
$msg=$row['message'];//The Message
$useron=$row['username'];//This is the user where the message is posted on
$date=$row['date'];//Message date
//$msg=toLink($msg);
$id2=$row['com_id'];//Comment id
$comment=$row['comment'];//The Comment 
$date2=$row['_date'];//Comment date
$usernameqw=$row['_username'];//User who posted the comment


if ($messagedump!=$msg) {
	$messagedump=$msg;

echo '
<ol class="timeline2">
<li class="bar'.$idt.'"  onmouseover=\'document.getElementById("'.$idt.'").style.display="";\' onMouseout=\'document.getElementById("'.$idt.'").style.display="none";\'>
<div>
<div align="left" class="post_box">

<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td valign="top" width="60px">'.$user_picpost.'</td>
    <td valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td valign="top"><a href="#" class="comment"><font style=" font-family:Arial, Helvetica, sans-serif; font-size:14px; color:#333333;"><b>'.$useron.'</b></font></a>&nbsp;<font style=" font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#333333;">'.$msg.'</font></td>
  </tr>
</table>
</td>
    <td valign="top" width="19px" align="right">
<div id="hide_edit_btn" ><span style="display:none;" class="'.$idt.'"  id="'.$idt.'"><a href="javascript: void(0)" id="'.$idt.'" >'. (($useron == $username || $user2 == $username) ? "<div class=\"hover_delete\">&nbsp;</div>" : "") .'</a></span></div>
	</td>
  </tr>
</table>
</div>

<div id="expand_box">
<div id="expand_url"></div>
</div>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="65px">&nbsp;</td>
    <td align="left"><span class="feed_link"><font style=" font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#606060;">'.$date.'  
	</font>
	'. (($username !=="") ? "&nbsp;-&nbsp;" : "") .'<a href="#" class="comment" id="'.$idt.'" title="Click here to comment"><font style=" font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#333333;">'. (($username !=="") ? "comment" : "") .'</font></a></span></td>
  </tr>
</table>
';
}

echo '
<div class="comment_load" id="comment'.$id2.'" >
 <table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td valign="top" align="left" width="32px">'.$user_pic2post3.'</td>
<td valign="top">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td align="left" valign="top">&nbsp;<b>'.$usernameqw.'</b>&nbsp;'.$comment.'</td>
  </tr>
  <tr>
   <td align="left">&nbsp;<font style=" font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#606060;">'.$date2.'
    &nbsp;-&nbsp;</font><span><a href="javascript: void(0)" id="'.$id2.'" class="cdelete_update"><font style=" font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#333333;">Remove</font></a></span></td>
  </tr>
</table>
</td>
  </tr>
</table>
 </div>






<div id="fullbox" class="fullbox'.$idt.'">
<div id="commentload'.$idt.'" ></div>
<div class="comment_box" id="c'.$idt.'">
<form method="post" action="" name="'.$idt.'">
<div class="comment_left">'.$user_pic2post.'</div>
<div class="comment_right"><textarea class="text_area" name="comment_value" id="textarea'.$idt.'" onKeyDown="limitText(this,100);" 
onKeyUp="limitText(this,100);">
Write a comment...</textarea><br />
<input type="hidden" name="username" id="'.$idt.'" value="'.$user2.'" /></div>
 <div class="clearer"> </div>
<div class="comment_left">&nbsp;</div>
<div class="comment_right2"><input type="submit" value=" Comment " class="comment_submit" id="'.$idt.'"/></div>
<div class="clearer"> </div>
</form>
</div>
</div>

<hr width="570px">
</div>
</li>
</ol>
';

}
?>

If you want could you incorarate yor code into mine without heredoc.
I tried but it do's not display right.

I could but I would require a html template for the place the php code into. As for how my code works, in the if statement is the message then outside the if statement is the comments.

I could but I would require a html template for the place the php code into. As for how my code works, in the if statement is the message then outside the if statement is the comments.

Ok cool.

Here it is
The php go's in the ol tags

<?php 
include_once "scripts/connect_to_mysql.php";
include("db.php");//Connect to Database
include("tolink.php");

//Insert mysql connections here

$r=mysql_query('SELECT * FROM comments, messages WHERE comments.msg_id_fk = messages.msg_id ORDER BY comments.com_id, messages.msg_id') or die(mysql_error());
$messagedump='';
while ($row = mysql_fetch_assoc($r)) {


$idt=$row['msg_id'];//Message id
$msg=$row['message'];//The Message
$useron=$row['username'];//This is the user where the message is posted on
$date=$row['date'];//Message date
//$msg=toLink($msg);
$id2=$row['com_id'];//Comment id
$comment=$row['comment'];//The Comment 
$date2=$row['_date'];//Comment date
$usernameqw=$row['_username'];//User who posted the comment


if ($messagedump!=$msg) {
	$messagedump=$msg;

$display .='
<li class="bar'.$idt.'"  onmouseover=\'document.getElementById("'.$idt.'").style.display="";\' onMouseout=\'document.getElementById("'.$idt.'").style.display="none";\'>
<div>
<div align="left" class="post_box">

<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td valign="top" width="60px">'.$user_picpost.'</td>
    <td valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td valign="top"><a href="#" class="comment"><font style=" font-family:Arial, Helvetica, sans-serif; font-size:14px; color:#333333;"><b>'.$useron.'</b></font></a>&nbsp;<font style=" font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#333333;">'.$msg.'</font></td>
  </tr>
</table>
</td>
    <td valign="top" width="19px" align="right">
<div id="hide_edit_btn" ><span style="display:none;" class="'.$idt.'"  id="'.$idt.'"><a href="javascript: void(0)" id="'.$idt.'" >'. (($useron == $username || $user2 == $username) ? "<div class=\"hover_delete\">&nbsp;</div>" : "") .'</a></span></div>
	</td>
  </tr>
</table>
</div>

<div id="expand_box">
<div id="expand_url"></div>
</div>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="65px">&nbsp;</td>
    <td align="left"><span class="feed_link"><font style=" font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#606060;">'.$date.'  
	</font>
	'. (($username !=="") ? "&nbsp;-&nbsp;" : "") .'<a href="#" class="comment" id="'.$idt.'" title="Click here to comment"><font style=" font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#333333;">'. (($username !=="") ? "comment" : "") .'</font></a></span></td>
  </tr>
</table>
';
}

$display .='
<div class="comment_load" id="comment'.$id2.'" >
 <table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td valign="top" align="left" width="32px">'.$user_pic2post3.'</td>
<td valign="top">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td align="left" valign="top">&nbsp;<b>'.$usernameqw.'</b>&nbsp;'.$comment.'</td>
  </tr>
  <tr>
   <td align="left">&nbsp;<font style=" font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#606060;">'.$date2.'
    &nbsp;-&nbsp;</font><span><a href="javascript: void(0)" id="'.$id2.'" class="cdelete_update"><font style=" font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#333333;">Remove</font></a></span></td>
  </tr>
</table>
</td>
  </tr>
</table>
 </div>






<div id="fullbox" class="fullbox'.$idt.'">
<div id="commentload'.$idt.'" ></div>
<div class="comment_box" id="c'.$idt.'">
<form method="post" action="" name="'.$idt.'">
<div class="comment_left">'.$user_pic2post.'</div>
<div class="comment_right"><textarea class="text_area" name="comment_value" id="textarea'.$idt.'" onKeyDown="limitText(this,100);" 
onKeyUp="limitText(this,100);">
Write a comment...</textarea><br />
<input type="hidden" name="username" id="'.$idt.'" value="'.$user2.'" /></div>
 <div class="clearer"> </div>
<div class="comment_left">&nbsp;</div>
<div class="comment_right2"><input type="submit" value=" Comment " class="comment_submit" id="'.$idt.'"/></div>
<div class="clearer"> </div>
</form>
</div>
</div>

<hr width="570px">
</div>
</li>
';

}
?>
<ol class="timeline2">
PHP code here!
That's it
<? echo"$display";?>
</ol>

I have adjusted your code but it is mainly invalid variables. Below is a patched up version of your code but still needs a little improvement as some variables don't exist.

<?php 
include_once "scripts/connect_to_mysql.php";
include("db.php");//Connect to Database
include("tolink.php");

//Insert mysql connections here

$r=mysql_query('SELECT * FROM comments, messages WHERE comments.msg_id_fk = messages.msg_id ORDER BY comments.com_id, messages.msg_id') or die(mysql_error());
$messagedump='';
while ($row = mysql_fetch_assoc($r)) {


$idt=$row['msg_id'];//Message id
$msg=$row['message'];//The Message
$useron=$row['username'];//This is the user where the message is posted on
$date=$row['date'];//Message date
//$msg=toLink($msg);
$id2=$row['com_id'];//Comment id
$comment=$row['comment'];//The Comment 
$date2=$row['_date'];//Comment date
$usernameqw=$row['_username'];//User who posted the comment


if ($messagedump!=$msg) {
	$messagedump=$msg;

$display .='
<li class="bar'.$idt.'"  onmouseover=\'document.getElementById("'.$idt.'").style.display="";\' onMouseout=\'document.getElementById("'.$idt.'").style.display="none";\'>
<div>
<div align="left" class="post_box">

<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td valign="top" width="60px">'.$user_picpost.'</td>
    <td valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td valign="top"><a href="#" class="comment"><font style=" font-family:Arial, Helvetica, sans-serif; font-size:14px; color:#333333;"><b>'.$useron.'</b></font></a>&nbsp;<font style=" font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#333333;">'.$msg.'</font></td>
  </tr>
</table>
</td>
    <td valign="top" width="19px" align="right">
<div id="hide_edit_btn" ><span style="display:none;" class="'.$idt.'"  id="'.$idt.'"><a href="javascript<b></b>: void(0)" id="'.$idt.'" >';

$display.= ($useron == $username || $usernameqw == $username) ? "<div class=\"hover_delete\">&nbsp;</div>" : "";
$display.= '</a></span></div>
	</td>
  </tr>
</table>
</div>

<div id="expand_box">
<div id="expand_url"></div>
</div>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="65px">&nbsp;</td>
    <td align="left"><span class="feed_link"><font style=" font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#606060;">'.$date.'  
	</font>
	';
$display.= ($username !=="") ? "&nbsp;-&nbsp;" : "";
$display.= '<a href="#" class="comment" id="'.$idt.'" title="Click here to comment"><font style=" font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#333333;">';
$display.= ($username !=="") ? "comment" : "";
$display.= '</font></a></span></td>
  </tr>
</table>
';
}

$display .='
<div class="comment_load" id="comment'.$id2.'" >
 <table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td valign="top" align="left" width="32px">'.$user_pic2post3.'</td>
<td valign="top">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td align="left" valign="top">&nbsp;<b>'.$usernameqw.'</b>&nbsp;'.$comment.'</td>
  </tr>
  <tr>
   <td align="left">&nbsp;<font style=" font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#606060;">'.$date2.'
    &nbsp;-&nbsp;</font><span><a href="javascript<b></b>: void(0)" id="'.$id2.'" class="cdelete_update"><font style=" font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#333333;">Remove</font></a></span></td>
  </tr>
</table>
</td>
  </tr>
</table>
 </div>






<div id="fullbox" class="fullbox'.$idt.'">
<div id="commentload'.$idt.'" ></div>
<div class="comment_box" id="c'.$idt.'">
<form method="post" action="" name="'.$idt.'">
<div class="comment_left">'.$user_pic2post.'</div>
<div class="comment_right"><textarea class="text_area" name="comment_value" id="textarea'.$idt.'" onKeyDown="limitText(this,100);" 
onKeyUp="limitText(this,100);">
Write a comment...</textarea><br />
<input type="hidden" name="username" id="'.$idt.'" value="'.$usernameqw.'" /></div>
 <div class="clearer"> </div>
<div class="comment_left">&nbsp;</div>
<div class="comment_right2"><input type="submit" value=" Comment " class="comment_submit" id="'.$idt.'"/></div>
<div class="clearer"> </div>
</form>
</div>
</div>

<hr width="570px">
</div>
</li>
';

}
?>
<ol class="timeline2">
PHP code here!
That's it
<?php echo $display; ?>
</ol>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.