###############################################
# 資料來源 : bbs.ecstart.com
# 作者 : FIEND
# 轉貼請註明出處
###############################################
最近太無聊了 花了一星期玩了一下 cakephp ^^bb
果然是一套有趣的大玩具 .
雖然功能很弱看起來還沒有很成熟 .
不過蠻適合做小專案的 , create 程式的速度超快 ^^
官方並沒有出 cake php 的換頁和排序 method .
而 1.2 版有出 但好像也不怎麼好用.
放棄~~~
所以後來想想還是自己寫 比較好用..
1.2 以下的使用者 苦腦怎麼實現 換頁 排序 功能的 或是覺得官方預設的不夠強 .
可以抄小弟寫好的 回家用 下星期有空我再加上 搜尋功能
^^
cakephp 換頁 排序 (第一版) :
1. create tests table :
CREATE TABLE IF NOT EXISTS `ecstart_tests` (
`id` int(10) unsigned NOT NULL auto_increment,
`title` varchar(50) default NULL,
`body` text,
`created` datetime default NULL,
`modified` datetime default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=35 ;
2. vim cake/app_controller.php << 你也可以將其放在 app/controllers/components :
首先將 這些寫好的 method 放進 app_controler class 內 .
function ecstart_record_page($p , $max_page ,$max_range , $rows){
if(!isset($p)){$p = "" ;}
if(trim($p)=="" or $p < 1){$p = 1 ; }
$lastpage = ceil($rows/$max_page);
if($p > $lastpage){ $p=1 ;}
$rec_start= ($p-1)*$max_page +1;
$rec_end = $rec_start + $max_page -1;
$ploops = floor(($p-1)/$max_range)*$max_range + 1 ;
$ploope = $ploops + $max_range -1;
if($ploope >= $lastpage){ $ploope=$lastpage;}
$ppg = $p - 1 ;
$npg = $p + 1 ;
if($ppg<= 0) $ppg=$lastpage;
if($npg > $lastpage) $npg=1;
if($rec_end > $rows) $rec_end=$rows;
$ecstart_page["rec_start"] = $rec_start ;
$ecstart_page["rec_end"] = $rec_end ;
$ecstart_page["firstpage"] = 1 ;
$ecstart_page["lastpage"] = $lastpage ;
$ecstart_page["previousrange"] = $ploops - $max_range ;
$ecstart_page["nextrange"] = $ploops + $max_range ;
$ecstart_page["previouspage"] = $ppg ;
$ecstart_page["nextpage"] = $npg ;
$ecstart_page["thispage"] = $p ;
$ecstart_page["total"] = $rows ;
$ecstart_page["loop"] = $lastpage+1 ;
for($i=$ploops;$i <= $ploope;$i++){
$ecstart_page["item"][]["p"] = $i ;
}
return $ecstart_page;
}
function ecstart_show_me_the_var($ecstart){
echo "<pre>";
print_r($ecstart);
exit ;
}
function ecstart_show_me_the_obj(){
echo "<pre>";
print_r($this);
exit ;
}
function ecstart_record_sort($field_arr){
//echo "<pre>"; echo $field_list; print_r($field_arr); exit;
foreach($field_arr as $fv){
$fvs = explode('.',$fv);
if(isset($this->params["url"]["sort_".str_replace('`','',$fvs[1])])){
//print_r($this->params["url"]["sort_id"]);
$sort["subquery"] = $fv." ".$this->params["url"]["sort_".str_replace('`','',$fvs[1])];
$sort["status"]["path"]["sort"] = '&sort_'.str_replace('`','',$fvs[1]).'='.$this->params["url"]["sort_".str_replace('`','',$fvs[1])] ;
$sort["status"]["sort"][str_replace('`','',$fvs[1])] = $this->params["url"]["sort_".str_replace('`','',$fvs[1])];
}
}
if(isset($sort)){
return $sort ;
}
}
function ecstart_get_page_record($CALL,$funname){
if(!isset($this->params["url"]["p"])){
$this->params["url"]["p"] = "1";
}
$ecstart["params"] = $this->params ;
$fields_arr = $CALL->getColumnTypes() ;
if(is_array($fields_arr)){
foreach($fields_arr as $fk => $fv){
$fields[$fk] = "`".$funname."`.`".$fk."`" ;
}
}
$ecstart["sort"] = $this->ecstart_record_sort($fields);
$ecstart["record"] =
$CALL->findAll(
$conditions = null,
$fields = null,
$order = $ecstart["sort"]["subquery"],
$limit = MAX_RECORD,
$page = $this->params["url"]["p"],
$recursive = null
);
$num = $CALL->findCount() ;
$ecstart["page"] = $this->ecstart_record_page($this->params["url"]["p"],MAX_RECORD,MAX_RANGE,$num) ;
$ecstart["status"]["funname"] = $funname ;
if(isset($this->params["url"]["show_me_the_var"])){
$this->ecstart_show_me_the_var($ecstart) ;
}
if(isset($this->params["url"]["show_me_the_obj"])){
$this->ecstart_show_me_the_obj() ;
}
$this->set('ecstart', $ecstart);
}
3. 呼叫 ecstart_get_page_record method :
app/controllers/test_controller.php
class TestController extends AppController {
var $name = 'Test';
var $helpers = array('Html','Ecstartsort'); // << 這是 排序的 helper 版型邏輯 未來要給 標題使用的 稍後再跟大家介紹 helper 相信己玩過 cakephp的人不漠生 .
function index() {
$this->ecstart_get_page_record($this->Test,$this->name); // << 呼叫先前寫好的 換頁和 排序 邏輯 .
}
function add()
{
if (!empty($this->data))
{
if ($this->Test->save($this->data))
{
$this->flash('Your post has been saved.','/test');
//$this->redirect('/test');
}
}
}
function edit($id = null)
{
if (empty($this->data))
{
$this->Test->id = $id;
$this->data = $this->Test->read();
}
else
{
//print_r($this->data); exit ;
if ($this->Test->save($this->data['Test']))
{
$this->flash('Your post has been updated.','/test');
}
}
}
function delete($id)
{
$this->Test->del($id);
$this->flash('The post with id: '.$id.' has been deleted.', '/test');
}
}
4. SORTHELPER :
這是 要給所有版型 可以呼叫用的 helper method , 您可以參考 3. 小弟 的註解
主要的功能是使標題的排序 .
vim app/views/helpers/ecstartsort.php
<?php
class EcstartsortHelper extends Helper {
var $helpers = array('Html');
function fieldsort($name,$field,$url,$ecstart)
{
// Use the HTML helper to output
// formatted data:
if(@$ecstart["sort"]["status"]["sort"][$field] == "desc"){
$url .= "&sort_".$field."=asc" ;
}
else{
$url .= "&sort_".$field."=desc" ;
}
if(@$ecstart["sort"]["status"]["sort"][$field] == "desc"){
$image_path = "<img src='/images/s_desc.png'>" ;
}
elseif(@$ecstart["sort"]["status"]["sort"][$field] == "asc"){
$image_path = "<img src='/images/s_asc.png'>" ;
}
else{
$image_path = "" ;
}
$url .= "&p=".$ecstart["page"]["thispage"] ;
//echo "<pre>"; print_r($ecstart["page"]["thispage"]); exit ;
unset($ecstart);
return $this->output("<a href=".$url." class="editOuter">".$name."</a> ".$image_path);
}
}
?>
5. 換頁區塊 :
因為換頁的區塊一般都長一樣 , 所以小弟將換頁 做成 區塊讓所有的版型可以去呼叫它
vim app/views/elements/ecstart_record_page.thtml
<table>
<tr>
<td>
<?php
echo $html->link('最前頁', '/test&p='.$ecstart["page"]["firstpage"].$ecstart["sort"]["status"]["path"]["sort"]);echo " | ";
echo $html->link('上一頁', '/test&p='.$ecstart["page"]["previouspage"].$ecstart["sort"]["status"]["path"]["sort"]);echo " | ";
if(is_array($ecstart["page"]["item"])){
foreach($ecstart["page"]["item"] as $pv){
if($ecstart["params"]["url"]["p"] == $pv['p']){
echo "<b>[ ".$pv['p']." ]</b>";
echo " " ;
echo " " ;
}
else{
echo "".$html->link($pv['p'], '/test&p='.$pv['p'].$ecstart["sort"]["status"]["path"]["sort"])."";
echo " " ;
echo " " ;
}
}
}
echo " | ";
echo $html->link('下一頁', '/test&p='.$ecstart["page"]["nextpage"].$ecstart["sort"]["status"]["path"]["sort"]);echo " | ";
echo $html->link('最終頁', '/test&p='.$ecstart["page"]["lastpage"].$ecstart["sort"]["status"]["path"]["sort"]);
?>
</td>
</tr>
</table>
6. record 頁的 換頁 排序 版型 :
<table>
<tr>
<th>No</th>
<th>
<?= $ecstartsort->fieldsort("ID","id","/test",$ecstart);?> <!-- 利用 helper 寫好的 展示層排序邏輯 直接呼叫 method 即可組合 連結 -->
</th>
<th>
<?= $ecstartsort->fieldsort("標題","title","/test",$ecstart);?>
</th>
<th>
<?= $ecstartsort->fieldsort("新增時間","created","/test",$ecstart);?>
</th>
<th>Update</th>
<th>Delete</th>
</tr>
<!-- Here's where we loop through our $posts array, printing out post info -->
<?PHP //echo "<pre>"; print_r($test); exit ; ?>
<?php foreach ($ecstart["record"] as $rid => $record): ?> <!-- // 由 test_controler 去呼叫 ecstart_get_page_record 的 record 陣列再由這裡去 做 foreach -->
<tr>
<td><?php echo $rid+1; ?></td>
<td><?php echo $record[$ecstart["status"]['funname']]["id"]; ?></td>
<td>
<?php echo $html->link($record[$ecstart["status"]['funname']]['title'], '/test/view/'.$record[$ecstart["status"]['funname']]['id']);?>
</td>
<td><?php echo $record[$ecstart["status"]['funname']]['created']; ?></td>
<td>
<?php echo $html->link('Edit', '/test/edit/'.$record[$ecstart["status"]['funname']]['id']);?>
</td>
<td>
<?php echo $html->link(
'Delete',
"/test/delete/{$record[$ecstart["status"]['funname']]['id']}",
null,
'Are you sure?'
)?>
</td>
</tr>
<?php endforeach; ?>
</table>
<?php echo $this->renderElement('ecstart_record_page'); ?> <!-- // 這裡就是將 5. 做好的 區塊版型抓進來用 -->
<p><?php echo $html->link('Add Post', '/test/add'); ?></p>
對了這裡是完成的 程式 範例 用我家的 vmware 架的 linux 有時會連不上請多包含:
[url]http://fiend.no-ip.biz/test/[/url]
^^ po 完了 大家慢慢消化吧 .
fiend 上