Jump to content

Comments Admin


tomhoad

Recommended Posts

Hi,

 

I'm trying to display a list of comments for each article:

 

<?php
//display the current news comments
$query = "SELECT id, news_id, name, comment, date FROM news_comments ORDER BY news_id";
$result = mysql_query($query) or die('Error : ' . mysql_error());
$comment = substr($comment,0,50); 
?>
<br/><br/>
<table border="1">
<tr>
<th>Week number</th>
<th>Author</th>
    <th>Comment</th>
    <th></th>
</tr>
<?php
while(list($news_id) = mysql_fetch_array($result, MYSQL_NUM))
{
	echo $news_id;

	while(list($name, $comment) = mysql_fetch_array($result, MYSQL_NUM))
	{//while loop finished below


	?>

	<tr>
		<td><?php echo $name;?></td>
	    <td><?php echo $comment;?></td>
	    <td><a href="javascript:delArticle('<?php echo $id;?>','<?php echo $name;?>');">delete</a></td>
	</tr>

<?php
	}
} //finishes the above while loops
?>

 

I think the problem lies in my query at the top, although I'm not sure. Ignore the fact the tables will be all over the place for the moment (I plan to convert this into divs when the functionality is there).

 

Can anybody recommend a way to do this? I think from the code it's fairly clear what's going on (or at least what I want to go on!!). For every news article, display the comments for that article.

 

Kind regards,

 

Tom

Link to comment
https://forums.phpfreaks.com/topic/150556-comments-admin/
Share on other sites

Have made some small progress:

 

<?php
//display the current news comments
$query = "SELECT news_id FROM news_comments ORDER BY news_id";
$result = mysql_query($query) or die('Error : ' . mysql_error());
?>
<br/><br/>

<?php
while(list($news_id) = mysql_fetch_array($result, MYSQL_NUM))
{
	echo $news_id;

	$comment_query = "SELECT name, comment, news_id FROM news_comments WHERE news_id = '$news_id'";
	$comment_result = mysql_query($comment_query) or die('Error : ' . mysql_error());
	$comment = substr($comment,0,50);

	while(list($name, $comment) = mysql_fetch_array($comment_result, MYSQL_NUM))
	{//while loop finished below


	?>

	    <?php echo $name;?> <br/>
	    <?php echo $comment;?><br/>
	    <a href="javascript:delArticle('<?php echo $id;?>','<?php echo $name;?>');">delete</a><br/>

	<br/>
	<br/>


<?php
	}
} //finishes the above while loops
?>

 

Which outputs something closer:

 

1 John Johnson

This comment should only appear on article 1

delete

 

 

2 tom

comment on article 2

delete

 

 

jimmy jones

article comment

delete

 

 

tommy h

its me!

delete

 

 

2 tom

comment on article 2

delete

 

 

jimmy jones

article comment

delete

 

 

tommy h

its me!

delete

 

 

2 tom

comment on article 2

delete

 

 

jimmy jones

article comment

delete

 

 

tommy h

its me!

delete

 

Not sure why it's listing the 2nd article two further times?

 

What I'd like to end up with is:

 

Week 1

John Johnson - This comment should only appear on article 1 - [delete]

 

Week 2

John Johnson - This comment should only appear on article 1 - [delete]

John Johnson - This comment should only appear on article 1 - [delete]

 

Week 3

John Johnson - This comment should only appear on article 1 - [delete]

 

etc.

 

I hope that's a bit clearer?

 

Link to comment
https://forums.phpfreaks.com/topic/150556-comments-admin/#findComment-790821
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.