|
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 { 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
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
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);
Function References date: http://www.php.net/date
Original Article http://www.debuggeddesigns.com/blog/view/php-help-using-usort-in-cakephp-controllers COMMENTS (displaying 0 comments) POST (leave a comment) |
POPULAR BLOG TAGS
brain
cakephp
usort
mbta
snowboard
privacy
quicksilver
encryption
email
boston
color
youtube
1984
captcha
routes
magazine
launch
code
flash
mailto
class
hobnox
mit
release
orwell
charlie
open source
defcon
security
whoami
gmail
trice
ableton
published
bakery
as
php
google
debuggeddesigns
science
php5tube
general
spam
germans
regular expressions
skateboard
rot13
drum machines
sports
geek
|