国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開(kāi)發(fā) > PHP > 正文

zend framework 實(shí)例

2024-05-04 23:06:37
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
index.php 頁(yè)面
  1. <?php  
  2.  error_reporting(E_ALL|E_STRICT); //在開(kāi)啟錯(cuò)誤報(bào)告  
  3.  date_default_timezone_set('Asia/Shanghai');//配置地區(qū)  
  4.  set_include_path('.' .PATH_SEPARATOR.'./library'.PATH_SEPARATOR .'./application/models/'.PATH_SEPARATOR. get_include_path());  //配置環(huán)境路徑  
  5.  require_once"Zend/Loader/Autoloader.php";  //載入zend框架  
  6.  Zend_Loader_Autoloader::getInstance()->setFallbackAutoloader(true);//靜態(tài)載入自動(dòng)類(lèi)文件  
  7.  $registry = Zend_Registry::getInstance();//靜態(tài)獲得實(shí)例  
  8.  $view = new Zend_View(); //實(shí)例化zend 模板  
  9.  $view->setScriptPath('./application/views/scripts/');//設(shè)置模板顯示路徑  
  10.  $registry['view'] = $view;//注冊(cè)View  
  11.  //  設(shè)置數(shù)據(jù)庫(kù)連接  
  12.  $config=newZend_Config_Ini('./application/config/config.ini',null,true);  
  13.  Zend_Registry::set('config',$config);  
  14.  $dbAdapter=Zend_Db::factory($config->general->db->adapter,$config->general->db->config->toArray());  
  15.  $dbAdapter->query('SET NAMESutf8');  
  16.  Zend_Db_Table::setDefaultAdapter($dbAdapter);  
  17.  Zend_Registry::set('dbAdapter',$dbAdapter);  
  18.    
  19.  //  設(shè)置控制器  
  20.  $frontController=Zend_Controller_Front::getInstance();  
  21.  $frontController->setBaseUrl('/zendframework')//設(shè)置基本路徑,這里面是你的項(xiàng)目的名字  
  22.      ->setParam('noViewRenderer',true)  
  23.      ->setControllerDirectory('./application/controllers')  
  24.      ->throwExceptions(true)  
  25.      ->dispatch();  
  26. ?> 
