Yzmcms学习第三讲 -广告模块
defined('IN_YZMPHP') or exit('Access Denied'); 后台权限控制
yzm_base::load_controller('common', 'admin', 0); 加载admin模块下的common类
yzm_base::load_sys_class('page','',0);
完成的读数据,统计总数,分页,加载模板的流程:
$adver = D('adver'); //实例化adver表
$total = $adver->total();//获取广告总数
$page = new page($total, 5);//实例化page ,并分页为5条一页
$data = $adver->order('id DESC')->limit($page->limit())->select(); //查询总数并分页
include $this->admin_tpl('adver_list');//加载广告模板
htmlspecialchars — 将特殊字符转换为 HTML 实体
return_json(array('status'=>1,'message'=>L('operation_success'))); 添加广告成功提示
添加和编辑操作
public function add() {
if(isset($_POST['dosubmit'])) { //判断是否为dosubmit的name属性提交
$_POST['title'] = htmlspecialchars($_POST['title']);//接收广告标题并做过滤处理
$_POST['inputtime'] = SYS_TIME;//定义当前时间为添加时间
$_POST['code'] = $this->get_code($_POST);//code值定义
$_POST['start_time'] = !empty($_POST['start_time']) ? strtotime($_POST['start_time']) : 0;//定义开始时间
$_POST['end_time'] = !empty($_POST['end_time']) ? strtotime($_POST['end_time']) : 0;//定义结束时间
if(D('adver')->insert($_POST)){
return_json(array('status'=>1,'message'=>L('operation_success')));
}else{
return_json(array('status'=>0,'message'=>L('operation_failure')));
}
}else{
include $this->admin_tpl('adver_add');
}
}
/**
* 编辑
*/
public function edit() {
if(isset($_POST['dosubmit'])) {
$id = isset($_POST['id']) ? intval($_POST['id']) : 0;
$_POST['title'] = htmlspecialchars($_POST['title']);
$_POST['code'] = $this->get_code($_POST);
$_POST['start_time'] = !empty($_POST['start_time']) ? strtotime($_POST['start_time']) : 0;
$_POST['end_time'] = !empty($_POST['end_time']) ? strtotime($_POST['end_time']) : 0;
if(D('adver')->update($_POST, array('id'=>$id))){ //如果有影响条数,提示更新成功
return_json(array('status'=>1,'message'=>L('operation_success')));
}else{
return_json();
}
}else{
$id = isset($_GET['id']) ? intval($_GET['id']) : 0;//get接收广告id
$data = D('adver')->where(array('id'=>$id))->find();//读取本条光
include $this->admin_tpl('adver_edit');
}
}
删除操作
public function del() {
if($_POST && is_array($_POST['id'])){
if(D('adver')->delete($_POST['id'], true)){
showmsg(L('operation_success'), '', 1);
}else{
showmsg(L('operation_failure'));
}
}
}
模板:
onclick="yzm_open('添加广告','<?php echo U('add');?>','700','500')" href="javascript:;" 以弹框形式打开添加框