Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

PHP - how to use while loop effectively?

Options
  • 07-04-2009 8:34am
    #1
    Registered Users Posts: 517 ✭✭✭


    Hey there,

    I have php code that is querying a database (mysql) and am bring results back to the web browser.

    Lets say im building a chat forum, and people post comments. For this i have something like: "SELECT comment, user, commentdate FROM COMMENTS". As each users comment is listed, I want their userid to be a link to their own page with comments they posted (or a personal page if you will - kinda like boards - each username is a hyperlink) To do this, I set up my code like this:
    $followinguser = "<a href = 'http://127.0.0.1/userfeed.php'>";
    $result = mysql_query($query, $connection);
    $i = 0;
    if(!$result) {
    die("could not query database");
    }


    while($row = mysql_fetch_array($result)) {


    $commentby[$i] = $row;

    $_SESSION = $commentby[$i];
    echo("<b>". "Comment by:" ."</b>" .$followinguser .$commentby[$i] ."</a>" ."<br />");
    $comment[$i] = $row;
    echo ($comment[$i] ."<br />");
    $commentdate[$i] = $row;
    echo ("<b>" .$commentdate[$i] ."</b>" ."<br />");
    $i++;

    }

    Problem is, that whenever the above code executes and I click on any of the uesrs - it always directs me to the last listed user in the SQL (while loop) as this is the value of i at the end. If I select Adam or Barry, I dont want to be brought to Zed's homepage.

    Any ideas how i might overcome this?
    Cheers


Comments

  • Users Awaiting Email Confirmation Posts: 351 ✭✭ron_darrell


    No offense but what a mess. What's with all the arrays? Are you using them later on for something else? I think what you're trying to generate is something like this
    $followinguser = "<a href = 'http://127.0.0.1/userfeed.php?id=";
    $result = mysql_query($query, $connection);
    $i = 0; 
    if(!$result) {
    die("could not query database");
    }
    while($row = mysql_fetch_array($result)) {
    echo "<b>". "Comment by:" ."</b>" .$followinguser .$row['username']."'>".$row['username']."</a><br />"; 
    echo $row['comment']."<br />";
    echo "<b>" .$row['commentdate']."</b>" ."<br />";
    } 
    

    I'm making a couple of assumptions here
    1. Your userfeed.php page uses the get stream to access the user you wish to see
    2. You're not using the session variable to get the user for userfeed.php (if you are that's why you're only getting the last user)

    Hope that's some help. No need for all those messy arrays really unless you need the data someplace else.

    Regards,
    RD


  • Registered Users Posts: 517 ✭✭✭lisbon_lions


    Hi Thanks for your comments. I was not aware of a simpler way to do this other than the use of arrays. I will give your suggestion a shot and see if it works - as it does look more tidy. And yes, i am not using the arrays elsewhere.

    I got this working , btw, by passing the $followinguser variable into the while loop itself and linking that to the userfeed.php page which has the user in the url (which I dont mind).
    $followinguser = "<a href = 'http://127.0.0.1/userfeed.php?User=&quot; .$commentby[$i] ."'>";
    Thanks again.


Advertisement