tomhoad Posted March 22, 2009 Share Posted March 22, 2009 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 More sharing options...
Yesideez Posted March 22, 2009 Share Posted March 22, 2009 How are you linking to the article in question? You also forgot to mention what your program isn't doing what it should be - any sample data? Link to comment https://forums.phpfreaks.com/topic/150556-comments-admin/#findComment-790809 Share on other sites More sharing options...
tomhoad Posted March 22, 2009 Author Share Posted March 22, 2009 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 More sharing options...
redarrow Posted March 22, 2009 Share Posted March 22, 2009 are you looping twice? clue? look into mysql joins. Link to comment https://forums.phpfreaks.com/topic/150556-comments-admin/#findComment-790849 Share on other sites More sharing options...
tomhoad Posted March 22, 2009 Author Share Posted March 22, 2009 Im not sure how mySql joins will help, I only have one table I need a query. I just want it to loop the comments for each news_id, displaying all comments for that ID. Link to comment https://forums.phpfreaks.com/topic/150556-comments-admin/#findComment-790937 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.