控制器代碼
\Application\Admin\Controller\ArticleController.class.php
~~~
<?php
namespace Admin\Controller;
use Admin\Controller\AuthController;
use Think\Auth;
class ArticleController extends AuthController
{
public function _initialize()
{
parent::_initialize();
$this->db = D('Article');
$this->ArticleCateModel = D('ArticleCate');
}
// 文章列表
public function index()
{
$count = $this->db->where($map)->count();
$page = new \Think\Page($count , C('DEFAULT_PAGE_LIMIT'));
$list = $this->db->where($map)->order('id desc')->limit($page->firstRow.','.$page->listRows)->select();
foreach ($list as $k=>$v){
$list[$k]['catename'] = $this->ArticleCateModel->where( array('id'=>$v['cid']))->getField('name');
}
$this->list = $list;
$this->page = $page->show();
$this->catelist = $this->ArticleCateModel->select();
$this->display();
}
/**
* 編輯文章
*/
public function update()
{
if (IS_POST) {
$data = I('post.');
$res = $this->db->update($data);
if ($res) {
$this->ajaxReturn(['code'=>1,'msg'=>'更新成功']);
}else{
$this->ajaxReturn(['code'=>0,'msg'=>$this->db->getError()]);
}
}else{
$id = I('id');
if ($id) {
$this->info = $this->db->where(array('id'=>$id))->find();
}
$this->list = $this->ArticleCateModel->select();
$this->display();
}
}
//刪除文章
public function del(){
$id = I('id');
$res = $this->db->delete($id);
if ($res) {
$this->ajaxReturn(['code'=>1,'msg'=>'刪除成功']);
}else{
$this->ajaxReturn(['code'=>0,'msg'=>'刪除失敗']);
}
}
}
~~~
模型
\Application\Admin\Model\ArticleModel.class.php
~~~
<?php
namespace Admin\Model;
use Think\Model;
class ArticleModel extends Model
{
/**
* 自動驗證
* @var array
*/
protected $_validate = array(
array('cid', 'require', '請選擇分類'),
array('title', 'require', 'title不能為空'),
array('content', 'require', 'content不能為空'),
array('sort', 'require', 'sort不能為空'),
array('introductions', 'require', '簡介不能為空'),
);
/**
* 自動完成
* @var array
*/
protected $_auto = array(
array('create_time', NOW_TIME, 1),
array('update_time', NOW_TIME, 3),
array('content','htmlspecialchars_decode',3,'function'), // 對name字段在新增和編輯的時候回調getName方法
);
/**
* 更新數據
* @DateTime 2018-05-04
* @return [type] [description]
*/
public function update($info){
$data = $this->create($info);
// dump($this->getError());die;
if(!$data){ //數據對象創建錯誤
return false;
}
/* 添加或更新數據 */
if(empty($data['id'])){
return $this->add();
}else{
return $this->save();
}
}
}
~~~
列表頁面
\Application\Admin\View\Article\index.html
~~~
<include file="Public:header" />
<include file="Public:top" />
<!-- Contents -->
<div id="Contents">
<!-- TopMain -->
<div id="TopMain">
<!-- btn_box -->
<div class="btn_box floatL">
<a href="{:U('Article/update')}" >
<input type="button" value="添加文章">
</a>
</div>
<div class="btn_box floatL">
<form id="form" action="__SELF__" method="post">
<span class="sttl">文章標題:</span>
<input name="title" type="text" size="16" style="background-color: #fff;color:#cdcdcd;">
<input type="submit" value="查詢" >
</form>
</div>
<!-- /btn_box -->
</div>
<!-- /TopMain -->
<!-- MainForm -->
<div id="MainForm">
<div class="form_boxA">
<h2>文章列表</h2>
<table cellpadding="0" cellspacing="0">
<tr>
<th>ID</th>
<th>文章標題</th>
<th>文章分類</th>
<th>文章排序</th>
<th>更新時間</th>
<th>狀態</th>
<th>操作</th>
</tr>
<volist name="list" id="vo">
<tr>
<td>{$vo.id}</td>
<td>{$vo.title}</td>
<td>{$vo.catename}</td>
<td>{$vo.sort}</td>
<td>{$vo.update_time|date="Y-m-d H:i:s",###}</td>
<td><if condition="$vo['status'] eq 1"><span class="f_cB">啟用</span><else />禁止</if></td>
<td>
<a href="{:U('Article/update',array('id'=>$vo[id]))}">編輯</a>
<a class="del" link="{:U('Article/del',array('id'=>$vo['id']))}">刪除</a>
</td>
</tr>
</volist>
</table>
</div>
</div>
<!-- /MainForm -->
<!-- PageNum -->
{$page}
<!-- /PageNum -->
</div>
<!-- /Contents -->
<include file="Public:footer" />
~~~
修改頁面
\Application\Admin\View\Article\update.html
~~~
<include file="Public:header" />
<include file="Public:top" />
<!-- Contents -->
<div id="Contents">
<!-- TopMain -->
<div id="TopMain">
<!-- btn_box -->
<div class="btn_box floatL">
<a href="{:U('Article/index')}" style="text-decoration: none;">
<input type="button" value="返回" >
</a>
</div>
<!-- /btn_box -->
</div>
<!-- /TopMain -->
<!-- MainForm -->
<div id="MainForm" style="padding: 50px;">
<div class="form_boxC">
<form id="form" method="post">
<table cellpadding="0" cellspacing="0">
<tr>
<th>文章標題</th>
<td><div class="txtbox floatL">
<input type="text" name="title" id="title" size="100" value="{$info.title}" />
</div></td>
</tr>
<tr>
<th>分類</th>
<td>
<div class="selectbox floatL mag_r20">
<select name="cid" id="cid">
<volist name="list" id="vo">
<option value="{$vo.id}" <if condition="$info['cid'] eq $vo['id']">selected="selected"</if>>{$vo.name}</option>
</volist>
</select>
</div>
</td>
</tr>
<tr>
<th>新聞摘要</th>
<td><div class="txtbox floatL">
<textarea rows="5" cols="80" name="introductions">{$info.introductions}</textarea>
</div><span class="f_cB">(必填)</span></td>
</tr>
<tr>
<th>文章內容</th>
<td><div class="txtbox floatL">
<script id="content" name="content" type="text/plain" style="width:800px;height:300px;">{$info.content}</script>
</div>
</td>
</tr>
<tr>
<th>排序</th>
<td><div class="txtbox floatL">
<input type="text" name="sort" id="sort" size="24" value="{$info.sort}" />
</div></td>
</tr>
<tr>
<th> </th>
<td>
<div class="btn_box" style="box-shadow: none;">
<input type="hidden" name="id" value="{$info.id}" />
<input type="submit" style="min-width:160px;" value="提交">
</div>
</td>
</tr>
</table>
</form>
</div>
</div>
<!-- /MainForm -->
</div>
<!-- /Contents -->
<js href="__PUBLIC__/Admin/js/jquery-2.0.2.js" />
<js href="__PUBLIC__/static/ueditor/ueditor.config.js" />
<js href="__PUBLIC__/static/ueditor/ueditor.all.min.js" />
<js href="__PUBLIC__/static/ueditor/lang/zh-cn/zh-cn.js" />
<script>
$(function(){
var UEDITOR_HOME_URL = "http://img.user.com/Public/static/ueditor/";
var ue = UE.getEditor('content',{
serverUrl: UEDITOR_HOME_URL + "/php/controller.php"
});
$("#form").submit(function(e){
var res = $(this).serialize();
var url = "{:U('Article/update')}";
$.ajax({
url: url,
data: res,
type: 'post',
success:function(data){
if (data.code == 1) {
layer.alert(data.msg,{icon:6},function (index) {
layer.close(index);
window.location.href = "{:U('Article/index')}";
});
} else{
layer.alert(data.msg,{icon:5},function (index) {
layer.close(index);
window.location.reload();
});
}
}
});
return false; // 阻止表單跳轉
});
});
</script>
<include file="Public:footer" />
~~~
數據表sql語句
~~~
CREATE TABLE `lxl_article` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`cid` tinyint(4) NOT NULL COMMENT '分類ID',
`title` varchar(50) NOT NULL COMMENT '文章標題',
`introductions` varchar(250) NOT NULL COMMENT '摘要',
`content` text NOT NULL COMMENT '文章內容',
`sort` int(11) NOT NULL DEFAULT '50' COMMENT '排序',
`create_time` int(11) NOT NULL COMMENT '創建時間',
`update_time` int(11) NOT NULL COMMENT '更新時間',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=16 DEFAULT CHARSET=utf8 COMMENT='// 文章';
~~~
- php ping 地址
- python調用php腳本和sh調用php腳本
- php隨機字符串
- redis操作
- 公共頁面文件
- 登錄
- 文章
- 文章管理
- 文章分類
- 圖片
- 圖片分類
- 圖片管理
- 配置文件
- ueditor配置遠程上傳
- django
- 安裝
- jinja2模板標簽
- 虛擬機
- centos寶塔面板安裝
- mysql主從搭建
- 虛擬機安裝centos7
- 2
- 主從復制
- uni-app
- 更新
- 直播簡單代碼
- 搞笑的注釋代碼
- jwt
- centos以太坊環境搭建
- thinkphp5.1下的redis使用
- redis的安裝
- tp5.1中使用
- tp5.1下載酷狗音樂
- 跨域
- tp5.1導出數據庫到excel
- 鉤子和行為
- 支付寶
- 申請支付寶app接入
- 視頻播放
- 模塊安裝
- 推流配置
- pc端網頁代碼
- srs
- 后臺布局
- 基礎布局文件
- 左邊
- 頭部
- css特效代碼
- 圖片旋轉
- 圖片放大
- 頂部
- 列表頁
- 更新數據表單頁
- 模型獲取器
- 上傳圖片
- mysql
- 一些常用
- 遠程授權
- 數據庫常用命令
- 忘記密碼
- webpack
- 一些亂七八糟的東西
- linux后臺運行腳本過大處理辦法
- sublime插件
- linux svn安裝
- 工具
- 查看進程
- 獲取微信公眾號文章
- 爬取微信公眾號文章
- 清空nohup
- 服務器上跨域配置
- sql語句生成orm模型寫法的工具
- centos換阿里源
- linux一些日志操作
- zsh