cakephp 換頁 排序 搜尋 下拉選單 (完整版)

edited 十月 2013 in CakePHP
############################################
# 文章來源 : bbs.ecstart.com
# 程式作者 : FIEND ([email protected])
# 轉貼文章請註明 文章出處
############################################


晚上睡不著 , 整理了一下 最近 玩 cakephp 的 code .

雖然不把它做為專案用 , 不過拿來玩了一下 .

分享給大家玩看看好了 .

主要功能 :

1. 自動換頁

2. 自動排序

3. 搜尋

4. 群組 db 組成 下拉選單


一般您 開發一個 新增 , 修改 , 刪除 , 搜尋 , 換頁 ,排序 , 下拉 db 選單 需要多久?

以往不是模組化開發 , 這樣從頭寫到尾可能要一二個星期.

小弟以此為例.

教大家在 利用 cakephp + 小弟 自製的 components+helper 五分鐘以內 快速 create 一個 完整的功能.

體驗一下 mvc 下架構的產物可以加速開發專案到什麼程度.

演示 : http://fiend.no-ip.biz/test2 << 我用我家 vmware 寫的... ^^!! 有時連不上 請多包含.

########
[hide]
1. db schema :
-- 資料表格式: `ecstart_groups`
--

CREATE TABLE IF NOT EXISTS `ecstart_groups` (
  `prefixid` varchar(20) NOT NULL default '',
  `id` varchar(32) NOT NULL default 'user',
  `name` varchar(32) NOT NULL default '',
  `seq` int(3) NOT NULL default '60',
  `created` datetime NOT NULL default '0000-00-00 00:00:00',
  `updated` datetime NOT NULL default '0000-00-00 00:00:00',
  PRIMARY KEY  (`prefixid`,`id`)
) ENGINE=MyISAM ;

-- --------------------------------------------------------

--
-- 資料表格式: `ecstart_test2s`
--

CREATE TABLE IF NOT EXISTS `ecstart_test2s` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `title` varchar(50) default NULL,
  `groupid` varchar(32) NOT NULL,
  `body` text,
  `created` datetime default NULL,
  `modified` datetime default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  ;
2. app/controllers/components/ecstart_model.php

這裡我改寫了 一些 model 拉資料的方法 , 讓 controler 可以更便利的拉資料 .
<?php
class EcstartModelComponent extends Object {



        function ecstart_show_me_the_var($ecstart){
                echo "<pre>";
                print_r($ecstart);
                exit ;
        }

        function ecstart_show_me_the_obj($this){
                echo "<pre>";
                print_r($this);
                exit ;
        }




        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_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_field($CALL,$funname){

                $fields_arr = $CALL->getColumnTypes() ;
                if(is_array($fields_arr)){
                        foreach($fields_arr as $fk => $fv){
                                $fields[$fk] = "`".$funname."`.`".$fk."`" ;
                        }
                }

                return $fields ;
        }


        function ecstart_get_one_record($CALL,$field_str){
                $field_arr = explode(",",str_replace("\t","",str_replace("\n","",str_replace(" ","",$field_str))));
                $subquery = array();
                foreach($field_arr as $fv){
                        $fd = explode(":",$fv);
                        if(@$this->;params["url"][$fd[2]] != ""){
                                        $fd[1] = ucfirst($fd[1]);
                                        $subquery[$fd[0]]["`".$fd[1]."`.`".$fd[2]."`"] = $fd[3] ." ".@$fd[4].$this->params["url"][$fd[2]].@$fd[5] ;
                                        //@$search[";status"]["search"][$fd[2]] = $this->params["url"][$fd[2]] ;
                                        //@$search[";status"]["path"]["search"] .= $fd[2].'='.$this->params["url"][$fd[2]] ;
                        }
                        else{
                                $this->ecstart_alert_msg(MSG_SEARCH_ID);
                        }

                }

                if(is_array($subquery)){

                        //print_r($subquery); exit ;

                        $record =
                                   $CALL->find(
                                                        $conditions = $subquery,
                                                        $fields = null,
                                                        $order = null,
                                                        $page = 1,
                                                        $recursive = null
                                   );

                        return  $record ;
                }
                else{
                        return false ;
                }


        }



        function ecstart_record_search($field_str,$params){
                $field_arr = explode(",",str_replace("\t","",str_replace("\n","",str_replace(" ","",$field_str))));
                $search = array();
                foreach($field_arr as $fv){
                        $fd = explode(":",$fv);
                        if(isset($params["url"]["search_".$fd[2]])){
                                if($params["url"]["search_".$fd[2]] != ""){
                                        $fd[1] = ucfirst($fd[1]);
                                        @$search[";subquery"][$fd[0]]["`".$fd[1]."`.`".$fd[2]."`"] = $fd[3] ." ".@$fd[4].$params["url"]["search_".$fd[2]].@$fd[5] ;
                                        @$search[";status"]["search"]["search_".$fd[2]] = $params["url"]["search_".$fd[2]] ;
                                        @$search[";status"]["path"]["search"] .= '&search_'.$fd[2].'='.$params["url"]["search_".$fd[2]] ;
                                }
                        }
                }
                return  $search ;
        }
        function ecstart_get_page_record($CALL,$funname,$search_str,$default_sort='',$params,$relation_str){


                $search_arr = $this->ecstart_record_search($search_str,$params);


                if(!isset($params["url"]["p"])){
                        $params["url"]["p"] = "1";
                }
                $ecstart["params"] = $params ;


                $fields = $this->ecstart_get_field($CALL,$funname);

                $sort_arr = $this->ecstart_record_sort($fields);


                if(@$sort_arr[";subquery"] == ""){ // default sort
                        $sort_arr["subquery"] = $default_sort ;
                }



                $ecstart["record"] =
                                   $CALL->findAll(
                                                        $conditions = @$search_arr[";subquery"] ,
                                                        $fields = null,
                                                        $order = @$sort_arr[";subquery"],
                                                        $limit = MAX_RECORD,
                                                        $page = $params["url"]["p"],
                                                        $recursive = null
                                   );
                $num = $CALL->findCount(@$search_arr[";subquery"]) ;

                //$num = $CALL->getNumRows() ;
                $ecstart["rt"] =  $this->ecstart_get_relation($CALL,$relation_str);

                $ecstart["page"] = $this->ecstart_record_page($params["url"]["p"],MAX_RECORD,MAX_RANGE,$num) ;

                $ecstart["status"]["funname"] = $funname ;
                $ecstart["status"]["search"] = @$search_arr[";status"]["search"];
                $ecstart["status"]["search_path"] = @$search_arr[";status"]["path"]["search"];
                $ecstart["status"]["sort"] = @$sort_arr[";status"]["sort"];
                $ecstart["status"]["sort_path"] = @$sort_arr[";status"]["path"]["sort"];

                //$this->set('ecstart', $ecstart);
                return $ecstart ;
        }

        function ecstart_get_relation($CALL,$relation_str){


                $relation_arr = array();
                $relation = array();
                $where = "";
                $order_by = "";

                //$relation_arr = explode(",",str_replace("\t","",str_replace("\n","",str_replace(" ","",$relation_str))));
                $relation_arr = explode(",",$relation_str);

                foreach($relation_arr as $rk => $rv){
                        $q_arr = explode(":",$rv);

                        foreach($q_arr as $qk => $qv){
                                if($qk == 3){
                                        $q_arr[$qk] = trim(str_replace("\t","",str_replace("\n","",$qv))) ;
                                }
                                elseif($qk == 4){
                                        $q_arr[$qk] = str_replace("\t","",str_replace("\n","",$qv)) ;
                                }
                                else{
                                        $q_arr[$qk] = str_replace("\t","",str_replace("\n","",str_replace(" ","",$qv))) ;

                                }
                        }

                        if($CALL->tablePrefix != ""){
                                $prefix_table = "`".$CALL->tablePrefix;
                                $table_name = $CALL->tablePrefix.$q_arr[0] ;

                        }
                        else{
                                $prefix_table = "`";
                                $table_name = $q_arr[0] ;
                        }

                        if($q_arr[1] != ""){
                                $field_id = "`".$q_arr[1]."` as id ,";
                        }
                        else{
                                echo "ID ERROR!!"; exit ;
                        }


                        if($q_arr[2] != ""){
                                $field_name = "`".$q_arr[2]."` as name ";
                        }
                        else{
                                echo "NAME ERROR!!"; exit ;
                        }

                        if($q_arr[3] != ""){
                                $where = " where ".$q_arr[3];

                        }
                        $query = "SELECT ".$field_id.$field_name." FROM ".$prefix_table.$q_arr[0]."`".$where." ".$q_arr[4] ;
                        //print_r($q_arr) ;
                        //echo $query . "
" ;
                        $relation_arr = $CALL->findBySql($query);

                        foreach($relation_arr as $rk => $rv){
                                //print_r($rv[$table_name]);
                                $relation[$q_arr[0]][$rk] = $rv[$table_name] ;
                        }
                        unset($relation_arr);


                }



                //print_r($relation);




                return $relation ;
        }














}
?>
3. app/views/helpers/ethtml.php

