2007年09月20日

Sorting Model data with usort()

For example, if you want to achive like this:


Controller -------------------
$data1 = $this->Reservation->findAll();
$data2 = $this->AdditionalReservation->findAll();

//If needed, change $data2's array name
"AdditionalReservation" to "Reservation". Then,

$data = array_merge($data1, $data2);

$data = $this->Reservation->sortbytime($data);
$this->set("reservations", $data);
------------------------------

You can use this technique.

Model ------------------------
function myCmp($array1, $array2)
{
$time1 = strtotime($array1['Reservation']['rsv_time']);
$time2 = strtotime($array2['Reservation']['rsv_time']);
if($time1 == $time2){
return 0;
}
return ($time1 > $time2)? 1 : -1;
}

function sortbytime($data)
{
usort($data, array("Reservation", "myCmp"));
return $data;
}
------------------------------
ラベル:tips cakephp Model
posted by SDozono at 11:44| 東京 ☀| Comment(0) | TrackBack(0) | CakePHP Tips | このブログの読者になる | 更新情報をチェックする
×

この広告は180日以上新しい記事の投稿がないブログに表示されております。