数据库设计
DROP TABLE IF EXISTS `think_category`;
CREATE TABLE `think_category` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '分类ID',
`pid` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '上级分类ID',
`name` varchar(50) NOT NULL COMMENT '分类名称',
`type` tinyint(1) unsigned NOT NULL DEFAULT '1' COMMENT '分类类型 1 列表 2 单页',
`sort` smallint(5) unsigned NOT NULL DEFAULT '0' COMMENT '排序',
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`) USING BTREE
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COMMENT='分类表';
模型
public function getLevelList() {
$category_level = $this->order(['sort' => 'DESC', 'id' => 'ASC'])->select();
return array2level($category_level);
}
分类的添加
protected $category_model;
protected $article_model;
protected function _initialize() {
parent::_initialize();
$this->category_model = new CategoryModel();
$category_level_list = $this->category_model->getLevelList();
$this->assign('category_level_list', $category_level_list);
}
public function save() {
$data = $this->request->post();
if ($this->category_model->allowField(true)->save($data)) {
$this->success('保存成功');
}else {
$this->error('保存失败');
}
}
/**
* 编辑栏目
* @param $id
* @return mixed
*/
public function edit($id) {
$category = $this->category_model->find($id);
return $this->fetch('edit', ['category' => $category]);
}
html代码栏目选择部分,其他都是<input>标签
<div class="layui-form-item">
<label class="layui-form-label">上级栏目</label>
<div class="layui-input-block">
<select name="pid" lay-verify="required">
<option value="0">一级栏目</option>
{foreach name="category_level_list" item="vo"}
<option value="{$vo.id}" {if condition="$pid==$vo.id"} selected="selected"{/if}>
{neq name="vo.level" value="1"}
|{php}
for($i=1;$i<$vo['level'];$i++)
{echo ' ----';}
{/php}
{/neq} {$vo.name}</option>
{/foreach}
</select>
</div>
</div>
用到的函数common.php
<?php
/**
* 数组层级缩进转换
* @param array $array
* @param int $pid
* @param int $level
* @return array
*/
function array2level($array, $pid = 0, $level = 1) {
static $list = [];
foreach ($array as $v) {
if ($v['pid'] == $pid) {
$v['level'] = $level;
$list[] = $v;
array2level($array, $v['id'], $level + 1);
}
}
return $list;
}
/**
* 构建层级(树状)数组
* @param array $array 要进行处理的一维数组,经过该函数处理后,该数组自动转为树状数组
* @param string $pid 父级ID的字段名
* @param string $child_key_name 子元素键名
* @return array|bool
*/
function array2tree(&$array, $pid = 'pid', $child_key_name = 'children') {
$counter = array_children_count($array, $pid);
if ($counter[0] == 0)
return false;
$tree = [];
while (isset($counter[0]) && $counter[0] > 0) {
$temp = array_shift($array);
if (isset($counter[$temp['id']]) && $counter[$temp['id']] > 0) {
array_push($array, $temp);
} else {
if ($temp[$pid] == 0) {
$tree[] = $temp;
} else {
$array = array_child_append($array, $temp[$pid], $temp, $child_key_name);
}
}
$counter = array_children_count($array, $pid);
}
return $tree;
}
/**
* 子元素计数器
* @param $array
* @param $pid
* @return array
*/
function array_children_count($array, $pid) {
$counter = [];
foreach ($array as $item) {
$count = isset($counter[$item[$pid]]) ? $counter[$item[$pid]] : 0;
$count++;
$counter[$item[$pid]] = $count;
}
return $counter;
}
/**
* 把元素插入到对应的父元素$child_key_name字段
* @param $parent
* @param $pid
* @param $child
* @param string $child_key_name 子元素键名
* @return mixed
*/
function array_child_append($parent, $pid, $child, $child_key_name) {
foreach ($parent as &$item) {
if ($item['id'] == $pid) {
if (!isset($item[$child_key_name]))
$item[$child_key_name] = [];
$item[$child_key_name][] = $child;
}
}
return $parent;
}
/**
* 循环删除目录和文件
* @param string $dir_name
* @return bool
*/
function delete_dir_file($dir_name) {
$result = false;
if(is_dir($dir_name)){
if ($handle = opendir($dir_name)) {
while (false !== ($item = readdir($handle))) {
if ($item != '.' && $item != '..') {
if (is_dir($dir_name . DS . $item)) {
delete_dir_file($dir_name . DS . $item);
} else {
unlink($dir_name . DS . $item);
}
}
}
closedir($handle);
if (rmdir($dir_name)) {
$result = true;
}
}
}
return $result;
}
/**
* 判断是否为手机访问
* @return boolean
*/
function is_mobile() {
static $is_mobile;
if (isset($is_mobile)) {
return $is_mobile;
}
if (empty($_SERVER['HTTP_USER_AGENT'])) {
$is_mobile = false;
} elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'Mobile') !== false
|| strpos($_SERVER['HTTP_USER_AGENT'], 'Android') !== false
|| strpos($_SERVER['HTTP_USER_AGENT'], 'Silk/') !== false
|| strpos($_SERVER['HTTP_USER_AGENT'], 'Kindle') !== false
|| strpos($_SERVER['HTTP_USER_AGENT'], 'BlackBerry') !== false
|| strpos($_SERVER['HTTP_USER_AGENT'], 'Opera Mini') !== false
|| strpos($_SERVER['HTTP_USER_AGENT'], 'Opera Mobi') !== false
) {
$is_mobile = true;
} else {
$is_mobile = false;
}
return $is_mobile;
}
来源:oschina
链接:https://my.oschina.net/u/2932102/blog/796281