Re: Am I usort-ing correctly?
| From: | Rasmus Lerdorf | Date: | Thu, 22 Jun 2000 17:02:51 +0000 |
| Subject: | Re: Am I usort-ing correctly? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-2658@lists.php.net to get a copy of this message | ||
> I have a table that stores information on the number of employees at
> approximately 300+ companies, 200 of which are companies of a certain type
> (let's say retailers). I generate rankings on the fly by having my query
> ORDER by #_employees DESC. This eliminates the need of having to maintain
> a "ranks" field in the db, but it raises a new problem for me.
>
> I need to present a list of the 200 retailers alphabetically, but first
> broken into two halves i.e., the biggest 100 retailers sorted alpha, the
> remaining 200 retailers sorted alpha. I couldn't think of a way to do this
> in MySQL -- if someone knows a way, that would be great.
select * from employees order by size,name DESC LIMIT 100
then to do the second set
select * from employees order by size,name DESC LIMIT 101,200
> What I'm attempting instead is to run the ranking query (which I can LIMIT
> to break the list in half), then usort the returned array. But I can't
> make it work (wrong datatype in usort() call). I think there's something
> wrong with the variable I'm using in my strcmp?? Here's the code. Any
> hints/suggestions greatly appreciated (as always).
>
> $result = mysql_query("SELECT DISTINCT CoID, Co, FROM Cos WHERE
> CoType='Retailer' ORDER BY CoEmps DESC LIMIT 100");
>
> function co_sort($a,$b) {
> return strcmp($result['Co']);
> }
> usort($result, co_sort);
You can't just pass in $result. This is just an integer identifying the
result set. You need to read the result set into an array by looping
through one of the Fetch functions.
-Rasmus