MVC 代码书写:
控制器代码书写:
}
function indexAction()
{
$this->view->word=" I love spurs";
echo $this->view->render("index.html");
}
function addAction(){
//如果是POST过来的值.就增加.否则就显示增加页面
}
}
?>
控制当中写内容:
application->config.ini
[general]
db.adapter=PDO_MYSQL
db.config.host=localhost
db.config.username=root
db.config.password=
db.config.dbname=think_zw
配置文件引入到framework里面去
单一入口模式:localhost/index/add/访问index模块下的add方法
function addAction(){}(在IndexController.php)
默认访问为index模块下的index方法
再建立一个模块model里面的message.php
模块实例化:
//获取数据库内容
$this->view->messages=$message->fetchAll()->toArray();
echo $this->view->render(‘index.phtml‘);//显示模版
}
<?foreach($this->messages as $message): ?>
<tr>
<th><?php echo $message[‘title‘]; ?></th>
<td><?php echo $message[‘content‘]; ?></td>
</tr>
<?endforeach; ?>
*************
修改和删除数据
index.phtml里面加上
添加一个新的方法:edit.phtml
$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>‘;
}
异常出现:
解决办法:在index.php中的
*******
id/3 等于以前的?id=3
PHP开源开发框架ZendFramework使用中常见问题说明及解决方案
原文:http://www.jb51.net/article/50951.htm