您的位置:首页 > 编程语言 > PHP开发

php面向对象深入理解(二)

2015-09-02 09:45 681 查看
一个简单的小程序:

配置 config.ini

<?php
//项目的根目录
define("ROOT","F:/文件夹的名字/oop/");
//数据库连接信息
define("DB_HOST",'localhost');
define("DB_USERNAME","root");
define("DB_PASSWORD","root");
define("DB_NAME",'cms');
define("DB_CHARSET","utf8");


类 Db.class.php

<?php
class Db{
protected $conn="";
/*
* 作用:连接数据库,打开 设置交互字符集,选择数据库
* 参数:host  username  password dbName charset
* 返回值:bool
*/
function Db(){
$link=mysql_connect(DB_HOST,DB_USERNAME,DB_PASSWORD);
$this->conn=$link;
if(is_resource($link)){
mysql_set_charset(DB_CHARSET);
$re=mysql_select_db(DB_NAME);
if($re){
return true;
}else{
return false;
}
}else{
return false;
}
}
/*
* 作用:执行sql语句
* 参数:sql语句
* 返回值:update delete 返回影响记录条数
*          insert 返回主键id值
*          select 返回二维数组
*/
function query($sql){
$re=mysql_query($sql);
if($re){
//判断sql语句的类型
if(preg_match("/^update|^delete/i",$sql)){
return mysql_affected_rows();
}else if(preg_match("/^insert/i",$sql)){
return mysql_insert_id();
}else if(preg_match("/^select/i",$sql)){
//返回二维数组
$arr=array();
while($row=mysql_fetch_assoc($re)){
$arr[]=$row;
}
return $arr;
}else{
return $re;
}
}else{
return false;
}
}

/*
* 作用:关闭数据库连接
* return:bool
*/
function __destruct(){
$re=mysql_close($this->conn);
}
}


增 add.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>Document</title>
</head>
<body>
<div>当前操作:文章添加</div>
<form action="doAction.php" method="post">
标题:<input type="text" name="title"/><br/>
内容:<textarea rows="8" cols="60" name="content"></textarea><br/>
作者:<input type="text" name="author"/><br/>
分类:<select name="type">
<option value="国内">国内</option>
<option value="国际">国际</option>
<option value="体育">体育</option>
<option value="军事">军事</option>
</select><br/>
<input type="submit" value="发布"/>
</form>
</body>
</html>


列表页 db.php

<?php
header("content-type:text/html;charset=utf-8");
require '../config.ini.php';
require '../class/Db.class.php';
$ob=new Db();
$arr=$ob->query("select * from news limit 5");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>Document</title>
</head>
<body>
<!-- 呈现表格 -->
<table>
<tr>
<th>id</th>
<th>标题</th>
<th>发布时间</th>
<th>操作</th>
</tr>
<?php
foreach($arr as $v){
?>
<tr>
<td><?php echo $v['id']?></td>
<td><?php echo $v['title']?></td>
<td><?php echo $v['pubtime']?></td>
<td><a href="">修改</a> <a href="delete.php?id=<?php echo $v['id']?>">删除</a></td>
</tr>
<?php
}
?>
</table>
</body>
</html>


删除delete.php

<?php
header("content-type:text/html;charset=utf-8");
//删除某id对应的文章
require '../config.ini.php';
require '../class/Db.class.php';
$ob=new Db();
//拼sql语句
$sql="delete from news where id=".$_GET['id'];
//执行 db::query()
$re=$ob->query($sql);
//根据结果提示
var_dump($re);


业务逻辑处理doAction.php

<?php
require '../config.ini.php';
require '../class/Db.class.php';
//调用 Db::__construt()
$ob=new Db();
//接收数据
$title=$_POST['title'];
$content=$_POST['content'];
$pubtime=time();
$type=$_POST['type'];
$author=$_POST['author'];
//拼sql语句
$sql="insert into news(title,content,pubtime,type,author)
values('$title','$content','$pubtime','$type','$author')";
//Db::query()
$re=$ob->query($sql);
//提示
var_dump($re);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: