封装一套自己的model类(增 删 查 改)
select * from news where 1 :默认子查询查询所有的条件 ,如果有其他的条件可以覆盖默认查询所有的条件
mysqli_insert_id //返回添加的id
mysqli_affected_rows();//返回删除的行数
例子:
header("Content-type:text/html;charset=utf-8");
class Model{
//封装一套自己的model类
//链接池
public $link = NULL;
//链接表
public $tablename;
//链接数据库配置
public function __construct($tablename)
{
$this->tablename = $tablename; //获取表名
$this->link = mysqli_connect(HOST,USER,PASS) or die("链接数据库失败");
mysqli_select_db($this->link,DB);
mysqli_set_charset($this->link,"utf8");
}
//查询单行语句
public function dbgetone($where="1")
{
$sql = "select * from {$this->tablename} where {$where}";
echo ($sql);
$res = mysqli_query($this->link,$sql);
$row = mysqli_fetch_assoc($res);
return $row;
}
//查询多行语句
public function dbgetlist($where="1",$order="order by id desc",$limit="0,5")
{
$sql = "select * from {$this->tablename} where {$where} {$order} limit {$limit}";
$res =mysqli_query($this->link,$sql);
$data = array();
while($row = mysqli_fetch_assoc($res))
{
$data[] = $row;
}
return $data;
}
//添加语句
public function dbinsert($data=array())
{
$str_key = ""; // `id`,`title`,`content`
$str_val = ""; // 'asda','asd','asdas'
foreach($data as $k=>$v)
{
$str_key .= $k.","; // `id`,`title`,`content`,
$str_val .= "'".$v."'," ;//'asda','asd','asdas',
}
$str_key = substr($str_key,0,-1);
//echo ($str_key)."<br>";
$str_val = substr($str_val,0,-1);
//echo ($str_val);die;
$sql = "insert into {$this->tablename} ({$str_key}) VALUES ({$str_val})";
//echo ($sql);
$res = mysqli_query($this->link,$sql);
return mysqli_insert_id($this->link);//返回添加的id
}
//修改语句
public function dbgetupdate($where,$data=array())
{
$key_value_str = ""; //`title`="哈哈哈",`content`="嘻嘻嘻"
foreach($data as $k=>$v)
{
$key_value_str .= $k."='".$v."',";//`title`="哈哈哈哈",`content`="嘻嘻嘻",
}
$key_value_str = substr($key_value_str,0,-1);
//echo ($key_value_str);die;
$sql = "update {$this->tablename} set {$key_value_str} where {$where}";
echo ($sql);
$res = mysqli_query($this->link,$sql);
}
//删除语句
public function dbdelete($where)
{
$sql="delete from {$this->tablename} where {$where}";
//echo ($sql);die;
$res = mysqli_query($this->link,$sql);
return mysqli_affected_rows($this->link);//返回删除的行数
}
public function dbcount()
{
//统计总条数 ceil(总条数 / 固定的条数) 得到总页数
$sql = "select count(*) as count from `{$this->tablename}`";
$res = mysqli_query($this->link,$sql);
$row = mysqli_fetch_assoc($res);
//print_r($row);die;
$pageNum = $row['count'];//得到总条数
return $pageNum;
}
}
define("HOST","127.0.0.1");
define("USER","root");
define("PASS","root");
define("DB","xiexie");
$m = new Model("news");
$id = 1;
$b = $m->dbgetone("id=".$id);
//print_r($b);
$m = new Model("news");
$a = $m->dbgetlist();
print_r($a);
$m = new Model("news");
$arr = ["title"=>"sadsa","content"=>"asdasda","time"=>"11-8","category_id"=>2];
$m->dbinsert($arr);
$m = new Model("admin");
$m->dbdelete("id=6");
$m = new Model("admin");
$data = ["username"=>"aaaaaa","password"=>"bbbbbbbbbbb"];
$m->dbgetupdate("id=2",$data);
smarty
(1) : Smarty : 模板引擎 (视图分离(php 和 html 分离)) 用Smarty模板引擎mvc模式,Smarty模板写了自己的一套模板语法,且比较简单.
(2):Smarty版本 Smarty2版本 Smarty3版本
Smarty用于ecshop框架比较多, ecshop做购物商城 (二次开发 )
头次开发 (自己封装低层代码) mvc 框架
libs:核心文件
Smarty.class.php :Smarty核心文件
(3):安装
拷贝libs文件
加载libs文件下的Smarty.class.php
存放视图文件
template: 视图 view:视图
$smarty->template_dir = "template/"; //设置smarty视图分离中的模板的路径,以后所有的视图文件都放在template文件夹下面.
存放程序编译文件
$smarty->compile_dir = "templates_c/";//设置smarty编译文件的路径
html文件后缀名可以用:tpl后缀 html
参数传递:
$smarty->assign(‘标签名’,‘值');
加载视图文件:
$smarty->display("文件路径");
$smarty->display("1.tpl"); : 加载视图文件
页面循环输出值:
循环列表
Smarty2版本:<{foreach from=$result key=k item=v}><{$k}><{/foreach}>
Smarty3版本:
{foreach 标签名 as $k=>$v}
{$k}
{/foreach}
{foreach $data as $k=>$v}
{$k}
{/foreach}
设置两个备用属性 (为了防止页面里面的语法和Smarty语法起冲突)
如果没有设置备用属性,默认就是 {开始 结束} ;
如果设置了备用属性,那就必须按照备用属性使用
$smarty->left_delimiter = "<{";
$smarty->right_delimiter = "}>";
输出单个值:
<{标签名}>
<{$name}>
传入session的值
$smarty->assign("username",$_SESSION['username']);
$smarty语法可以传入数据类型的值,也可以传入超级数组的值.
模板的注释
<{*欢迎谢燕*}>
//加载模板文件
<{include file="文件路径"}>
设置缓存文件: (缓存开发时不启用,项目上线了之后启用)
$smarty->caching = true; //默认设置为1,默认为1个小时的周期,1个小时周期已过自动会清除缓存
$smarty->caching = 2; //缓存各自的缓存(模板生成各自的缓存) 有各自的缓存周期
$smarty->cache_lifetime = 300; //执行代码之后.
设置缓存存放的文件
$smarty->cache_dir="缓存文件夹";
例子:
header("Content-type:text/html;charset=utf-8");
//加载核心文件
session_start();
require_once("libs/Smarty.class.php");
$smarty = new Smarty();//实例化得到Smarty类
//定义一个视图文件,存放html文件
$smarty->template_dir = "template/";
//程序编译文件
$smarty->compile_dir = "templates_c/";//设置smarty编译文件的路径
//设置缓存文件
$smarty->cache_dir = "cache/";//设置缓存文件
$smarty->caching = 2; //缓存各自的缓存(模板生成各自的缓存) 有各自的缓存周期
$smarty->cache_lifetime = 300; //执行代码之后.
$arr = ["可爱","宝宝","贝贝"];
//设置定界符
$smarty->left_delimiter = "<{";
$smarty->right_delimiter = "}>";
$name = "哈哈";
$_SESSION['username'] = "aaa";
/*foreach($arr as $k=>$v)
{
print_r($v);
}
*/
$smarty->assign("data",$arr);
$smarty->assign("name",$name);
$smarty->assign("username",$_SESSION['username']);
$smarty->display("1.tpl");
$smarty->display("2.html");
1.tpl
2.html