Re: [PHP] usort within a class
On Monday, 18 April 2011 at 21:52, Ashley Sheridan wrote: On Mon, 2011-04-18 at 21:41 +0100, Stuart Dallas wrote: > > 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: > > > > > > > > > > > > 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 > Because I don't need it to return anything at that point, the usort call > is within the get_results() method itself. Also, as I keep getting a > variety of errors with the usort call I'm taking a wild guess that it > doesn't work. > > With usort($results, array($this, "content_score_sort")); > I get 'Fatal error: Cannot use object of type stdClass as array' > > usort($this->results, array($this, "content_score_sort")); gives > 'Message: usort() expects parameter 1 to be array, null given' > > This is all built around a CodeIgniter class, but I figured this isn't > really a CI question but a PHP one, however I'm still at a loss :-/ The usort call is at the end of the get_results method, and $results is local. If you want it to be a member of the class you need to set it as such. I think you'll find your issue is that each element of $results will be an object, but in content_score_sort you're treating them as arrays. Change $a['score'] to $a->score and modify $b in the same way. -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
Re: [PHP] usort within a class
On Mon, 2011-04-18 at 21:41 +0100, Stuart Dallas wrote: > 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: > > > > > > > > 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/ > > > > > > Because I don't need it to return anything at that point, the usort call is within the get_results() method itself. Also, as I keep getting a variety of errors with the usort call I'm taking a wild guess that it doesn't work. With usort($results, array($this, "content_score_sort")); I get 'Fatal error: Cannot use object of type stdClass as array' usort($this->results, array($this, "content_score_sort")); gives 'Message: usort() expects parameter 1 to be array, null given' This is all built around a CodeIgniter class, but I figured this isn't really a CI question but a PHP one, however I'm still at a loss :-/ -- Thanks, Ash http://www.ashleysheridan.co.uk -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] usort within a class
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: > > > > 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
RE: [PHP] usort within a class
Working perfect, thanks :) I did RTFM but I did miss that :) -Original Message- From: Robert Cummings [mailto:[EMAIL PROTECTED] Sent: Thursday, August 24, 2006 3:46 AM To: Peter Lauri Cc: php-general@lists.php.net Subject: Re: [PHP] usort within a class On Thu, 2006-08-24 at 03:13 +0700, Peter Lauri wrote: > Hi, > > Is it possible to have the compare function in a class? I can not get it to > work, this is pseudo code: > > class A { >function getArray() { > //dosomethingandgetanarray > $array = blabla; > usort($array, "$this->myCompareFunction"); > //Or maybe "A::myCompareFunction" >} > >function myCompareFunction($a, $b) { > //return rajraj depending on $a and $b values >} > } > > > Right now I have keep the compare function outside of the class like this: > > class A { >function getArray() { > //dosomethingandgetanarray > $array = blabla; > usort($array, "$this->myCompareFunction"); >} > } > > function myCompareFunction($a, $b) { >//return rajraj depending on $a and $b values > } > RTF :) usort( $array, array( $this, 'myCompareFunction' ) ) Cheers, Rob. -- .. | InterJinn Application Framework - http://www.interjinn.com | :: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] usort within a class
On Thu, 2006-08-24 at 03:13 +0700, Peter Lauri wrote: > Hi, > > Is it possible to have the compare function in a class? I can not get it to > work, this is pseudo code: > > class A { >function getArray() { > //dosomethingandgetanarray > $array = blabla; > usort($array, "$this->myCompareFunction"); > //Or maybe "A::myCompareFunction" >} > >function myCompareFunction($a, $b) { > //return rajraj depending on $a and $b values >} > } > > > Right now I have keep the compare function outside of the class like this: > > class A { >function getArray() { > //dosomethingandgetanarray > $array = blabla; > usort($array, "$this->myCompareFunction"); >} > } > > function myCompareFunction($a, $b) { >//return rajraj depending on $a and $b values > } > RTF :) usort( $array, array( $this, 'myCompareFunction' ) ) Cheers, Rob. -- .. | InterJinn Application Framework - http://www.interjinn.com | :: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php