Jump to content

Adding Numbers To Each Result - Can't figure it out!


sandbox

Recommended Posts

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.
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

 

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
}
Link to comment
Share on other sites

@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. :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.