這裡我做了 三個 html helper 的 function 供 版型讀取 .

等一下會 po 出 版型 給大家看 , 就會明白如何使用.
<?php
class EthtmlHelper extends Helper {

    var $helpers = array('Html');

    function fieldsort($name,$field,$url,$ecstart)
    {
        // Use the HTML helper to output
        // formatted data:
        if(@$ecstart[";status"]["sort"][$field] == "desc"){
                $url .= "&sort_".$field."=asc" ;
        }
        else{
                $url .= "&sort_".$field."=desc" ;
        }


        if(@$ecstart[";status"]["sort"][$field] == "desc"){
                $image_path = "<img src='/images/s_desc.png'>" ;
        }
        elseif(@$ecstart[";status"]["sort"][$field] == "asc"){
                $image_path = "<img src='/images/s_asc.png'>" ;

        }
        else{
                $image_path = "" ;
        }
        $url .= "&p=".$ecstart["page"]["thispage"].$ecstart["status"]["search_path"] ;
        //echo "<pre>";         print_r($ecstart["page"]["thispage"]);  exit ;
        unset($ecstart);


        return $this->output("<a href=".$url." class=\"editOuter\">".$name."</a> ".$image_path);
    }
    function select($id,$arr,$searchid="")
    {

        $id_arr = explode('/',$id);
        if(count($id_arr) > 1){
                $name = "data[".$id_arr[0]."][".$id_arr[1]."]";
                $ids = $id_arr[0].$id_arr[1] ;

        }
        else{
                $name = $id ;
                $ids = $id;
        }


        $select_html = "<select name='".$name."' id='".$ids."'>";
        $select_html .= "<option value=''></option>";
        foreach($arr as $av){
                if($searchid == $av["id"]){
                        $select_html .= "<option value='".$av["id"]."' selected>".$av["name"]."</option>";
                }else{
                        $select_html .= "<option value='".$av["id"]."'>".$av["name"]."</option>";
                }
        }

        $select_html .= "</select>";

        return $select_html ;
        // Use the HTML helper to output
        // formatted data:
    }



    function rtvalue($arr,$record_id="")
    {
        $rtvalue = "";
        foreach($arr as $av){
                if($record_id == $av["id"]){
                        $rtvalue = $av["name"];
                }
        }

        return $rtvalue ;
        // Use the HTML helper to output
        // formatted data:
    }







}
?>
4. app/controllers/test2_controller.php

這支程式是 直接套用 剛才秀給大家的二支

helpers => Ethtml
components => EcstartModel

以下為 其語法和用法 :
<?php

class Test2Controller extends AppController {

   var $name = 'Test2';

   var $helpers = array('Html','Ethtml');

   var $components = array('EcstartModel');


