sandbox Posted April 3, 2013 Share Posted April 3, 2013 Hi, I'm new to the forums and would like to take this opportunity to say hello to everybody. I'm having a slight problem and was wondering if you kind folks could shed some light on it for me please. Basically, I am trying to add numbers, ascending from 1 to 50, to the left side of each persons username. The person with the highest gold amount would have 1. next to their name, then 2. for the second highest, and so forth. I'm thinking a for or foreach loop is the answer, but I cannot seem to get it right. $sql = "SELECT * FROM `users` ORDER BY `gold` DESC LIMIT 50"; $qry = mysql_query($sql); while ($res = mysql_fetch_array($qry)) { echo $res['username'] . " "; echo $res['gold'] . "<br /><br />"; } Thanks for your time. Quote Link to comment https://forums.phpfreaks.com/topic/276472-adding-numbers-to-each-result-cant-figure-it-out/ Share on other sites More sharing options...
computermax2328 Posted April 3, 2013 Share Posted April 3, 2013 Hey, Welcome to the club! You joined a great community! If you want to make this really easy, instead of using a foreach loop, just echo and ordered list <ol></ol>. So something like this...... echo "<ol>"; echo "<li>" . $res['username'] . ""; echo $res['gold'] . "<br/><br/>" . </li>; echo "</ol>" I am not really sure how you want to arrange your content, but that would be the start. Quote Link to comment https://forums.phpfreaks.com/topic/276472-adding-numbers-to-each-result-cant-figure-it-out/#findComment-1422642 Share on other sites More sharing options...
peppericious Posted April 3, 2013 Share Posted April 3, 2013 Hi, I'm new to the forums and would like to take this opportunity to say hello to everybody. I'm having a slight problem and was wondering if you kind folks could shed some light on it for me please. Basically, I am trying to add numbers, ascending from 1 to 50, to the left side of each persons username. The person with the highest gold amount would have 1. next to their name, then 2. for the second highest, and so forth. I'm thinking a for or foreach loop is the answer, but I cannot seem to get it right. $sql = "SELECT * FROM `users` ORDER BY `gold` DESC LIMIT 50"; $qry = mysql_query($sql); while ($res = mysql_fetch_array($qry)) { echo $res['username'] . " "; echo $res['gold'] . "<br /><br />"; } Thanks for your time. If you do want to use a loop, initialise a counter before the loop starts and then increment it before the bottom of the loop: $sql = "SELECT * FROM `users` ORDER BY `gold` DESC LIMIT 50"; $qry = mysql_query($sql); $counter = 1; // create a counter before the loop starts while ($res = mysql_fetch_array($qry)) { echo $counter . " " . $res['username'] . " " . $res['gold'] . "<br />"; $counter++; // add 1 to counter for each iteration of loop } Quote Link to comment https://forums.phpfreaks.com/topic/276472-adding-numbers-to-each-result-cant-figure-it-out/#findComment-1422649 Share on other sites More sharing options...
sandbox Posted April 3, 2013 Author Share Posted April 3, 2013 @computermax2328 - I didn't even think to use HTML for this and that is certainly thinking outside of the box. I prefer to use PHP on this occasion, but thanks though. @peppericious - So simple! I'm kicking myself now! Many thanks. Quote Link to comment https://forums.phpfreaks.com/topic/276472-adding-numbers-to-each-result-cant-figure-it-out/#findComment-1422666 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.