Jump to content

comments function


m00nz00mer

Recommended Posts

hey, im making a little system that allows users to add comments to a page.. ive got it all working and it adds the info the the db however say i have for example 'comment1' showing if the user adds a new comment 'comment2' it replaces 'coment1'. im wanting to display all the comments ( comment1 and 2 ).

 

I think its to do with selecting one row...? how could this be adjusted to select more than one row?

 

$reviewedByQuery =

 

"SELECT ..... query here";

 

$reviewedByQueryResult = mysql_query($reviewedByQuery);

$num_rows = mysql_num_rows($reviewedByQueryResult);

 

if($num_rows>0 && $num_rows_date>0 && $num_rows_comment>0){

 

$commentText = mysql_result($reviewCommentQueryResults, 0);

$reviewDate = mysql_result($reviewDateQueryResults, 0);

$reviewedBy = mysql_result($reviewedByQueryResult, 0);

 

//Display Comment

echo "Comments: <div class='detailTextGrey'>".$commentText." </div><br/>";

 

//Display Date

echo "Date of Review: <div class='detailTextGrey'>".$reviewDate." </div>";

 

//Display Review Query

echo "Reviewed by: <div class='detailTextGrey'>".$reviewedBy." </div><br/><br/>";

} else {

echo "No reviews have been posted...";

}

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

You need to loop through the comments else if there is more than one else only 1 result will be shown.

if($num_rows>0){
         
    while ($comment = mysql_fetch_row($reviewedByQueryResult)) {
        // your info here
    }

} else {

    // no comments made

}

Link to comment
https://forums.phpfreaks.com/topic/92245-comments-function/#findComment-472617
Share on other sites

hey, thanks for the reply, ive tried using the while loop like so... but its displaying a never ending list of the same commend, name and date?

 

$reviewedByQueryResult = mysql_query($reviewedByQuery);

$num_rows = mysql_num_rows($reviewedByQueryResult);

 

if($num_rows>0 && $num_rows_date>0 && $num_rows_comment>0){

while ($comment = mysql_fetch_row($reviewedByQueryResult)) {

 

$commentText = mysql_result($reviewCommentQueryResults, 0);

$reviewDate = mysql_result($reviewDateQueryResults, 0);

$reviewedBy = mysql_result($reviewedByQueryResult, 0);

 

//Display Comment

echo "Comments: <div class='detailTextGrey'>".$commentText." </div><br/>";

 

//Display Date

echo "Date of Review: <div class='detailTextGrey'>".$reviewDate." </div>";

 

//Display Review Query

echo "Reviewed by: <div class='detailTextGrey'>".$reviewedBy." </div><br/><br/>";

}

} else {

echo "No reviews have been posted...";

}

 

Link to comment
https://forums.phpfreaks.com/topic/92245-comments-function/#findComment-472644
Share on other sites

Hey thanks for the advice, im trying to use the array now however im getting Notice: Undefined offset: 3 2 and 1. Does anyone know why this is happening? thanks again.

 

if($num_rows_comment>0){

while ($comment = mysql_fetch_array($reviewedByQueryResult)) {

 

list ($comBookISBN, $comUserID, $comReviewDate, $comReviewText) = $comment;

 

//Display Review Query

echo "Reviewed by: <div class='detailTextGrey'>".$comReviewText." </div><br/><br/>";

}

} else {

echo "No reviews have been posted...";

}

 

?>

Link to comment
https://forums.phpfreaks.com/topic/92245-comments-function/#findComment-472716
Share on other sites

Hey,

 

You need to follow this kind of logic

 

$query = mysql_query("SELECT field1, field2 FROM table")

if (mysql_num_rows > 0) {

         while ($comment = mysql_fetch_array($query)) {
         
         //Display Comment
         echo $comment['field1'];
         echo $comment['field2'];
         
         }

}

 

I think you need to read up on loops and the mysql_fetch_array function. I think you are trying to over-complicate your code.

Link to comment
https://forums.phpfreaks.com/topic/92245-comments-function/#findComment-472878
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.