On Monday, 18 April 2011 at 21:27, Ashley Sheridan wrote: I'm trying to get the usort function working inside of a class, but am > having some issues. Basically, the compare function which is the second > parameter isn't recognised, but I'm not really sure how to indicate > exactly where it is. > > I've gone over the usort() docs and read the user comments, and the only > thing I've found so far which looked like it was the same issue gave > this example: > > > > bo at erichsen dot com 20-Mar-2001 01:16 > when using usort to refer to a function inside a class i have > succesfully used: > > <?php usort($myarray,array($this,"cmp")); ?> > > Unfortunately, that doesn't work either. A basic example is as follows: > > > class Search_model extends Model > { > function get_results($q) > { > if(strlen($q)) > { > $results = array(); > > $words = explode(' ', $q); > sort($words); > > // build the basic match query parts to be used for the content and > blog tables > if(substr($words[0], 0, 1) == '+' || substr($words[0], 0, 1) == '-' > || substr($words[0], 0, 1) == '"') > { > $queryP1 = "SELECT *, MATCH(`content`) AGAINST('$q' IN BOOLEAN MODE) > AS `score` FROM"; > $queryP2 = "WHERE MATCH(`content`) AGAINST('$q' IN BOOLEAN MODE)"; > } > else > { > $queryP1 = "SELECT *, MATCH(`content`) AGAINST('$q') AS `score` > FROM"; > $queryP2 = "WHERE `display`='yes' AND MATCH(`content`) > AGAINST('$q')"; > } > > > $sql = "$queryP1 `content` $queryP2"; > $query = $this->db->query($sql); > foreach($query->result() as $row) > { > $results[] = $row; > $results[count($result)-1]->content_type = 'content'; > } > > usort($results, array($this, "content_score_sort")); > } > } > > function content_score_sort($a, $b) > { > // custom sort function to sort pages based on their score, which is > an array value within each page > if($a['score'] == $b['score']) > return 0; > else > return ($a['score'] > $b['score']) ? -1 : 1; > } > } > > I know it's probably something very simple, but for the life of me I > can't find what I'm missing. > > I think in part it's that the normal way I know of calling that sort > function from within the class is $this->content_score_sort(), but that > isn't recognised either. > > If anyone can shed any light on this it'd be very welcome! That is the correct way to specify it. Works fine for me: http://dev.stut.net/php/usort.php What makes you think it's not being recognised? I note that your get_results method doesn't actually return anything, so how do you know the sort isn't working? -Stuart -- Stuart Dallas 3ft9 Ltd http://3ft9.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php