...

View Full Version : PHP MYSQL while loop, assign row to unique variable??



CrazyCrisUk
01-23-2012, 01:28 PM
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.



$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

abduraooft
01-23-2012, 01:35 PM
$positions=array("First", "Second", "Third", "Fourth", "Fifth");
$count=0;
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
// PRINT THE RESULTS
echo $positions[$count]'. ':';
$count++;
}

CrazyCrisUk
01-23-2012, 01:45 PM
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!

:thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum