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
    Jul 2009
    Posts
    72
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Finding a users position within a query!?

    I have a script which prints out the position of a user in a while loop like this:

    PHP Code:
    $sql "SELECT u.fname, u.lname, u.userid, up.points FROM users u 
    INNER JOIN ( SELECT SUM(point) points, userid FROM bonuspoints 
    GROUP BY user ) up ON u.userid = up.userid 
    ORDER BY up.points DESC"


    $result mysql_query($sql); 

    $x 0
    $i 0
    $prev 0
    $r_count 0

    while(
    $row mysql_fetch_array($result)){ 

    $curr $row['points']; 

    if (
    $curr != $prev

    $i $x 1


    echo 
    ''.$i.' '.$row['fname'].' '.$row['lname'].'<br>'

    $prev $curr
    $x++; 


    Now, this works like a charm, but I want to, first of all, to print out only the users current placement and not all the others...

    As it is now all users are shown in a list...

    The tricky part to this is that the total amount of points the users have is calculated in the query:

    PHP Code:
    SELECT SUM(pointpoints 
    How do i figure out what number the user is within tha or a query?!?!?

    Hope this makes sense and thanks in advance...

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Since your query is what is determining the ranking, you can't just specify WHERE userid = 12345. So I guess you keep the query as-is and only echo anything when the userid matches current userid, at which point you echo and break out of the loop.

  • #3
    Regular Coder
    Join Date
    Apr 2007
    Posts
    295
    Thanks
    0
    Thanked 19 Times in 19 Posts
    You could also query for a total of all users with more points than the current user, increment by one and that should be their position


  •  

    Posting Permissions

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