rageframe(PHP微擎)树分类(curd)
【摘要】
sql: 控制器:
<?php
namespace frontend\controllers;
use app\models\ShopCategory;
use yii\web\Controll...
sql:
控制器:
<?php
namespace frontend\controllers;
use app\models\ShopCategory;
use yii\web\Controller;
use Yii;
class CygController extends Controller
{
public function actionList()
{
$model = new ShopCategory;
//获取列表
$cates = $model->getTreeList();
return $this->render("cates", ['cates' => $cates]);
}
public function actionAdd()
{
$model = new ShopCategory();
$list = $model->getOptions();
if (Yii::$app->request->isPost) {
$post = Yii::$app->request->post();
if ($model->add($post)) {
Yii::$app->session->setFlash("info", "添加成功");
}
}
return $this->render("add", ['list' => $list, 'model' => $model]);
}
public function actionMod()
{
$cateid = Yii::$app->request->get("cateid");
$model = ShopCategory::find()->where('cateid = :id', [':id' => $cateid])->one();
if (Yii::$app->request->isPost) {
$post = Yii::$app->request->post();
if ($model->load($post) && $model->save()) {
Yii::$app->session->setFlash('info', '修改成功');
}
}
$list = $model->getOptions();
return $this->render('add', ['model' => $model, 'list' => $list]);
}
public function actionDel()
{
try {
$cateid = Yii::$app->request->get('cateid');
if (empty($cateid)) {
throw new \Exception('参数错误');
}
$data = ShopCategory::find()->where('parentid = :pid', [":pid" => $cateid])->one();
if ($data) {
throw new \Exception('该分类下有子类,不允许删除');
}
if (!ShopCategory::deleteAll('cateid = :id', [":id" => $cateid])) {
throw new \Exception('删除失败');
}
} catch(\Exception $e) {
Yii::$app->session->setFlash('info', $e->getMessage());
}
return $this->redirect(['cyg/list']);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
model:
<?php
namespace app\models;
use yii\db\ActiveRecord;
use Yii;
use yii\helpers\ArrayHelper;
/**
* This is the model class for table "shop_category".
*
* @property string $cateid
* @property string $title
* @property string $parentid
* @property int $createtime
*/
class ShopCategory extends \yii\db\ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'shop_category';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['parentid', 'createtime'], 'integer'],
[['title'], 'string', 'max' => 32],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'cateid' => 'Cateid',
'title' => 'Title',
'parentid' => 'Parentid',
'createtime' => 'Createtime',
];
}
public function add($data)
{
$data['ShopCategory']['createtime'] = time();
if ($this->load($data) && $this->save()) {
return true;
}
return false;
}
public function getData()
{//获取sopcategory类的数据
$cates = self::find()->all();
//列成数组
$cates = ArrayHelper::toArray($cates);
return $cates;//返回
}
public function getTree($cates, $pid = 0)//获取树的子类
{
$tree = [];
foreach($cates as $cate) {
if ($cate['parentid'] == $pid) {//如果是顶级分类的话.
$tree[] = $cate;//就把顶级分类放到里面,以次内推.....
$tree = array_merge($tree, $this->getTree($cates, $cate['cateid']));//继续递归调用自己,意思是
//比如新闻
}
}
return $tree;
}
public function setPrefix($data, $p = "|-----")
{
var_dump($data);
$tree = [];
$num = 1;
$prefix = [0 => 1];
while($val = current($data)) {
$key = key($data);//key开始在0的位置
if ($key > 0) {//从第二个开始
if ($data[$key - 1]['parentid'] != $val['parentid']) {
$num ++;
}
}
if (array_key_exists($val['parentid'], $prefix)) {
$num = $prefix[$val['parentid']];
}
$val['title'] = str_repeat($p, $num).$val['title'];
$prefix[$val['parentid']] = $num;
$tree[] = $val;
next($data);
}
return $tree;
}
public function getOptions()
{
$data = $this->getData();
$tree = $this->getTree($data);
$tree = $this->setPrefix($tree);
$options = ['添加顶级分类'];
foreach($tree as $cate) {
$options[$cate['cateid']] = $cate['title'];
}
return $options;
}
public function getTreeList()
{
$data = $this->getData();
$tree = $this->getTree($data);
return $tree = $this->setPrefix($tree);
}
public static function getMenu()
{
$top = self::find()->where('parentid = :pid', [":pid" => 0])->limit(11)->orderby('createtime asc')->asArray()->all();
$data = [];
foreach((array)$top as $k=>$cate) {
$cate['children'] = self::find()->where("parentid = :pid", [":pid" => $cate['cateid']])->limit(10)->asArray()->all();
$data[$k] = $cate;
}
return $data;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
视图:add.php
<?php
use yii\bootstrap\ActiveForm;
use yii\helpers\Html;
?>
<link rel="stylesheet" href="assets/admin/css/compiled/new-user.css" type="text/css" media="screen" />
<!-- main container -->
<div class="content">
<div class="container-fluid">
<div id="pad-wrapper" class="new-user">
<div class="row-fluid header">
<h3>添加新分类</h3>
</div>
<div class="row-fluid form-wrapper">
<!-- left column -->
<div class="span9 with-sidebar">
<div class="container">
<?php
if (Yii::$app->session->hasFlash('info')) {
echo Yii::$app->session->getFlash('info');
}
$form = ActiveForm::begin([
'fieldConfig' => [
'template' => '<div class="span12 field-box">{label}{input}</div>{error}',
],
'options' => [
'class' => 'new_user_form inline-input',
],
]);
echo $form->field($model, 'parentid')->dropDownList($list);
echo $form->field($model, 'title')->textInput(['class' => 'span9']);
?>
<div class="span11 field-box actions">
<?php echo Html::submitButton('添加', ['class' => 'btn-glow primary']);?>
<span>OR</span>
<?php echo Html::resetButton('取消', ['class' => 'reset']); ?>
<?php echo Html::a('返回', ['cyg/list'], ['class' => 'btn btn-primary fanhui']);?>
</div>
<?php ActiveForm::end(); ?>
</div>
</div>
<!-- side right column -->
</div>
</div>
</div>
</div>
<!-- end main container -->
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
cates.php
<link rel="stylesheet" href="assets/admin/css/compiled/user-list.css" type="text/css" media="screen" />
<!-- main container -->
<div class="content">
<div class="container-fluid">
<div id="pad-wrapper" class="users-list">
<div class="row-fluid header">
<h3>分类列表</h3>
<div class="span10 pull-right">
<a href="<?php echo yii\helpers\Url::to(['cyg/add']) ?>" class="btn-flat success pull-right">
<span>+</span>
添加新分类
</a>
</div>
</div>
<?php
if (Yii::$app->session->hasFlash('info')) {
echo Yii::$app->session->getFlash('info');
}
?>
<!-- Users table -->
<div class="row-fluid table">
<table class="table table-hover">
<thead>
<tr>
<th class="span3 sortable">
<span class="line"></span>分类ID
</th>
<th class="span3 sortable">
<span class="line"></span>分类名称
</th>
<th class="span3 sortable align-right">
<span class="line"></span>操作
</th>
</tr>
</thead>
<tbody>
<!-- row -->
<?php foreach($cates as $cate): ?>
<tr class="first">
<td>
<?php echo $cate['cateid'] ?>
</td>
<td>
<?php echo $cate['title'] ; ?>
</td>
<td class="align-right">
<a href="<?php echo yii\helpers\Url::to(['cyg/mod', 'cateid' => $cate['cateid']]); ?>">编辑</a>
<a href="<?php echo yii\helpers\Url::to(['cyg/del', 'cateid' => $cate['cateid']]); ?>">删除</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<div class="pagination pull-right">
<?php /*echo yii\widgets\LinkPager::widget([
'pagination' => $pager,
'prevPageLabel' => '‹',
'nextPageLabel' => '›',
]);*/ ?>
</div>
<!-- end users table -->
</div>
</div>
</div>
<!-- end main container -->
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
结构:
效果图
文章来源: blog.csdn.net,作者:贵哥的编程之路(喜欢分享),版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/qq_37805832/article/details/122242478
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)