IndexController.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. get_include_path()); //配置环境路径
- 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=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);
- // 设置控制器
- $frontController=Zend_Controller_Front::getInstance();
- $frontController->setBaseUrl('/zendframework')//设置基本路径,这里面是你的项目的名字
- ->setParam('noViewRenderer',true)
- ->setControllerDirectory('./application/controllers')
- ->throwExceptions(true)
- ->dispatch();
- ?>
model content.php
- <?php
- class IndexController extends Zend_Controller_Action
- {
- function init() //__construct 代替初始化函数
- {
- $this->registry =Zend_Registry::getInstance();
- $this->view =$this->registry['view'];
- $this->view->baseUrl =$this->_request->getBaseUrl();
- }
- function indexAction()
- {
- //实例化
- $content = new Content();
- $db = $content->getAdapter();
- // 查找所有的信息,并赋值给变量info
- $where = $db->quoteInto('1 = ?', '1');
- // 根据id降序
- $order = 'id desc';
- // 查找记录
- $info =$content->fetchAll($where,$order)->toArray();
- $this->view->news = $info;
- echo$this->view->render('index.phtml');//显示模版
- }
- //添加数据
- functionaddAction()
- {
- // 获取数据传输方式并把它变成小写,判断是否等于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();
- // 将所有的数据放在一个数组当中
- $data = array(
- 'title'=>$title,
- 'content' =>$content,
- 'addtime'=>$addtime
- );
- // 添加数据
- if($news->insert($data)){
- echo'添加数据成功!';
- echo'<ahref="'.$this->view->baseUrl.'/index/index/">返回首页</a>';
- unset($data);
- }else{
- echo'添加数据失败!';
- echo'<ahref="'.$this->view->baseUrl.'/index/index/">返回首页</a>';
- }
- }
- }
- // 编辑数据
- functioneditAction()
- {
- // 实例化
- $content = new Content();
- $db =$content->getAdapter();
- // 获取数据传输方式并把它变成小写,判断是否等于post
- if(strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
- // 通过post方式获取 id,title,content
- $id =$this->_request->getPost('id');
- $title =$this->_request->getPost('title');
- $content =$this->_request->getPost('content');
- // 将数据放在一个数组当中
- $data = array(
- 'title'=>$title,
- 'content'=>$content,
- );
- // 条件语句 id=
- $where = $db->quoteInto('id =?',$id);
- // 更新数据
- $content->update($data,$where);
- echo "更新数据成功!";
- echo'<ahref="'.$this->view->baseUrl.'/index/index/">返回首页</a>';
- unset($data);
- }else{
- $id =$this->_request->getParam('id');
- $this->view->content=$content->fetchAll('id='.$id)->toArray();
- echo$this->view->render('edit.phtml');//显示编辑模板
- }
- }
- // 删除数据
- functiondeleteAction()
- {
- // 实例化Content类
- $content = new Content();
- // (Zendframeword)将会自动对修改对数据进行加引号处理,但是这种检查不包括 条件分句,
- // 所以你需要使用该表的zend_db_adapter对象完成该工作.
- $db =$content->getAdapter();
- // 通过get方式来取得id的值
- $id =$this->_request->getParam('id');
- // sql语句的删除条件
- $where = $db->quoteInto('id =?',$id);
- // 删除
- $content->delete($where);
- echo "删除成功!";
- echo'<ahref="'.$this->view->baseUrl.'/index/index/">返回首页</a>';
- }
- //文章的具体信息
- functionarticleAction()
- {
- //实例化
- $content = new Content();
- // get方式获取id
- $id =$this->_request->getParam('id');
- // 查找记录
- $info =$content->find($id)->toArray();
- // 赋值给模板页面
- $this->view->info =$info;
- echo$this->view->render('article.phtml');
- }
- }
- <?php
- class Content extends Zend_Db_Table
- {
- protected $_name = "content";
- protected $_primary = 'id';
- }
- ?>
新闻热点
疑难解答