#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2000
    Posts
    32
    Rep Power
    15
    Anyone know how to get an array indexed by numbers (i.e. - NON - Associative)? Seems like it should be able to be done with a function - but I haven't been able to find one. Any help is greatly appreciated...thanks!
  2. #2
  3. No Profile Picture
    Apprentice Deity
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 1999
    Location
    Niagara Falls (On the wrong side of the gorge)
    Posts
    3,237
    Rep Power
    19
    mysql_fetch_row()
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2000
    Posts
    32
    Rep Power
    15
    Let me explain myself more. I want to query a database with an ORDER BY clause, then retrieve a certain row from that entire database based on the order. So fetching a row won't work...what I'm looking for is a function that'll return an array indexed by how they were returned in mysql (all rows).. get what I'm saying? If not and you want to help (please!) contact me at my email address here.
  6. #4
  7. No Profile Picture
    Apprentice Deity
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 1999
    Location
    Niagara Falls (On the wrong side of the gorge)
    Posts
    3,237
    Rep Power
    19
    Let me get this straight. You're using an order by clause and you want just a certain row from the result?

    If so you can use LIMIT in your query to get that row

    select * from table where something order by some_field LIMIT n,1;

    where n is the row you want from 1 to whatever and 1 tells it to return just one row.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2000
    Posts
    32
    Rep Power
    15
    No, like I want to be able to rank someone based on everyone else. Say there's 1000 users and I want to order them by a cetain field and then get what # in that order a particular user is. I've been thinking it over and I don't think there's a way to do that with php/mySql so I might be chasing down a false path.
    Doy ou get what I'm saying? I want to get mySql to arrange the database in an order THEN match the query string and return an array so that I can tell what rank that user is. I'm fairly sure I'm going down a black hole here but it seems like it should be able to be done....
  10. #6
  11. No Profile Picture
    Apprentice Deity
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 1999
    Location
    Niagara Falls (On the wrong side of the gorge)
    Posts
    3,237
    Rep Power
    19
    You sure have a round about way of explaining yourself

    Anyway, there isn't really an elegent way to do what you want (not that I can think of anyway) but it is most definately possible.

    Limit your query to the fields you have an interest in, the one you want to order by and the one that holds the user identifier.

    $query="select userid from table order by some_field";
    $result=mysql_query($query);
    $i=1;
    while (list($temp)=mysql_fetch_row($result))
    {
    $user[$temp]=$i;
    $i++;
    }

    This will allow you to put the user's id as the index of $user and get the rank.

    print $user[gpolins];

    would display your rank, etc.

    I know you said you wanted a standard array and not associative, but from what you described this should work better. If not just change it to:

    $user[$i]=$temp;
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2000
    Posts
    32
    Rep Power
    15
    Thanks - that's basically what I had in mind of doing, but I was hoping of an elegant way of doing it. Thanks anyway. (Sorry about my roundaboutness )
  14. #8
  15. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Posts
    1
    Rep Power
    0
    There's another, quite easy way to find the rank of a table entry, not using arrays at all (and therefore, presumably less memory intensive). For fun, I timed the two apporoaches using microtime, and found this solution to be marginally quicker (2-3/100 of a second= on a table with about 5000 entries. Could of course depend on the complexity of your if-statement... :-)

    Here's my 2 cents:

    $query="select userid from table order by some_field";
    $result=mysql_query($query);

    $rank=1;
    while($myrow=mysql_fetch_row($result))
    {
    // $match is the ID you're trying to find
    // the rank number for
    if($myrow[0]==$match)
    { print $rank; }
    $rank++;
    }

Similar Threads

  1. Need help getting value of all numbers in an array
    By mark46 in forum PHP Development
    Replies: 7
    Last Post: February 15th, 2004, 11:49 PM
  2. Replies: 2
    Last Post: February 12th, 2004, 08:29 AM
  3. Create a dynamic array of sorted numbers
    By catz423 in forum C Programming
    Replies: 2
    Last Post: February 2nd, 2004, 07:50 PM
  4. Writing array of random numbers to a database
    By Beda in forum ASP Programming
    Replies: 2
    Last Post: February 2nd, 2004, 04:34 PM
  5. populate array with mysql data
    By clearbar in forum PHP Development
    Replies: 2
    Last Post: January 14th, 2004, 04:41 AM

IMN logo majestic logo threadwatch logo seochat tools logo