Display comments AND poster's info!

Discussion in 'MySQL' started by Tony Brar, Jun 6, 2012.

  1. #1
    Hi everybody.
    I have a problem with my PHP/MySQL. I want my code to display all comments for a piece of content.
    Each comment should have the poster's info in a table, then the text in the comment, then the date posted.
    Right now, it displays comments right, but that's not the issue.
    Just to test it out, I put one comment in the database. It displayed the comment - exactly 10 times.:confused:
    For the page to work, the id of the content has been sent through $_GET.
    Here is the code (very messy, I'm new to PHP/MySQL):
    
    <?php
    //connect/declare
    $con = mysql_connect("******************","******","******");
    if (!$con)
    {
    die('Error: ' . mysql_error());
     }
    //select db
    mysql_select_db("*********", $con);
    //select appropriate row
    $contentresult = mysql_query("SELECT * FROM content WHERE content_id=$_GET[content_id]");
    $userresult = mysql_query("SELECT * FROM users WHERE user_id='$_GET[user_id]'");
    $commentresult = mysql_query("SELECT * FROM comments WHERE content_id='$_GET[content_id]'");
    $commentrow = mysql_fetch_array($commentresult);$userrow = mysql_fetch_array($userresult);
    $contentrow = mysql_fetch_array($contentresult);
    //end connect/declare
    //print each comment
    foreach ($commentrow as $value)
    {
    $commentrow['user_id'] = $user_id;
    $posterresult = mysql_query("SELECT * FROM users WHERE user_id='1'");
    $posterrow = mysql_fetch_array($posterresult, MYSQL_ASSOC);
    //print a comment
    echo '<div class="comment"><div class="userinfo"><table border="0"><tr><span class="userinfoheader"><th colspan="2"><strong><em>User info</em></strong></th></span></tr><tr><span class="uiusername"><td><em>Username:</em></td></span><span class="uiusernameval"><td><strong>' . $posterrow['username'] . '</strong></td></span></tr><tr><span class="uicoins"><td><em>Coins:</em></td></span><span class="uicoinsval"><td><strong>' . $posterrow['coins'] . '</strong></td></span></tr><tr><span class="uijoined"><td><em>Join Date:</em></td></span><span class="uijoinedval"><td><strong>' . $posterrow['datejoined'] . '</strong></td></span></tr><tr><span class="uiuploads"><td><em>Uploads:</em></td></span><span class="uiuploadsval"><td><strong>' . $posterrow['uploads'] . '</strong></td></span></tr></table></div><h3><strong>' . $posterrow['username'] . '</strong> said:</h3><p>' . $commentrow['text'] . '</p><p><em>Posted ' . $commentrow['dateposted'] . '</em></p></div>';
    }
    mysql_close($con);
    ?>
    
    PHP:
    Please help.

    - Tony
     
    Tony Brar, Jun 6, 2012 IP
  2. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #2
    What's the value of $commentrow['text'] at this point? (Use whatever debugger you use to throw it out.) Also, how many rows are there that have a content_id of $_GET[content_id]?
     
    Rukbat, Jun 16, 2012 IP
  3. Tony Brar

    Tony Brar Active Member

    Messages:
    220
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    75
    #3
    for the first question, $commentrow['text'] is the text of the comment

    for the second question, as many rows that are comments on the piece of content that has that content_id
     
    Tony Brar, Jun 16, 2012 IP
  4. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #4
    Not what I asked. What are the actual values at the time the code is executing? (If you can't get to the values of variables during runtime, you can't debug your code.)

    IOW, is $commentrow['text'] an array with one element that has some text, or is it an array of 10 elements, each of which has the same text, or something else? Right after the code executes and before
    [COLOR=#b1b100]foreach[/COLOR] [COLOR=#009900]([/COLOR][COLOR=#000088]$commentrow[/COLOR] [COLOR=#b1b100]as[/COLOR] [COLOR=#000088]$value[/COLOR][COLOR=#009900])
    Code (markup):
    [/COLOR]
     
    Rukbat, Jun 16, 2012 IP
  5. Tony Brar

    Tony Brar Active Member

    Messages:
    220
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    75
    #5
    Tony Brar, Jun 16, 2012 IP