   function index() {
                $search_str = "
                                AND:    Test2:  title:          like:   %:      %:      ,
                                AND:    Test2:  id:             =:                      ,
                                AND:    Test2:  groupid:        =:                      ,
                                AND:    Test2:  created:        like:   %:      %:
                ";
                $default_sort = 'order by created desc';

                $relation_str = "
                                groups : id : name : 1=1 : order by seq asc
                ";
                //$search_arr = $this->EcstartModel->ecstart_record_search($search_str);
                $ecstart =  $this->EcstartModel->ecstart_get_page_record($this->Test2,$this->name,$search_str,$default_sort,$this->params,$relation_str);
                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) ;
                }
                $this->set('ecstart', $ecstart);
   }

   function view($id = null)
   {
       $this->Test2->id = $id;
       $this->set('test2', $this->Test2->read());
   }
   function add()
   {

        $relation_str = "
                        groups : id : name : 1=1 : order by seq asc
        ";
        $ecstart["rt"] =  $this->EcstartModel->ecstart_get_relation($this->Test2,$relation_str);

        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) ;
        }
        $this->set('ecstart', $ecstart);

        if (!empty($this->data))
        {

            if ($this->Test2->save($this->data))
            {
                //$this->flash('Your post has been saved.','/test2');
                $this->redirect('/test2');
            }
        }

   }
   function edit($id = null)
   {


       if (empty($this->data))
       {


           $this->Test2->id = $id;
           $this->data = $this->Test2->read();


                $relation_str = "
                                groups : id : name : 1=1 : order by seq asc
                ";
                $ecstart["rt"] =  $this->EcstartModel->ecstart_get_relation($this->Test2,$relation_str);

                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) ;
                }
                $this->set('ecstart', $ecstart);





       }
       else
       {
           //print_r($this->data);        exit ;
           if ($this->Test2->save($this->data['Test2']))
           {
               //$this->flash('Your post has been updated.','/test2');
                $this->redirect('/test2');
           }
       }



   }
   function delete($id)
   {
        $this->Test2->del($id);
        $this->redirect('/test2');
        //$this->flash('The post with id: '.$id.' has been deleted.', '/test2');
   }



}
?>
5. app/views/test2/index.thtml
        <!--sitemap start-->
        <table width="100%"  border="0" cellspacing="0" cellpadding="5" align="center">
          <tr>
            <td><span class="site_map">
                        <a href=".">首頁</a>
                        >>
                        <a href="#">SIMAP TEST</a>
                        >>
                        <a href="/test">TEST ITEM</a>

                                    </span></td>
          </tr>
        </table>
        <!--sitemap end-->
        <table width="100%"  border="0" cellspacing="0" cellpadding="1" align="center">
          <tr><td bgcolor="#E5E5E5">
            <table width="100%"  border="0" cellspacing="0" cellpadding="0">
              <tr><td height="28" bgcolor="#F7F7F7">
                    <table width="100%"  border="0" cellpadding="0" cellspacing="0">

                      <tr><td height="24">
                        <!-- search start -->
                                   <table border="0">
                                        <form name="search" action="." method="get">
                                        <tr><td>
                                        <input type="hidden" name="url" value="<?=$ecstart["params"]["url"]["url"];?>">

                                          
                                        群組 :
                                        <?= $ethtml->select("search_groupid",$ecstart["rt"]["groups"],@$ecstart[";status"]["search"]["search_groupid"]);?>

                                          
                                        標題 : <input type="text" name="search_title" value="<?=@$ecstart[";status"]["search"]["search_title"]?>">
                                          
                                        新增時間 : <input type="text" name="search_created" value="<?=@$ecstart[";status"]["search"]["search_created"]?>">

                                          
                                        <input type="submit" value="搜尋" class="input_type">
                                        </td></tr>
                                   </form></table>
                        <!-- search end -->

                        </td>
                        <td><div align="right">資料共 : [<font color=red><?=$ecstart["page"]["total"]?></font>] 筆,總頁數 : [<font color=red><?=$ecstart["page"]["lastpage"]?></font>] 頁 </div></td>

                      </tr>
                    </table>

              </td></tr>
          </table></td></tr>
          <tr><td height="5"></td></tr>
        </table>
        



        <form name="list" action="./?action=module&fun=pay_range&permit=other" method="post">

        <table width="100%"  border="0" cellpadding="0" cellspacing="0" align="center">
          <tr>
            <td bgcolor="#999999"><table width="100%"  border="0" cellspacing="1" cellpadding="0">
                <tr>
                  <td height="29" valign="bottom" background="/images/PIC_26.gif">
                    <table width="150"  border="0" cellspacing="0" cellpadding="0">
                      <tr>
                        <td width="7%"> </td>
                        <td width="93%"><font color="#444444" size="2"><b>Test</b></font></td>

                      </tr>
                      <tr>
                        <td height="5"></td>
                        <td></td>
                      </tr>
                  </table></td>
                </tr>
                <tr>
                  <td><table width="100%"  border="0" cellspacing="0" cellpadding="0">

                      <tr><td bgcolor="#FFFFFF">
                        <!-- record start -->
                            <table width="100%"  border="0" cellpadding="0" cellspacing="0" class="record_table">
                                <input type="hidden" name="list">
                                <input type="hidden" name="edit">
                                <input type="hidden" name="editlist" value="editlist">
                                <tr><td height="1" colspan="100"><img src="/images/space.gif" width="8" height="1"></td></tr>

                                <tr height="20">

                                  <TH width="3%" height="24" background="/images/PIC_34.gif" class="table_TH"> </TH>

                                  <TH width="3%" height="24" background="/images/PIC_34.gif" class="table_TH"> </TH>



                                  <th background="/images/PIC_34.gif" class="table_TH">No</th>

                                  <th background="/images/PIC_34.gif" class="table_TH">
                                      <?= $ethtml->fieldsort("ID","id","/".$ecstart["params"]["url"]["url"],$ecstart);?>
                                  </th>

                                  <th background="/images/PIC_34.gif" class="table_TH">
                                      <?= $ethtml->fieldsort("群組","groupid","/".$ecstart["params"]["url"]["url"],$ecstart);?>
                                  </th>


                                  <th background="/images/PIC_34.gif" class="table_TH">
                                      <?= $ethtml->fieldsort("標題","title","/".$ecstart["params"]["url"]["url"],$ecstart);?>
                                  </th>
                                  <th background="/images/PIC_34.gif" class="table_TH">
                                      <?= $ethtml->fieldsort("新增時間","created","/".$ecstart["params"]["url"]["url"],$ecstart);?>
                                  </th>

                                  <TH width="50%" height="24" background="/images/PIC_34.gif" class="table_TH"> </TH>


                                </tr>
                                <!-- record block start -->

                                <?php foreach ($ecstart["record"] as $rid => $record): ?>
                                <tr>



                                  <td height="20" align="center">
                                        <a href="<?='/'.$ecstart["params"]["url"]["url"].'/delete/'.$record[$ecstart["status"]['funname']]['id']?>">
                                        <img src="/images/b_drop.png" border=0 alt="刪除">
                                        </a>
                                  </td>


                                  <td height="20" align="center">

                                        <a href="<?='/'.$ecstart["params"]["url"]["url"].'/edit/'.$record[$ecstart["status"]['funname']]['id']?>">
                                        <img src="/images/b_edit.png" border=0 alt="編輯">
                                        </a>
                                  </td>




                                  <td height="20"><?=$rid+1?></td>
                                  <td height="20"><?php echo $record[$ecstart["status"]['funname']]["id"]; ?></td>
                                  <td height="20">
                                        <?= $ethtml->rtvalue($ecstart["rt"]["groups"],@$record[$ecstart[";status"]['funname']]["groupid"])?>
                                  </td>
                                  <td height="20">
                                      <?php echo $html->link($record[$ecstart["status"]['funname']]['title'], '/'.$ecstart["params"]["url"]["url"].'/view/'.$record[$ecstart["status"]['funname']]['id']);?>
                                  </td>
                                  <td height="20"><?php echo $record[$ecstart["status"]['funname']]['created']; ?></td>

                                  <td height="20"> </td>
                                </tr>
                                <?php endforeach; ?>
                           </table>
                        <!-- record end -->

                      </td></tr>
                  </table></td>
                </tr>
            </table></td>
          </tr>
        </table>
        <table width="100%"  border="0" cellspacing="0" cellpadding="0" align="center">
          <tr>

            <td width="41%">
                <!--BUTTON-->
                <input type="button" class="button" value="新增" onclick="goto_url('<?='/'.$ecstart["params"]["url"]["url"].'/add'?>');">
            </td>
            <td width="8%"><div align="right"><img src="/images/PIC_38.gif" width="55" height="24" alt=""></div></td>
            <td width="51%" background="/images/PIC_39.gif"><table width="95%"  border="0" align="center" cellpadding="0" cellspacing="0">
              <tr><td>

                        <div align="right"><span class="gary-12">

                        <!--page block start-->
                                <?php echo $this->renderElement('ecstart_record_page'); ?>
                        <!--page block end-->

              </span></div></td></tr>
            </table></td>
          </tr>
        </table>
        </form>
