一个简单的分类
建表SQL:
/*
Navicat MySQL Data Transfer
Source Server : localhost
Source Server Version : 50553
Source Host : localhost:3306
Source Database : db_hsgj_site_2
Target Server Type : MYSQL
Target Server Version : 50553
File Encoding : 65001
Date: 2017-08-10 23:58:15
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `think_nav`
-- ----------------------------
DROP TABLE IF EXISTS `think_nav`;
CREATE TABLE `think_nav` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`pid` int(11) DEFAULT '0',
`sort` int(11) DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=20 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of think_nav
-- ----------------------------
INSERT INTO `think_nav` VALUES ('1', 'A', '0', '1');
INSERT INTO `think_nav` VALUES ('2', 'B', '0', '0');
INSERT INTO `think_nav` VALUES ('3', 'A-a', '1', '2');
INSERT INTO `think_nav` VALUES ('4', 'A-b', '1', '3');
INSERT INTO `think_nav` VALUES ('10', 'A-a-2', '3', '0');
INSERT INTO `think_nav` VALUES ('8', 'A-c', '1', '1');
INSERT INTO `think_nav` VALUES ('9', 'A-a-1', '3', '0');
INSERT INTO `think_nav` VALUES ('11', 'A-a-3', '3', '0');
INSERT INTO `think_nav` VALUES ('12', 'A-b-1', '4', '0');
INSERT INTO `think_nav` VALUES ('13', 'A-b-2', '4', '0');
INSERT INTO `think_nav` VALUES ('14', 'A-b-3', '4', '0');
INSERT INTO `think_nav` VALUES ('15', 'A-c-1', '8', '0');
INSERT INTO `think_nav` VALUES ('16', 'A-c-2', '8', '0');
INSERT INTO `think_nav` VALUES ('17', 'A-c-3', '8', '0');
INSERT INTO `think_nav` VALUES ('18', 'B-a', '2', '0');
INSERT INTO `think_nav` VALUES ('19', 'B-b', '2', '0');
model类:
NewsClassModel.class.php
<?php
namespace Admin\Model;
use Think\Model;
/**
* 新闻分类
* newsClassModel = D("NewsClass");
*/
class NewsClassModel extends Model {
protected $tableName = 'news_class';
public function sorted(&$newList, $dataList, $parentID = 0, $level = 1) {
foreach ($dataList as &$item) {
if ($item['pid'] == $parentID) {
if ($parentID) {
if ($newList[$parentID]) {
$newList[$parentID]['has_children'] = 1;
$item['path'] = $newList[$parentID]['path'] . '_' . $item['id'];
} else {
$item['path'] = $parentID . '_' . $item['id'];
}
} else {
$item['path'] = $item['id'];
}
$item['level'] = $level;
$item['space'] = str_repeat(' ', ($level-1)*4);
$newList[$item['id']] = $item;
$this->sorted($newList, $dataList, $item['id'], $level+1);
}
}
}
public function getChildrenTypes($typeID, $incluedSelf = true) {
static $childrenTypes = array();
if ($incluedSelf) {
array_push($childrenTypes, $typeID);
}
$children = $this->where(array('pid'=>$typeID))->getField('id', true);
if (!empty($children)) {
$type = null;
foreach ($children as $type) {
if (!$incluedSelf) {
array_push($childrenTypes, $type);
}
$this->getChildrenTypes($type, $incluedSelf);
}
}
return $childrenTypes;
}
}
控制器中:
/**
* [List 列表]
*/
public function NavList(){
//分类
$class_model = D('Nav');
$originTypes = $class_model->order('pid asc, id asc')->select();
$class = array();
$class_model->sorted($class, $originTypes);
$this->assign('class_list', $class);
$this->display("Nav/nav_list");
}
模板:
<table class="flex-table">
<thead>
<tr>
<th width="24" align="center" class="sign"><i class="ico-check"></i></th>
<th width="180" align="center" class="handle">操作</th>
<th width="60" align="center">排序</th>
<th width="60" align="center">-</th>
<th width="300">分类名称</th>
<th></th>
</tr>
</thead>
<tbody>
<volist name="class_list" id="vo">
<tr data-id="{$vo.id}">
<td class="sign"><i class="ico-check"></i></td>
<td class="handle">
</td>
<td class="sort"><span nc_type="class_sort" fieldname="sort" column_id="{$vo.id}" title="可编辑" class="editable ">{$vo.sort}</span></td>
<td ><span style="">{$vo.space}</span></td>
<td class="name">{$vo.space}<span nc_type="class_name" fieldname="name" column_id="{$vo.id}" title="可编辑" class="editable ">{$vo.name}</span></td>
<td></td>
</tr>
</volist>
以上代码在模板中循环一次就可以显示出缩进的层次结构。
如果想分层次循环,可以如下:
/**
* [List 列表]
*/
public function NavList(){
//$datas = M("News")->select();
//var_dump($datas);
//$this->PageListGetXml();
//分类
$class_model = D('Nav');
$originTypes = $class_model->order('pid asc, id asc')->select();
$class = array();
$class_model->sorted($class, $originTypes);
//var_dump($class);
foreach ($class as $k => $v) {
if ($v['level'] == 1){
echo $v['name'].'<br>';
if ($v['has_children'] == 1){
foreach ($class as $k1 => $v1) {
if ($v1['pid'] == $v['id']){
echo ' '.$v1['name'].'<br>';
if ($v1['has_children'] == 1){
foreach ($class as $k2 => $v2) {
if ($v2['pid'] == $v1['id']){
echo ' '.$v2['name'].'<br>';
}
}
}
}
}
}
}
}
exit;
$this->assign('class_list', $class);
$this->display("Nav/nav_list");
}