IndexController.php頁(yè)面
  1. <?php  
  2. class IndexController extends Zend_Controller_Action  
  3. {  
  4.  function init() //__construct 代替初始化函數(shù)  
  5.     {  
  6.        $this->registry =Zend_Registry::getInstance();  
  7.        $this->view =$this->registry['view'];  
  8.        $this->view->baseUrl =$this->_request->getBaseUrl();  
  9.     }  
  10.    
  11.  function indexAction()  
  12.     {  
  13.       //實(shí)例化  
  14.       $content = new Content();  
  15.       $db = $content->getAdapter();  
  16.       //  查找所有的信息,并賦值給變量info  
  17.       $where = $db->quoteInto('1 = ?''1');  
  18.       // 根據(jù)id降序  
  19.       $order = 'id desc';  
  20.       //  查找記錄  
  21.       $info =$content->fetchAll($where,$order)->toArray();  
  22.       $this->view->news = $info;  
  23.     echo$this->view->render('index.phtml');//顯示模版  
  24.     }  
  25.     //添加數(shù)據(jù)  
  26.     functionaddAction()  
  27.     {  
  28.     // 獲取數(shù)據(jù)傳輸方式并把它變成小寫(xiě),判斷是否等于post  
  29.     if(strtolower($_SERVER['REQUEST_METHOD']) =='post'){  
  30.            //使用post的方式接收title,content  
  31.      $title =$this->_request->getPost('title');  
  32.      $content =$this->_request->getPost('content');  
  33.      $addtime = time();  
  34.      $news = new Content();  
  35.      // 將所有的數(shù)據(jù)放在一個(gè)數(shù)組當(dāng)中  
  36.      $data = array(  
  37.      'title'=>$title,  
  38.      'content' =>$content,  
  39.      'addtime'=>$addtime 
  40.      );  
  41.      // 添加數(shù)據(jù)  
  42.      if($news->insert($data)){  
  43.       echo'添加數(shù)據(jù)成功!';  
  44.       echo'<ahref="'.$this->view->baseUrl.'/index/index/">返回首頁(yè)</a>';  
  45.       unset($data);  
  46.      }else{  
  47.       echo'添加數(shù)據(jù)失敗!';  
  48.       echo'<ahref="'.$this->view->baseUrl.'/index/index/">返回首頁(yè)</a>';  
  49.      }  
  50.     }  
  51.     }  
  52.    //  編輯數(shù)據(jù)  
  53.     functioneditAction()  
  54.     {  
  55.     //  實(shí)例化  
  56.     $content = new Content();  
  57.     $db =$content->getAdapter();  
  58.        //  獲取數(shù)據(jù)傳輸方式并把它變成小寫(xiě),判斷是否等于post  
  59.        if(strtolower($_SERVER['REQUEST_METHOD']) == 'post') {  
  60.            // 通過(guò)post方式獲取 id,title,content  
  61.         $id =$this->_request->getPost('id');  
  62.         $title =$this->_request->getPost('title');  
  63.         $content =$this->_request->getPost('content');  
  64.         // 將數(shù)據(jù)放在一個(gè)數(shù)組當(dāng)中  
  65.         $data = array(  
  66.         'title'=>$title,  
  67.         'content'=>$content,  
  68.         );  
  69.         //  條件語(yǔ)句 id=  
  70.         $where = $db->quoteInto('id =?',$id);  
  71.         //  更新數(shù)據(jù)  
  72.         $content->update($data,$where);  
  73.          
  74.         echo "更新數(shù)據(jù)成功!";  
  75.         echo'<ahref="'.$this->view->baseUrl.'/index/index/">返回首頁(yè)</a>';  
  76.         unset($data);  
  77.        }else{  
  78.         $id =$this->_request->getParam('id');  
  79.      $this->view->content=$content->fetchAll('id='.$id)->toArray();  
  80.    echo$this->view->render('edit.phtml');//顯示編輯模板  
  81.        }  
  82.     }  
  83.    //  刪除數(shù)據(jù)  
  84.     functiondeleteAction()  
  85.     {  
  86.     //  實(shí)例化Content類(lèi)  
  87.     $content = new Content();  
  88.     //  (Zendframeword)將會(huì)自動(dòng)對(duì)修改對(duì)數(shù)據(jù)進(jìn)行加引號(hào)處理,但是這種檢查不包括 條件分句,  
  89.     //   所以你需要使用該表的zend_db_adapter對(duì)象完成該工作.  
  90.     $db =$content->getAdapter();  
  91.        //  通過(guò)get方式來(lái)取得id的值  
  92.     $id =$this->_request->getParam('id');  
  93.     //  sql語(yǔ)句的刪除條件  
  94.     $where = $db->quoteInto('id =?',$id);  
  95.     //  刪除  
  96.     $content->delete($where);  
  97.     echo "刪除成功!";  
  98.     echo'<ahref="'.$this->view->baseUrl.'/index/index/">返回首頁(yè)</a>';  
  99.     }  
  100.     //文章的具體信息  
  101.     functionarticleAction()  
  102.     {  
  103.     //實(shí)例化  
  104.     $content = new Content();  
  105.        //  get方式獲取id  
  106.     $id =$this->_request->getParam('id');  
  107.        //  查找記錄  
  108.     $info =$content->find($id)->toArray();  
  109.        //  賦值給模板頁(yè)面  
  110.     $this->view->info =$info;  
  111.     echo$this->view->render('article.phtml');  
  112.     }  
model content.php
  1. <?php  
  2. class Content extends Zend_Db_Table  
  3. {  
  4.    
  5. protected $_name = "content";  
  6.  protected $_primary = 'id';  
  7. }  
  8. ?> 

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 沙湾县| 扶风县| 莒南县| 犍为县| 土默特左旗| 威海市| 公安县| 柘城县| 琼海市| 桐城市| 潼南县| 五台县| 靖安县| 长沙县| 英超| 松阳县| 浏阳市| 台东市| 古交市| 行唐县| 萨嘎县| 施甸县| 德兴市| 永春县| 湟中县| 清涧县| 隆安县| 瓦房店市| 华坪县| 辰溪县| 三河市| 玉林市| 隆昌县| 赤水市| 隆子县| 太和县| 焉耆| 同仁县| 青海省| 隆化县| 绥江县|