</table>
6. vim app/views/test2/add.thtml
<table width="100%" bgcolor="#999999" border="0" cellpadding="5" cellspacing="1" align="center">
         <tr>
           <td background="/images/PIC_26.gif" height="29" colspan="2">
                <b>新增資料</b>
           </td>
         </tr>

        <!-- sitemap edit -->
        <!-- edit start -->
                   <form name="add" action="/test2/add" method="post" onSubmit="return checkData();">



                             <tr bgcolor="#EEEEEE">
                               <td width="25%" height="25" valign="top" align="left">Group : </td>
                               <td height="25" align="left">
                                        <?= $ethtml->select("Test2/groupid",$ecstart["rt"]["groups"]);?>
                                </td>
                             </tr>



                             <tr bgcolor="#EEEEEE">
                               <td width="25%" height="25" valign="top" align="left">Title : </td>
                               <td height="25" align="left">
                                        <?php echo $html->input('Test2/title', array('size' => '40'))?>
                                        <?php echo $html->tagErrorMsg('Test2/title', 'Title is required.') ?>
                                </td>
                             </tr>


                             <tr bgcolor="#EEEEEE"><td height="25" valign="top" align="left">Body : </td>
                               <td height="25" align="left">
                                        <?php echo $html->textarea('Test2/body', array('rows'=>'8','cols'=>'25')) ?>
                                        <?php echo $html->tagErrorMsg('Test2/body', 'Body is required.') ?>
                               </td>
                             </tr>



                    <tr bgcolor="#EEEEEE">
                        <td></td>
                        <td height="40">
                                <input type="image" name="submit" src="/images/30.gif">
                        </td>
                     </tr>

            </form>
        <!-- edit end -->
           </td>
          </tr>
        </table>
7. app/views/test2/edit.thtml
<table width="100%" bgcolor="#999999" border="0" cellpadding="5" cellspacing="1" align="center">
         <tr>
           <td background="/images/PIC_26.gif" height="29" colspan="2">
                <b>新增資料</b>
           </td>
         </tr>

        <!-- sitemap edit -->
        <!-- edit start -->
                   <form name="edit" action="/test2/edit" method="post" onSubmit="return checkData();">

                        <?php echo $html->hidden('Test2/id'); ?>

                             <tr bgcolor="#EEEEEE">
                               <td width="25%" height="25" valign="top" align="left">Group : </td>
                               <td height="25" align="left">
                                        <?= $ethtml->select("Test2/groupid",$ecstart["rt"]["groups"],$this->data["Test2"]["groupid"]);?>
                                </td>
                             </tr>


                        <tr bgcolor="#EEEEEE">
                          <td width="25%" height="25" valign="top" align="left">Title : </td>
                          <td height="25" align="left">
                                <?php echo $html->input('Test2/title', array('size' => '40'))?>
                                <?php echo $html->tagErrorMsg('Test2/title', 'Title is required.') ?>
                           </td>
                        </tr>
                        <tr bgcolor="#EEEEEE"><td height="25" valign="top" align="left">Body : </td>
                          <td height="25" align="left">
                                <?php echo $html->textarea('Test2/body', array('rows'=>'8','cols'=>'25')) ?>
                                <?php echo $html->tagErrorMsg('Test2/body', 'Body is required.') ?>
                          </td>
                        </tr>


                    <tr bgcolor="#EEEEEE">
                        <td></td>
                        <td height="40">
                                <input type="image" name="submit" src="/images/30.gif">
                        </td>
                     </tr>

            </form>
        <!-- edit end -->
           </td>
          </tr>
        </table>
[/hide]

原始討論: http://twpug.net/x/modules/newbb/viewtopic.php?topic_id=3137

