Re: Am I usort-ing correctly?

From: Date: Thu, 22 Jun 2000 14:21:15 +0000
Subject: Re: Am I usort-ing correctly?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-2628@lists.php.net to get a copy of this message
At 10:03 AM 6/22/00 -0400, Melissa Tonelli wrote:
Hello, all. 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. 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).
Hi Melissa, I would really sit down with the php manual and read, read, read!!! :)
$result = mysql_query("SELECT DISTINCT CoID, Co, FROM Cos WHERE CoType='Retailer' ORDER BY CoEmps DESC LIMIT 100");
If you have to do two queries, then why not sort the results when you make the query? Also, the 'Co, From' part of your query will generate a syntax error.
function co_sort($a,$b) {
        return strcmp($result['Co']);
}
Where is the function getting access to $result['Co']? The function only knows about variables that are passed as function arguments (like $a and $b) or variables that you have declared as global using the global keyword. strcmp compares two sequences of one or more characters. It will not work if it only has one string of characters to work with. Appropriate use in this function would be strcmp($a, $b)
usort($result, co_sort);
You are trying to sort the result pointer from your query. Usort only works on arrays. BTW mysql_query does not return actual data - instead it returns a pointer to the data. You can then use that pointer with various other mysql functions to get at the data from your query. Something like this should do what you want: $query = "SELECT DISTINCT CoID, Co FROM Cos WHERE CoType='Retailer' ORDER BY Co, CoEmps DESC LIMIT"; # Query for the top 100 retailers $query_handle = mysql_query( $query . ' 100'); # Fetch the data from your query and assign it to an array while ($temp = mysql_fetch_array ($query_handle, MYSQL_ASSOC)) {
        $top_100_retailers[] = $temp;
} # Query for the next 100 retailers $query_handle = mysql_query( $query . ' 100, 100'); # Fetch the data from your query and assign it to an array while ($temp = mysql_fetch_array ($query_handle, MYSQL_ASSOC)) {
        $top_200_retailers[] = $temp;
} HTH, Zak Greant Creative Director Nucleus Information Service Inc. "A camel is a horse designed by a committee." - Alex Issigonis (Designer of the Austin Mini)

« previous php.general (#2628) next »