Rebelrebellious Posted December 29, 2008 Share Posted December 29, 2008 function orderBy($data, $field){ if($field == 'time'){ $code = "return strcmp(strtotime(\$a['$field']), strtotime(\$b['$field']));"; } else { $code = "return strnatcmp(\$a['$field'], \$b['$field']);"; } usort($data, create_function('$a,$b', $code)); return $data; } $array = orderBy($array, $sortby); The goal is to sort my time columns by the datetime which is formatted '2008-12-29 13:34:30' . Do I need to call strtotime() on each one? In other words, can I just sort the string? Either way, what is wrong with this because the results are not returning in asorted fashion? Thank you. Link to comment https://forums.phpfreaks.com/topic/138763-solved-strcmp-for-integers/ Share on other sites More sharing options...
Rebelrebellious Posted December 29, 2008 Author Share Posted December 29, 2008 This is the result order that I am getting. Time Agent Location Event 2008-12-29 13:58:38 413 19 Call ended. 2008-12-29 13:06:22 413 19 New response: oh yeah. Question: are you happy? 2008-12-29 13:09:26 413 15 New response: fish sticks Question: what would make you happier? 2008-12-29 13:58:40 413 15 Call ended. 2008-12-29 13:11:54 413 19 Call ended. 2008-12-29 13:09:12 413 15 Call placed. 2008-12-29 13:11:56 413 19 Call placed. 2008-12-29 12:56:19 413 1 Call ended. 2008-12-29 12:56:19 413 19 Call placed. Link to comment https://forums.phpfreaks.com/topic/138763-solved-strcmp-for-integers/#findComment-725561 Share on other sites More sharing options...
Rebelrebellious Posted December 29, 2008 Author Share Posted December 29, 2008 Sorry, and thanks for looking. My $sortby variable was not an accurate reference. In my application it needed to be $this->sortby. Thanks again, Link to comment https://forums.phpfreaks.com/topic/138763-solved-strcmp-for-integers/#findComment-725583 Share on other sites More sharing options...
GingerRobot Posted December 29, 2008 Share Posted December 29, 2008 You don't need to use the strcmp() function - just return the difference between the two integers. That said, if this data is coming from a database, why don't you sort in the query? Link to comment https://forums.phpfreaks.com/topic/138763-solved-strcmp-for-integers/#findComment-725587 Share on other sites More sharing options...
Rebelrebellious Posted December 29, 2008 Author Share Posted December 29, 2008 The solution I settled for uses the else clause for times and for other columns. I have discarded the strtotime conversion. My thinking is that the strnatcmp is better than doing strtotime. I really don't know however. The reason I have chosen to sort the results with php is that the array i am sorting is built from a compilation of three seperate queries. Is it possible to execute three queries in a row but return one result set that contains the reults from all three queries? Link to comment https://forums.phpfreaks.com/topic/138763-solved-strcmp-for-integers/#findComment-725647 Share on other sites More sharing options...
GingerRobot Posted December 29, 2008 Share Posted December 29, 2008 Is it possible to execute three queries in a row but return one result set that contains the reults from all three queries? Perhaps you're looking for the UNION sql clause? I'm not entirely sure if that's what you're after from your description though - perhaps you could give us an example. Link to comment https://forums.phpfreaks.com/topic/138763-solved-strcmp-for-integers/#findComment-725687 Share on other sites More sharing options...
Rebelrebellious Posted January 8, 2009 Author Share Posted January 8, 2009 Thank you. UNION worked perfectly. Link to comment https://forums.phpfreaks.com/topic/138763-solved-strcmp-for-integers/#findComment-732518 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.