1、zend入口index.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 . './application/controllers/'
. PATH_SEPARATOR . './application/views/'
.PATH_SEPARATOR . get_include_path()); //配置环境路径
// require_once 'Zend/Loader.php';
// Zend_Loader::registerAutoload();//设置Zend Framework 自动载入类文件
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=new Zend_Config_Ini('./application/configs/config.ini',null, true);
Zend_Registry::set('config',$config);//注册
$dbAdapter=Zend_Db::factory($config->general->db->adapter,$config->general->db->config->toArray());//创建zend_db_adapter对象
$dbAdapter->query('SET NAMES UTF8');//调用adapter的query方法,设置数据库获取的数据与浏览器同步
Zend_Db_Table::setDefaultAdapter($dbAdapter);//设置默认的适配器
Zend_Registry::set('dbAdapter',$dbAdapter);
//设置控制器
$frontController =Zend_Controller_Front::getInstance();
$frontController->setBaseUrl('/zendframe')//设置基本路径
->setParam('noViewRenderer', true)
->setControllerDirectory('./application/controllers')
->throwExceptions(true)
->dispatch(); //run
?>
2、config.ini
[general]
db.adapter = PDO_MYSQL
db.config.host = localhost
db.config.username = root
db.config.password =
db.config.dbname = zend
3、运用到的数据表定义message.php
<?php
class Message extends Zend_Db_Table
{
protected $_name = "message";
protected $_primary = 'id';
}
?>
4、控制器文件IndexController.php
<?php
class IndexController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
$this->registry = Zend_Registry::getInstance();
$this->view = $this->registry['view'];
$this->view->baseUrl = $this->_request->getBaseUrl();
}
function indexAction()
{
$message=new message();//实例化数据库类
//获取数据库内容
$this->view->messages=$message->fetchAll()->toArray();
echo $this->view->render('index.html');//显示模版
}
function addAction(){
//如果是POST过来的值.就增加.否则就显示增加页面
if(strtolower($_SERVER['REQUEST_METHOD'])=='post'){
$content=$this->_request->getPost('content');
$title=$this->_request->getPost('title');
$message=new Message();
$data=array(
'content'=>$content,
'title'=>$title
);
$message->insert($data);
unset($data);//删除data变量
echo '您增加数据成功!请您<a href="'.$this->view->baseUrl.'/index/index/">返回</a>';
}else{
echo $this->view->render('add.html');//显示增加模版
}
}
function editAction(){
$message = new message();
$db = $message->getAdapter();
if(strtolower($_SERVER['REQUEST_METHOD'])=='post'){
$content=$this->_request->getPost('content');
$title = $this->_request->getPost('title');
$id = $this->_request->getPost('id');
$set = array(
'content'=>$content,
'title'=>$title
);
$where = $db->quoteInto('id=?',$id);
$message->update($set,$where);
unset($set);
echo $content;
echo $title;
echo '修改成功<a href="'.$this->view->baseUrl.'/index/index/">返回</a>';
}else{
$id = $this->_request->getParam('id');//获取url所传递的变量id
$this->view->message = $message->fetchAll('id='.$id)->toArray();
echo $this->view->render('edit.html');
}
}
function deleteAction(){
$message = new message();
$db = $message->getAdapter();
$id = (int)$this->_request->getParam('id');
if($id>0){
$where = 'id = '.$id;
$message->delete($where);
}
echo '删除成功<a href="'.$this->view->baseUrl.'/index/index">返回</a>';
}
}
5、前台页面index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>增加数据</title>
</head>
<body>
<a href="<?php echo $this->baseUrl ?>/index/add/">增加数据</a>
<table width="500px">
<thead>
<tr>
<th class="title" colspan="2">数据列表显示</th>
</tr>
</thead>
<tbody>
<tr>
<th style="width:25%;">标题</th>
<th>内容</th>
</tr>
<?foreach($this->messages as $message): ?>
<tr>
<th><?php echo $message['title']; ?></th>
<td><?php echo $message['content']; ?></td>
<td><a href="<?php echo $this->baseUrl ?>/index/edit/id/<?=$message['id'];?>">修改数据</a>/
<a href="<?php echo $this->baseUrl ?>/index/delete?id=<?=$message['id'];?>">删除</a>
</td>
</tr>
<?endforeach; ?>
</tbody>
</table>
</body>
</html>
6、add.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>增加数据</title>
</head>
<body>
<form method="post"action="<?php echo $this->baseUrl; ?>/index/add/">
<table>
<thead>
<tr>
<th class="title" colspan="2">增加数据</th>
</tr>
</thead>
<tbody>
<tr>
<th style="width:25%;">标题</th>
<td><input type="text" name="title" id="title" value="" size="20"/> <span class="error">*</span></td>
</tr>
<tr>
<th>内容</th>
<td><textarea name="content" id="content" col="40" row="4"></textarea><span class="error">*</span></td>
</tr>
<tr>
<th colspan="2"><input type="submit" value="增加数据" class="cmd" /></th>
</tr>
</tbody>
</table>
</form>
</body>
</html>
7、edit.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>编辑页面</title>
</head>
<body>
<form method="post" action="<?php echo $this->baseUrl ?>/index/edit/">
<table>
<thead>
<tr>
<th class="title" colspan="2">修改数据</th>
</tr>
</thead>
<? foreach($this->message as $message):?>
<tbody>
<tr>
<th style="width:25%;">标题</th>
<td><input type="text" name="title" id="title" value="<?=$message['title'];?>" size="20"/> <span class="error">*</span></td>
</tr>
<tr>
<th>内容</th>
<td><textarea name="content" id="content" cols="40" rows="4" ><?=$message['content'];?></textarea><span class="error">*</span></td>
</tr>
<input type="hidden" id="id" name='id' value="<?=$message['id'];?>" />
<tr>
<th colspan="2"><input type="submit" value="修改数据" class="cmd" /></th>
</tr>
</tbody>
<? endforeach;?>
</table>
</form>
</body>
</html>