请问在PHP中存不存在架构?像,三层、MVC等。
举例比如在.NET中的小三层:UI、BLL、DAL、Model(传输数据)。如果有的话可以提供一个应用实例让小弟参考下吗?在这里还想再问个问题。在.NET里,ASP.NET+SqlServer组合可以使用“SqlHelper”等操作数据库的类函数。
而在PHP中存不存在相似的操作Mysql数据库的函数库?虽然Mysql数据库是没有存储过程的。
最好能给点实例。
举例比如在.NET中的小三层:UI、BLL、DAL、Model(传输数据)。如果有的话可以提供一个应用实例让小弟参考下吗?在这里还想再问个问题。在.NET里,ASP.NET+SqlServer组合可以使用“SqlHelper”等操作数据库的类函数。
而在PHP中存不存在相似的操作Mysql数据库的函数库?虽然Mysql数据库是没有存储过程的。
最好能给点实例。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html" />
<title>PHP MVC留言板</title>
</head>
<body leftmargin="50px">
<a href="notebook.htm">添加新留言</a><br>
<p>
<?php
//!index.php 总入口
/**
* index.php的调用形式为:
* 显示所有留言:index.php?action=list&page=x
* 添加留言 :index.php?action=post
* 删除留言 :index.php?action=delete&id=x
*/
require_once('lib/DataAccess.php');
require_once('lib/Model.php');
require_once('lib/View.php');
require_once('lib/Controller.php');
//创建DataAccess对象(请根据你的需要修改参数值)
$dao=& new DataAccess ('localhost','xx0754','xx0754','xx0754');
//请自行加入config.inc.php进行控制
//根据$_GET["action"]取值的不同调用不同的控制器子类
$action=$_GET["action"];
switch ($action)
{
case "post":
$controller=& new postController($dao); break;
case "list":
$controller=& new listController($dao,10); break;
case "delete":
$controller=& new deleteController($dao); break;
default:
$controller=& new listController($dao,10); break; //默认为显示留言
}
$view=$controller->getView(); //获取视图对象
$view->display(); //输出HTML
?>
</body>
</html>notebook.htm
<style type="text/css">
<!--
.STYLE1 {
color: #FF9933;
font-size: xx-large;
font-weight: bold;
font-style: italic;
}
-->
</style>
<h1 class="STYLE1">留言版 </h1>
<form name="form1" method="post" action="index.php?action=post">
您的姓名:
<label>
<input name="username" type="text" id="username">
</label>
<p>您的邮箱:
<label>
<input name="email" type="text" id="email">
</label>
</p>
<p>您的留言内容:
<label>
<textarea name="content" id="content"></textarea>
</label>
</p>
<p>
<label>
<input type="submit" name="Submit" value="提交">
</label>
</p>
</form>
<p class="STYLE1"> </p>
lib\DataAccess.php
<?php
/**
* 一个用来访问MySQL的类
* 仅仅实现演示所需的基本功能,没有容错等
* 代码未作修改,只是把注释翻译一下,加了点自己的体会
*/
class DataAccess {
var $link_id; //用于存储数据库连接
var $query_id; //用于存储查询源
//! 构造函数.
/**
* 创建一个新的DataAccess对象
* @param $host 数据库服务器名称
* @param $user 数据库服务器用户名
* @param $pass 密码
* @param $db 数据库名称
*/
function __construct($host,$user,$pass,$db) {
$this->link_id=mysql_pconnect($host,$user,$pass); //连接数据库服务器
mysql_select_db($db,$this->link_id); //选择所需数据库
//mysql_query("set names utf8;");
}
//! 执行SQL语句
/**
* 执行SQL语句,获取一个查询源并存储在数据成员$query中
* @param $sql 被执行的SQL语句字符串
* @return void
*/
function query($sql) {
$this->query_id=mysql_unbuffered_query($sql,$this->link_id); //Perform query here
if ($this->query_id) return true;
else return false;
}
//! 获取结果集
/**
* 以数组形式返回查询结果的所有记录
* @return mixed
*/
function fetchRows($sql) {
$this->query($sql);
$arr=array();
$i=0;
while( $row=mysql_fetch_array($this->query_id,MYSQL_ASSOC) ) //MYSQL_ASSOC参数决定了数组键名用字段名表示
{
$arr[$i]=$row;
$i++;
}
return $arr;
}
}
?>lib\Controller.php
<?
class Controller
{
var $model;
var $view; function __construct (&$dao)
{
$this->model = &new Model ($dao);
} function getView ()
{
return $this->view;
}
}class listController extends Controller
{
function __construct (&$dao,$perNumber)
{
parent::__construct ($dao);
$totalNumber = $this->model->sum();
$notes = $this->model->listNote ($perNumber);
$this->view = &new listView ($notes,$perNumber,$totalNumber);
}
}class postController extends Controller
{
function __construct (&$dao)
{
parent::__construct ($dao);
if ($this->model->postNote ())
$success = 1;
else
$success = 0;
$this->view = &new postView ($success);
}
}class deleteController extends Controller
{
function __construct (&$dao)
{
parent::__construct ($dao);
if ($this->model->deleteNote ())
$success = 1;
else
$success = 0;
$this->view = &new deleteView ($success);
}
}
?>lib\Model.php
<?
class Model
{
var $dao; function __construct (&$dao)
{
$this->dao = $dao;
}
function sum(){
$notes = $this->dao->fetchRows("SELECT * FROM note");
$sum = count($notes);
return $sum;
}
function listNote ($perNumber)
{
$page=$_GET['page'];
if (!isset($page)) {
$page=1;
}
$startCount=($page-1)*$perNumber;
$notes =$this->dao->fetchRows("select * from note order by timedate desc limit $startCount,$perNumber");
return $notes;
} function postNote ()
{
$name = $_POST['username'];
$email = $_POST['email'];
$content = $_POST['content'];
$timedate = date ("y-m-d H:i",time());
$sql ="insert into note(name,email,content,timedate) values('".$name."','".$email."','".$content."','".$timedate."')";
if ($this->dao->query ($sql))
return true;
else
return false;
} function deleteNote ()
{
$sql = "delete from note where id=".$_GET['id'];
if($this->dao->query ($sql))
return true;
else
return false;
}
}
?>lib\View.php
<?
class View
{
var $output;
var $totalNumber;
var $toatalPage;
function display ()
{
echo ($this->output);
}
}
class listView extends View
{
function __construct ($notes,$perNumber,$totalNumber)
{
$page=$_GET['page'];
if (!isset($page)) {
$page=1;
}
$totalPage=ceil($totalNumber/$perNumber);
echo $totalPage;
echo $page;
foreach ($notes as $value)
{
$this->output.= "<p><strong>访客姓名:</strong>".$value['name']."</p>".
"<p><strong>访客邮箱:</strong>".$value['email']."</p>".
"<p><strong>访客留言:</strong>".$value['content']."</p>".
"<p><strong>来访时间:</strong>".$value['timedate']."</p>".
"<p align=\"right\"><a href=\"index.php?action=delete&id=".$value['id']."\">删除留言</a>"."<hr />";
}
if($page!=1){
$this->output.= "<a href=\"index.php?action=list&page=".($page-1)."\">上一页</a>";
}
if ($page<$totalPage){
$this->output.= "<a href=\"index.php?action=list&page=".($page+1)."\">下一页</a>";
}
}
}
class postView extends View
{
function __construct ($success)
{
if ($success)
$this->output =
"留言成功!<br><a href=\"".$_SERVER['PHP_SELF']."?action=list\">查看</a>";
else
$this->output = "留言保存失败!";
}
}
class deleteView extends View
{
function __construct ($success)
{
if ($success)
$this->output =
"留言删除成功!<br><a href=\"".$_SERVER['PHP_SELF']."?action=list\">查看</a>";
}
}
?>
谁告诉你的??
MYSQL有存储过程?没用过啊?