posted 09/30/2008 by Chris

This was originally a tutorial I wrote for bakery.cakephp.org, but it didn't get published because it was "more a general PHP tip than a CakePHP article."  Haters.

Anyways, if you're using the function usort inside of a cakephp controller and are receiving errors like "Warning: usort() [function.usort]: Invalid comparison function in...", then you may be referencing your sorting function incorrectly.  Also useful examples showing how to use Unix timestamps (date/mktime).

 

Help using the usort function

The trick is to make sure to tell usort which list to sort, AND ALSO the class and function name that includes the search algorithm. (sort algorithm example shown below)

class ItemsController extends AppController {

  //if reusable, then you should consider putting this function in app_controller.php
  function item_sorter($a,$b) {
    //... comparison logic
  }

  //accepts an unsorted list and returns a sorted list
  function doSomething($unsorted_list) {
    //pass the list to sort and the class/name of the sort function
    usort($unsorted_list, array('ItemsController','item_sorter'));

    //$unsorted_list is now sorted
    return $unsorted_list;
  }
}

Help writing a sort function

Below is an example sorting function, in case you've never used usort before. I am given a list of blog, event, and video objects, which all have the common variable 'unixtime' - and use this to sort.

//this is the empty function found in the controller above
function item_sorter($a,$b) {
  $a_unixtime = $b_unixtime = '';

  //extract the timestamp from the first object passed to the function
  if( isset($a['Blog']) ) { $a_unixtime = $a['Blog']['unixtime']; }
  if( isset($a['Event']) ) { $a_unixtime = $a['Event']['unixtime']; }
  if( isset($a['Video']) ) { $a_unixtime = $a['Video']['unixtime']; }

  //extract the timestamp from the second object passed to the function
  if( isset($b['Blog']) ) { $b_unixtime = $b['Blog']['unixtime']; }
  if( isset($b['Event']) ) { $b_unixtime = $b['Event']['unixtime']; }
  if( isset($b['Video']) ) { $b_unixtime = $b['Video']['unixtime']; }
    
  //return -1, if object 'a' more recent than object 'b'
  //return 1, if object 'a' less recent than object 'b'
  if ($a_unixtime == $b_unixtime) {return 0;}
  return ($a_unixtime > $b_unixtime) ? -1 : 1;
}

 

Help creating Unix timestamps

The mktime() function returns the Unix timestamp corresponding to the arguments given. This timestamp is a long integer containing the number of seconds between the Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified.

//creates a timestamp for the current year/month/day/hour/minute/second
$current_unixtime = mktime(date('H'), date('i'), date('s'), date('m'), date('d'), date('Y'));

 

Help using Unix timestamps

Here are a few lines of code to show how easy it is to extract the year/month/day from a Unix timestamp:

$extract_the_year = date('Y', $current_unixtime);
$extract_the_month = date('F', $current_unixtime);
$extract_the_day = date('j', $current_unixtime);

 

Function References

date: http://www.php.net/date
isset: http://www.php.net/isset
mktime: http://www.php.net/mktime
usort: http://www.php.net/usort

 

Original Article

http://www.debuggeddesigns.com/blog/view/php-help-using-usort-in-cakephp-controllers

Share:
facebook myspace digg del.icio.us fark stumbleupon live spurl furl reddit yahoo

COMMENTS (displaying 0 comments)

POST (leave a comment)

Name:
Email:
Message:
Verify:
CAPTCHA Image