zoukankan      html  css  js  c++  java
  • Zend.framework.对数据库的操作与模版的编写

    一、对zendframework编写数据库配置文件 在application文件夹下面创建一个db_config文件夹, 然后建立一个config.ini文件,编写代码如下: [general] db.adapter=PDO_MYSQL //请开启PDO扩展 db.config.host=localhost //Mysql主机 db.config.username=root //用户名 db.config.password=123456 //密码 db.config.dbname=card //数据库名 二、将数据库配置文件引入zendframework中 在index.php中添加如下代码: //配置数据库参数,并连接数据库 $config=new Zend_Config_Ini('./application/db_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 NAMES UTF8'); Zend_Db_Table::setDefaultAdapter($dbAdapter); Zend_Registry::set('dbAdapter',$dbAdapter); 三、如何控制和处理Zend framework方法内容? 1、Zend framework 也是采用单一入口的模式,所有后面跟的参数需要 / 来区别 http://localhost/zendframework/index/add/ 这样就能访问到了index模块下的add方法(系统默认的是 indexAction()索引方法名) http://localhost/zendframework/index/del/id/3 后面的就相当于可 ?id=3 ----> function delAction() <--IndexController.php 四、Zend framework 中相关文件的关系 《模块》和《视图》通过《控制器》来控制,《控制器》依靠zendframework来处理数据,《配置文件》和《数据库》都依靠zendframework来处理数据 五、Zend framework Db_Table常用函数介绍 fetchAll($sql): 取回结果集中所有字段的值,作为连续数组返回 fetchRow($sql): 只取回结果集的第一行 fetchAssoc($sql): 取回结果集中所有字段的值,作为关联数组返回 fetchCol($sql): 取回所有结果行的第一个字段名 fetchOne($sql): 只取回第一个字段值 fetchPairs($sql): 取回相关数组,第一个字段值为码 第二个字段为值 insert($arrParams) $arrParams:关联数组, key是数据库字段?return:lastInsertId insert() 插入数据? insert()方法,列名:数据的关联数组作为参数 ?? 自动加引号处理, 并返回插入的最后一行的id值 ?? $data = array( 'name' => 'King', 'color' => 'blue' );??? $id = $table->insert($data); update( $arrParams,$strSqlWhere )?$arrParams:关联数组, key是数据库字段? $strSqlWhere:条件 update() 更新数据 ?? update()方法,列名:数据的关联数组作为参数,通过一个where条件从句来决 定需要改变的行,返回被修改的行数, 自动加引号处理,检查不包括条件分句 ?? $table = new RoundTable(); ?? $db = $table->getAdapter(); ? $set = array('color' => 'yellow');?? $where = $db->quoteInto('first_name = ?', 'Robin');??? $table->update($set, $where); delete( $strSqlWhere )?? $strSqlWhere:条件 delete() 删除数据 ?delete()方法,通过一个where条件分句来决定需要删除的行.该方法将会返回 被删除的行数 ?? $where = $db->quoteInto('first_name = ?', 'Patsy'); ?? $table->delete($where); 六、Zend framework 视图循环和条件函数 1、foreach循环 <?foreach($this->messages as $message): ?> <tr> <th><?php echo $message['cid']; ?></th> <td><?php echo $message['title']; ?></td> <td><a href="<?php echo $this->baseUrl ?>/index/edit/id/<?=$message['id']?>/">编辑</a>/ <a href="<?php echo $this->baseUrl ?>/index/del/id/<?php echo $message['id']?>/">删除</a></td> </tr> <?endforeach; ?> 2、if条件判断 <?php if(2=3): ?> echo "ok"; <?php else: ?> echo "fail"; <?php endif; ?>     七、各种函数功能(完整程序功能在上面的文件中) /zendframework/application/controllers/IndexController.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(); } /* * Action(动作)! */ function indexAction() { //这里给变量赋值,在index.html模板里显示 // $this->view->word = '测试一个内容'; // $this->view->php= array("第一个内容","第二个内容"); $message=new message();//实例化数据库类 //获取数据库内容 $this->view->messages=$message->fetchAll()->toArray(); echo $this->view->render('index.phtml');//显示模版 } //往数据库中添加内容的动作 function addAction(){ //如果是POST过来的值.就增加.否则就显示增加页面 if(strtolower($_SERVER['REQUEST_METHOD'])=='post'){ $cid=$this->_request->getPost('cid'); $title=$this->_request->getPost('title'); $message=new Message(); $data=array( 'cid'=>$cid, 'title'=>$title ); $message->insert($data); unset($data); echo '您增加数据成功!请您<a href="'.$this->view->baseUrl.'/index/index/">返回</a>'; }else{ echo $this->view->render('add.phtml');//显示增加模版 } } function editAction(){ $message = new Message(); $db = $message->getAdapter(); if(strtolower($_SERVER['REQUEST_METHOD'])=='post'){ $id = $this->_request->getPost('id'); $cid = $this->_request->getPost('cid'); $title = $this->_request->getPost('title'); $set = array( 'cid'=>$cid, 'title'=>$title ); $where = $db->quoteInto('id = ?',$id); //更新数据 $message->update($set,$where); unset($set); echo '修改数据成功!<a href="'.$this->view->baseUrl.'/index/index/">返回</a>'; }else{ $id = $this->_request->getParam('id'); $this->view->messages = $message->fetchAll('id='.$id)->toArray(); echo $this->view->render('edit.phtml'); } } function delAction(){ $message = new Message(); $id = (int)$this->_request->getParam('id'); if($id > 0){ $where = 'id = ' . $id; $message->delete($where); } echo '删除数据成功!<a href="'.$this->view->baseUrl.'/index/index/">返回</a>'; } } ?>    
  • 相关阅读:
    第8周课下作业1(补)
    第八章课下测试
    POJ
    POJ
    HDU
    UVa
    UVa
    CodeForces
    ZOJ
    LightOJ
  • 原文地址:https://www.cnblogs.com/gxldan/p/4066810.html
Copyright © 2011-2022 走看看