Re: usort within a class

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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: 
> > 
> > <?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/
> 
> 
> 
> 
> 
> 

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



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux