天天看點

zendframe --實作CURD

1、zend入口index.php檔案

<?php

 error_reporting(E_ALL|E_STRICT); //在開啟錯誤報告

 date_default_timezone_set('Asia/Shanghai'); //配置地區 

 set_include_path('.' .PATH_SEPARATOR .'./library'

  .PATH_SEPARATOR .'./application/models/'

  . PATH_SEPARATOR . './application/controllers/' 

       . PATH_SEPARATOR . './application/views/' 

  .PATH_SEPARATOR . get_include_path());  //配置環境路徑

 // require_once 'Zend/Loader.php';

 // Zend_Loader::registerAutoload();//設定Zend Framework 自動載入類檔案

 require_once "Zend/Loader/Autoloader.php";  //載入zend架構

 Zend_Loader_Autoloader::getInstance()->setFallbackAutoloader(true); //靜态載入自動類檔案

 $registry = Zend_Registry::getInstance(); //靜态獲得執行個體

 $view = new Zend_View(); //執行個體化zend 模闆

 $view->setScriptPath('./application/views/scripts/');//設定模闆顯示路徑

 $registry['view'] = $view;//注冊View

 //配置資料庫參數,并連接配接資料庫 

 $config=new Zend_Config_Ini('./application/configs/config.ini',null, true);

 Zend_Registry::set('config',$config);//注冊

 $dbAdapter=Zend_Db::factory($config->general->db->adapter,$config->general->db->config->toArray());//建立zend_db_adapter對象

 $dbAdapter->query('SET NAMES UTF8');//調用adapter的query方法,設定資料庫擷取的資料與浏覽器同步

 Zend_Db_Table::setDefaultAdapter($dbAdapter);//設定預設的擴充卡

 Zend_Registry::set('dbAdapter',$dbAdapter);

 //設定控制器

 $frontController =Zend_Controller_Front::getInstance();

 $frontController->setBaseUrl('/zendframe')//設定基本路徑

     ->setParam('noViewRenderer', true)

     ->setControllerDirectory('./application/controllers')

     ->throwExceptions(true)

     ->dispatch();    //run

?>

2、config.ini

[general]

db.adapter = PDO_MYSQL

db.config.host = localhost

db.config.username = root

db.config.password =

db.config.dbname = zend

3、運用到的資料表定義message.php

<?php

class Message extends Zend_Db_Table

{

 protected $_name = "message";

 protected $_primary = 'id';

}

?>

4、控制器檔案IndexController.php

<?php

class IndexController extends Zend_Controller_Action

{

    public function init()

    {

     $this->registry = Zend_Registry::getInstance();

        $this->view = $this->registry['view'];

        $this->view->baseUrl = $this->_request->getBaseUrl();

    }

 function indexAction()

    {

     $message=new message();//執行個體化資料庫類

        //擷取資料庫内容

        $this->view->messages=$message->fetchAll()->toArray();  

        echo $this->view->render('index.html');//顯示模版

    }

     function addAction(){

     //如果是POST過來的值.就增加.否則就顯示增加頁面

     if(strtolower($_SERVER['REQUEST_METHOD'])=='post'){

   $content=$this->_request->getPost('content');

   $title=$this->_request->getPost('title');

   $message=new Message();

   $data=array(

     'content'=>$content,

     'title'=>$title

   );

   $message->insert($data);

   unset($data);//删除data變量

   echo '您增加資料成功!請您<a href="'.$this->view->baseUrl.'/index/index/" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" >傳回</a>';

     }else{

      echo $this->view->render('add.html');//顯示增加模版

     }

    }

    function editAction(){

     $message = new message();

     $db = $message->getAdapter();

     if(strtolower($_SERVER['REQUEST_METHOD'])=='post'){

      $content=$this->_request->getPost('content');

      $title = $this->_request->getPost('title');

      $id = $this->_request->getPost('id');

      $set = array(

       'content'=>$content,

       'title'=>$title       

      );      

      $where = $db->quoteInto('id=?',$id);

      $message->update($set,$where);

      unset($set);

      echo $content;

      echo $title;

      echo '修改成功<a href="'.$this->view->baseUrl.'/index/index/" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" >傳回</a>';

     }else{

      $id = $this->_request->getParam('id');//擷取url所傳遞的變量id

      $this->view->message = $message->fetchAll('id='.$id)->toArray();

      echo $this->view->render('edit.html');

     }        

    }

    function deleteAction(){

     $message = new message();

     $db = $message->getAdapter();

     $id = (int)$this->_request->getParam('id');

     if($id>0){

      $where = 'id = '.$id;

      $message->delete($where);

     }

     echo '删除成功<a href="'.$this->view->baseUrl.'/index/index" target="_blank" rel="external nofollow" >傳回</a>';

    }

}

 5、前台頁面index.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>增加資料</title>

</head>

<body>

<a href="<?php echo $this->baseUrl ?>/index/add/" target="_blank" rel="external nofollow" >增加資料</a>

<table width="500px">

 <thead>

   <tr>

     <th class="title" colspan="2">資料清單顯示</th>

   </tr>

 </thead>

 <tbody>

 <tr>

     <th style="width:25%;">标題</th>

  <th>内容</th>

 </tr>

 <?foreach($this->messages as $message): ?>

    <tr>

     <th><?php echo $message['title']; ?></th>

  <td><?php echo $message['content']; ?></td>

  <td><a href="<?php echo $this->baseUrl ?>/index/edit/id/<?=$message['id'];?>" target="_blank" rel="external nofollow" >修改資料</a>/

   <a href="<?php echo $this->baseUrl ?>/index/delete?id=<?=$message['id'];?>" target="_blank" rel="external nofollow" >删除</a>

  </td>

   </tr>

   <?endforeach; ?>

   </tbody>

 </table>

</body>

</html>

6、add.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>增加資料</title>

</head>

<body>

<form method="post"action="<?php echo $this->baseUrl; ?>/index/add/">

<table>

 <thead>

   <tr>

     <th class="title" colspan="2">增加資料</th>

   </tr>

 </thead> 

 <tbody>     

   <tr>

     <th style="width:25%;">标題</th>

  <td><input type="text" name="title" id="title" value="" size="20"/> <span class="error">*</span></td>

   </tr>

   <tr>

     <th>内容</th>

  <td><textarea name="content" id="content" col="40" row="4"></textarea><span class="error">*</span></td>

   </tr>

     <tr>

     <th colspan="2"><input type="submit" value="增加資料" class="cmd" /></th>

   </tr>

   </tbody>

 </table>

</form>

</body>

</html>

 7、edit.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>編輯頁面</title>

</head>

<body>

<form method="post" action="<?php echo $this->baseUrl ?>/index/edit/">

<table>

 <thead>

   <tr>

     <th class="title" colspan="2">修改資料</th>

   </tr>

 </thead> 

 <? foreach($this->message as $message):?>

 <tbody>     

   <tr>

     <th style="width:25%;">标題</th>

  <td><input type="text" name="title" id="title" value="<?=$message['title'];?>" size="20"/> <span class="error">*</span></td>

   </tr>

   <tr>

     <th>内容</th>

  <td><textarea name="content" id="content" cols="40" rows="4" ><?=$message['content'];?></textarea><span class="error">*</span></td>

   </tr>

   <input type="hidden" id="id" name='id' value="<?=$message['id'];?>" />    

   <tr>

     <th colspan="2"><input type="submit" value="修改資料" class="cmd" /></th>

   </tr>

   </tbody>

   <? endforeach;?>

 </table>

</form>

</body>

</html>