Jump to content

[SOLVED] strcmp for integers?


Rebelrebellious

Recommended Posts

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

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.

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?

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.

 

 

  • 2 weeks later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.