IndexController.php頁(yè)面
- <?php
- error_reporting(E_ALL|E_STRICT); //在開(kāi)啟錯(cuò)誤報(bào)告
- date_default_timezone_set('Asia/Shanghai');//配置地區(qū)
- set_include_path('.' .PATH_SEPARATOR.'./library'.PATH_SEPARATOR .'./application/models/'.PATH_SEPARATOR. get_include_path()); //配置環(huán)境路徑
- require_once"Zend/Loader/Autoloader.php"; //載入zend框架
- Zend_Loader_Autoloader::getInstance()->setFallbackAutoloader(true);//靜態(tài)載入自動(dòng)類(lèi)文件
- $registry = Zend_Registry::getInstance();//靜態(tài)獲得實(shí)例
- $view = new Zend_View(); //實(shí)例化zend 模板
- $view->setScriptPath('./application/views/scripts/');//設(shè)置模板顯示路徑
- $registry['view'] = $view;//注冊(cè)View
- // 設(shè)置數(shù)據(jù)庫(kù)連接
- $config=newZend_Config_Ini('./application/config/config.ini',null,true);
- Zend_Registry::set('config',$config);
- $dbAdapter=Zend_Db::factory($config->general->db->adapter,$config->general->db->config->toArray());
- $dbAdapter->query('SET NAMESutf8');
- Zend_Db_Table::setDefaultAdapter($dbAdapter);
- Zend_Registry::set('dbAdapter',$dbAdapter);
- // 設(shè)置控制器
- $frontController=Zend_Controller_Front::getInstance();
- $frontController->setBaseUrl('/zendframework')//設(shè)置基本路徑,這里面是你的項(xiàng)目的名字
- ->setParam('noViewRenderer',true)
- ->setControllerDirectory('./application/controllers')
- ->throwExceptions(true)
- ->dispatch();
- ?>
model content.php
- <?php
- class IndexController extends Zend_Controller_Action
- {
- function init() //__construct 代替初始化函數(shù)
- {
- $this->registry =Zend_Registry::getInstance();
- $this->view =$this->registry['view'];
- $this->view->baseUrl =$this->_request->getBaseUrl();
- }
- function indexAction()
- {
- //實(shí)例化
- $content = new Content();
- $db = $content->getAdapter();
- // 查找所有的信息,并賦值給變量info
- $where = $db->quoteInto('1 = ?', '1');
- // 根據(jù)id降序
- $order = 'id desc';
- // 查找記錄
- $info =$content->fetchAll($where,$order)->toArray();
- $this->view->news = $info;
- echo$this->view->render('index.phtml');//顯示模版
- }
- //添加數(shù)據(jù)
- functionaddAction()
- {
- // 獲取數(shù)據(jù)傳輸方式并把它變成小寫(xiě),判斷是否等于post
- if(strtolower($_SERVER['REQUEST_METHOD']) =='post'){
- //使用post的方式接收title,content
- $title =$this->_request->getPost('title');
- $content =$this->_request->getPost('content');
- $addtime = time();
- $news = new Content();
- // 將所有的數(shù)據(jù)放在一個(gè)數(shù)組當(dāng)中
- $data = array(
- 'title'=>$title,
- 'content' =>$content,
- 'addtime'=>$addtime
- );
- // 添加數(shù)據(jù)
- if($news->insert($data)){
- echo'添加數(shù)據(jù)成功!';
- echo'<ahref="'.$this->view->baseUrl.'/index/index/">返回首頁(yè)</a>';
- unset($data);
- }else{
- echo'添加數(shù)據(jù)失敗!';
- echo'<ahref="'.$this->view->baseUrl.'/index/index/">返回首頁(yè)</a>';
- }
- }
- }
- // 編輯數(shù)據(jù)
- functioneditAction()
- {
- // 實(shí)例化
- $content = new Content();
- $db =$content->getAdapter();
- // 獲取數(shù)據(jù)傳輸方式并把它變成小寫(xiě),判斷是否等于post
- if(strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
- // 通過(guò)post方式獲取 id,title,content
- $id =$this->_request->getPost('id');
- $title =$this->_request->getPost('title');
- $content =$this->_request->getPost('content');
- // 將數(shù)據(jù)放在一個(gè)數(shù)組當(dāng)中
- $data = array(
- 'title'=>$title,
- 'content'=>$content,
- );
- // 條件語(yǔ)句 id=
- $where = $db->quoteInto('id =?',$id);
- // 更新數(shù)據(jù)
- $content->update($data,$where);
- echo "更新數(shù)據(jù)成功!";
- echo'<ahref="'.$this->view->baseUrl.'/index/index/">返回首頁(yè)</a>';
- unset($data);
- }else{
- $id =$this->_request->getParam('id');
- $this->view->content=$content->fetchAll('id='.$id)->toArray();
- echo$this->view->render('edit.phtml');//顯示編輯模板
- }
- }
- // 刪除數(shù)據(jù)
- functiondeleteAction()
- {
- // 實(shí)例化Content類(lèi)
- $content = new Content();
- // (Zendframeword)將會(huì)自動(dòng)對(duì)修改對(duì)數(shù)據(jù)進(jìn)行加引號(hào)處理,但是這種檢查不包括 條件分句,
- // 所以你需要使用該表的zend_db_adapter對(duì)象完成該工作.
- $db =$content->getAdapter();
- // 通過(guò)get方式來(lái)取得id的值
- $id =$this->_request->getParam('id');
- // sql語(yǔ)句的刪除條件
- $where = $db->quoteInto('id =?',$id);
- // 刪除
- $content->delete($where);
- echo "刪除成功!";
- echo'<ahref="'.$this->view->baseUrl.'/index/index/">返回首頁(yè)</a>';
- }
- //文章的具體信息
- functionarticleAction()
- {
- //實(shí)例化
- $content = new Content();
- // get方式獲取id
- $id =$this->_request->getParam('id');
- // 查找記錄
- $info =$content->find($id)->toArray();
- // 賦值給模板頁(yè)面
- $this->view->info =$info;
- echo$this->view->render('article.phtml');
- }
- }
- <?php
- class Content extends Zend_Db_Table
- {
- protected $_name = "content";
- protected $_primary = 'id';
- }
- ?>
|
新聞熱點(diǎn)
疑難解答