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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Apr 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Arrow Adding Numbers to Each Result - Can't Figure it out!

    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.

    PHP Code:
    $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.

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,456
    Thanks
    8
    Thanked 1,084 Times in 1,075 Posts
    What you're doing can be done with one query ... it's all in the query, using GROUP, COUNT, etc. Sorry I don't have the query string that you need, but maybe someone else will be able to post it. Meanwhile, I'm going to see if I can figure it out.

    You can try googling things about advanced mysql queries, until someone posts the real solution.

    EDIT:
    This is the type of query you'll be looking for when you search google:
    http://stackoverflow.com/questions/1...k-with-filters



    .
    Last edited by mlseim; 04-03-2013 at 01:50 PM.

  • #3
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    721
    Thanks
    20
    Thanked 84 Times in 84 Posts
    maybe just

    PHP Code:

    $sql 
    "SELECT * FROM `users` ORDER BY `gold` DESC LIMIT 50";

    $qry mysql_query($sql);
    $i 1;
    while (
    $res mysql_fetch_array($qry)) {

         echo 
    $i ." ".$res['username'] ." ".$res['gold'] . "<br /><br />";
        
    $i++;


    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • Users who have thanked Arcticwarrio for this post:

    sandboxer (04-03-2013)

  • #4
    New to the CF scene
    Join Date
    Apr 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    @mlseim - That looks like a rather complicated method of approaching the task, but thanks for bringing the alternate resolution to my attention.

    @Arcticwarrio - That worked a treat. Thanks very much!

  • #5
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,456
    Thanks
    8
    Thanked 1,084 Times in 1,075 Posts
    I guess I "over-thunk" that one. Just so you know in the future that if you need to do some complicated grouping, summing, etc. of your database, there are some powerful things that MySQL can do.

    In your example, I thought there were many rows where the users had multiple rows of points and you had to group the result by users and add their points.


  •  

    Posting Permissions

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