評論

  • edited 一月 2008
    PO 完了 ^^!! 大烏賊 又來 了.....

    有疑問或是想討論的.

    可以在這或是 ecstart 與本人討論 ^^
  • edited 一月 2008
    有 bug 修正一下 :

    請更新這二支 method
            function ecstart_record_sort($field_arr,$params){
                    //echo "<pre>"; echo $field_list; print_r($field_arr); exit;
                    foreach($field_arr as $fv){
    
                            $fvs = explode('.',$fv);
                            if(isset($params["url"]["sort_".str_replace('`','',$fvs[1])])){
                                    //print_r($this->params["url"]["sort_id"]);
                                    $sort["subquery"]  = $fv." ".$params["url"]["sort_".str_replace('`','',$fvs[1])];
                                    $sort["status"]["path"]["sort"] = '&sort_'.str_replace('`','',$fvs[1]).'='.$params["url"]["sort_".str_replace('`','',$fvs[1])]  ;
                                    $sort["status"]["sort"][str_replace('`','',$fvs[1])] = $params["url"]["sort_".str_replace('`','',$fvs[1])];
                            }
    
                    }
                    if(isset($sort)){
                            return  $sort ;
                    }
            }
    
    
    
            function ecstart_get_page_record($CALL,$funname,$search_str,$default_sort='',$params,$relation_str){
    
    
                    $search_arr = $this->ecstart_record_search($search_str,$params);
    
    
                    if(!isset($params["url"]["p"])){
                            $params["url"]["p"] = "1";
                    }
                    $ecstart["params"] = $params ;
    
    
                    $fields = $this->ecstart_get_field($CALL,$funname);
    
                    $sort_arr = $this->ecstart_record_sort($fields,$params);
    
    
                    if(@$sort_arr[";subquery"] == ""){ // default sort
                            $sort_arr["subquery"] = $default_sort ;
                    }
    
    
    
                    $ecstart["record"] =
                                       $CALL->findAll(
                                                            $conditions = @$search_arr[";subquery"] ,
                                                            $fields = null,
                                                            $order = @$sort_arr[";subquery"],
                                                            $limit = MAX_RECORD,
                                                            $page = $params["url"]["p"],
                                                            $recursive = null
                                       );
                    $num = $CALL->findCount(@$search_arr[";subquery"]) ;
    
                    //$num = $CALL->getNumRows() ;
                    $ecstart["rt"] =  $this->ecstart_get_relation($CALL,$relation_str);
    
                    $ecstart["page"] = $this->ecstart_record_page($params["url"]["p"],MAX_RECORD,MAX_RANGE,$num) ;
    
                    $ecstart["status"]["funname"] = $funname ;
                    $ecstart["status"]["search"] = @$search_arr[";status"]["search"];
                    $ecstart["status"]["search_path"] = @$search_arr[";status"]["path"]["search"];
                    $ecstart["status"]["sort"] = @$sort_arr[";status"]["sort"];
                    $ecstart["status"]["sort_path"] = @$sort_arr[";status"]["path"]["sort"];
    
                    //$this->set('ecstart', $ecstart);
                    return $ecstart ;
            }
    
    
  • edited 一月 2008
    機車~~我少給大家一個元件 :

    老闆~~一般 user 不能改文章真不方便.....


    這是 給 index.thtml 吃的 page 區塊 .

    7. app/views/elements/ecstart_record_page.thtml
    <table>
    <tr>
     <td>
    
    <?php
    echo $html->link('最前頁', '/'.$ecstart["params"]["url"]["url"].'&p='.$ecstart["page"]["firstpage"].$ecstart["status"]["sort_path"].$ecstart["status"]["search_path"]);echo " | ";
    echo $html->link('上一頁', '/'.$ecstart["params"]["url"]["url"].'&p='.$ecstart["page"]["previouspage"].$ecstart["status"]["sort_path"].$ecstart["status"]["search_path"]);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 "&nbsp;" ;
                            echo "&nbsp;" ;
                    }
                    else{
                            echo "".$html->link($pv['p'], '/'.$ecstart["params"]["url"]["url"].'&p='.$pv['p'].$ecstart["status"]["sort_path"].$ecstart["status"]["search_path"])."";
                            echo "&nbsp;" ;
                            echo "&nbsp;" ;
                    }
            }
    }
    echo " | ";
    echo $html->link('下一頁', '/'.$ecstart["params"]["url"]["url"].'&p='.$ecstart["page"]["nextpage"].$ecstart["status"]["sort_path"].$ecstart["status"]["search_path"]);echo " | ";
    echo $html->link('最終頁', '/'.$ecstart["params"]["url"]["url"].'&p='.$ecstart["page"]["lastpage"].$ecstart["status"]["sort_path"].$ecstart["status"]["search_path"]);
    ?>
    
     </td>
    </tr>
    </table>
    
  • edited 一月 2008
    感謝提供,不過還沒時間仔細瞧,只知道是為 CakePHP 1.1x 版設計,我目前在使用的是 1.2x 版。

    調整了一下權限,應該發表文章後會有一段時間能夠編輯或刪除文章。 :)
  • edited 一月 2008
    你誤會了說... 我沒有特別針對 什麼版本做設計 .

    而且這次 我特別用

    components + HELPER 的方式 將它獨立的模組化出來 .

    它的 主要功能是在

    換頁 , 排序 , 搜尋 , 下拉 RELATION TABLE 的加強 .

    1.1 和 1.2 都可以用.
  • edited 一月 2008
    因為 views 跟 elements 的副檔名, 1.1 為 thtml ,1.2 為 ctp

    只是純粹這樣子分,並沒有細看程式碼 :)
  • edited 一月 2008
    原來 呵呵~

    ~~改一下檔名應該就可以用了.
Sign In or Register to comment.