Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jan 2011
    Posts
    42
    Thanks
    5
    Thanked 0 Times in 0 Posts

    PHP MYSQL while loop, assign row to unique variable??

    Hi, I'll try to make this breif, I've dumbed down the example as well but the principle is the same.

    I have a module that draws the top 5 rated pieces of content from the database and displays them in a top rated box.

    PHP Code:
    $q "SELECT * FROM CONTENT ORDER BY average_rating DESC LIMIT 5";
    $r mysqli_query($dbc$q);
        while (
    $row mysqli_fetch_array($r) {
            
    // PRINT THE RESULTS
        

    Now what I want to do is individulise each of the results returned, ie. add '1st' to the top rated and '5th' to the last result in the set...

    What would be the best way to acheive this, I thought of making 5 queries and assigning each own to its own variable but there will be an easier way to do it that I'm unaware of I'm sure.

    Thanks in advance!!

    Thanks
    Last edited by CrazyCrisUk; 01-23-2012 at 12:31 PM.

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,801
    Thanks
    160
    Thanked 2,216 Times in 2,203 Posts
    Blog Entries
    1
    PHP Code:
    $positions=array("First""Second""Third""Fourth""Fifth");
    $count=0;
    while (
    $row mysqli_fetch_array($rMYSQLI_ASSOC)) {
            
    // PRINT THE RESULTS
             
    echo $positions[$count]'. ':;
             
    $count++;
        } 
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    CrazyCrisUk (01-23-2012)

  • #3
    New Coder
    Join Date
    Jan 2011
    Posts
    42
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Thanks!!

    I was thinking keeping track of some sort of increment would be the way to go but I had NO idea where to start. Using the count directly with the array index is genuis!



  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •