thinkphp5项目--企业单车网站(六)

项目地址

fry404006308/BicycleEnterpriseWebsite: Bicycle Enterprise Website
https://github.com/fry404006308/BicycleEnterpriseWebsite

1、thinkphp数据库字段操作很多都支持直接数组方式

$res=db('cate')->update($dataIn);

2、数组的访问方式

数组在控制器中是['']的访问方式

if($v['pid']==$id) return ture;

数组在视图界面是.(点)的访问方式

<td >{if condition="$vo.level neq 0"}|{/if}<?php echo str_repeat('----',$vo['level'])?>{$vo.catename}</td>

3、关于编程的教学

直接按照书上的一章一章的讲,太无趣了。因为这样一章一章的书直接叫做参考手册好了,比之于直接叫做书的话。

直接以项目起手,用到哪,讲到哪,整个项目做完,再来整个系统讲解所有知识点(可以结合项目的案例讲啊)。

我教算法的经历,我自学编程的经历,学java,学php,都验证了这一点。

4、控制器前置操作

前置操作

可以为某个或者某些操作指定前置执行的操作方法,设置 beforeActionList 属性可以指定某个方法为其
他方法的前置操作,数组键名为需要调用的前置方法名,无值的话为当前控制器下所有方法的前置方法。
['except' => '方法名,方法名']
表示这些方法不使用前置方法,
['only' => '方法名,方法名']
表示只有这些方法使用前置方法。
示例如下: namespace app\index\controller;
use think\Controller;
class Index extends Controller
{
protected $beforeActionList = [
'first',
'second' => ['except'=>'hello'],
'three' => ['only'=>'hello,data'],
];
protected function first()
{
echo 'first<br/>';
}
protected function second()
{
echo 'second<br/>';
}
protected function three()
{
echo 'three<br/>';
}
public function hello()
{
return 'hello';
}
public function data()
{
return 'data';
}
}

访问
http://localhost/index.php/index/Index/hello
最后的输出结果是
first
three
hello

访问
http://localhost/index.php/index/Index/data
的输出结果是:
first
second
three
data

项目实例:

无限分类中删除栏目的话删除所有孩子

控制器:

 <?php
namespace app\admin\controller;
use think\Controller;
use app\admin\model\Cate as ModelCate; use app\admin\controller\Base; class Cate extends Base
{
//前置方法
protected $beforeActionList=[
'delChilden'=>['only'=>'del'],
]; //删除无限分类的第二种方法:删除掉这个栏目及所有的孩子
public function del(){
//1、在前置方法里面删掉所有孩子
//2、在当前方法里面删除这条数据
$id = input('id');
$res = db('cate')->delete($id);
if($res){
$this->success('删除栏目成功','cate/lst');
}else{
$this->error('删除栏目失败');
}
} //删除前删除所有的孩子
public function delChilden(){
//1、获取要删除孩子的栏目id
$id = input('id');
//2、在模型中找到这个id对应的所有孩子
$modelCate=new ModelCate();
$ids=$modelCate->getChilden($id);
//3、在数据库中删除所有孩子
if($ids){
$res = db('cate')->delete($ids);
if(!$res) $this->error('删除当前栏目的子栏目失败');
} } }

模型:

 <?php
namespace app\admin\model;
use think\Model; class Cate extends Model
{ //获得指定id的所有孩子的数组
public function getChilden($id){
$data=$this->select();
$res=$this->getChildenId($data,$id);
// dump($res);die;
return $res;
} //获得指定id的所有孩子的数组
public function getChildenId($data,$id){
static $arr=array();
foreach ($data as $k => $v) {
if($v['pid']==$id){
$arr[]=$v['id'];
$this->getChildenId($data,$v['id']);
}
}
return $arr;
}
}

5、php递归使用

 /**
* 无线分类重新排序:使得那些栏目的顺序是对的,父级栏目在子级标题之上
* 这其实是一个再简单的递归也没有了,作为递归,访问标签页没有加
* @param [type] $data 传入的栏目数组
* @param integer $pid 父级栏目id,顶级栏目的id为0
* @param integer $level 栏目等级,初始等级为0
* @return [type] 排序好的栏目的数据
*/
public function sort($data,$pid=0,$level=0){
static $arr=array();
foreach ($data as $k => $v) {
//如果子级的父级id等于传传过来要查找的父级id,说明自己是这个父级id的孩子
if($v['pid']==$pid){
$v['level']=$level;
$arr[]=$v;
//在这个$data数组中去递归找它的孩子
$this->sort($data,$v['id'],$level+1);
}
}
return $arr;
}

还要注意这里的默认参数

6、无限分类删除

如果做无限分类的时候删除栏目的时候想删除这个栏目及所有孩子,算法思路如下

1、递归找到它的所有孩子的id(这个挺简单的)

2、用delete的批量删除主键的方法即可删除(查看手册里面的批量删除)

Db::table('think_user')->delete([1,2,3]);

7、静态数组使用

 //获得指定id的所有孩子的数组
public function getChildenId($data,$id){
static $arr=array();
foreach ($data as $k => $v) {
if($v['pid']==$id){
$arr[]=$v['id'];
$this->getChildenId($data,$v['id']);
}
}
return $arr;
}

看声明,=array()

看赋值,$arr[]=

thinkphp5项目--企业单车网站(六)的更多相关文章

  1. thinkphp5项目--企业单车网站(四)

    thinkphp5项目--企业单车网站(四) 项目地址 fry404006308/BicycleEnterpriseWebsite: Bicycle Enterprise Websitehttps:/ ...

  2. thinkphp5项目--企业单车网站(三)

    thinkphp5项目--企业单车网站(三) 项目地址 fry404006308/BicycleEnterpriseWebsite: Bicycle Enterprise Websitehttps:/ ...

  3. thinkphp5项目--企业单车网站(五)

    thinkphp5项目--企业单车网站(五) 项目地址 fry404006308/BicycleEnterpriseWebsite: Bicycle Enterprise Websitehttps:/ ...

  4. thinkphp5项目--企业单车网站(二)

    thinkphp5项目--企业单车网站(二) 项目地址 fry404006308/BicycleEnterpriseWebsite: Bicycle Enterprise Websitehttps:/ ...

  5. thinkphp5项目--企业单车网站(一)

    thinkphp5项目--企业单车网站(一) 项目地址 fry404006308/BicycleEnterpriseWebsite: Bicycle Enterprise Websitehttps:/ ...

  6. thinkphp5项目--企业单车网站(七)

    thinkphp5项目--企业单车网站(七) 项目地址 fry404006308/BicycleEnterpriseWebsite: Bicycle Enterprise Websitehttps:/ ...

  7. thinkphp5项目--企业单车网站(八)(文章板块要点)(删除图片)

    thinkphp5项目--企业单车网站(八)(文章板块要点)(删除图片) 项目地址 fry404006308/BicycleEnterpriseWebsite: Bicycle Enterprise ...

  8. thinkphp5项目--企业单车网站(九)(加强复习啊)(花了那么多时间写的博客,不复习太浪费了)

    thinkphp5项目--企业单车网站(九)(加强复习啊)(花了那么多时间写的博客,不复习太浪费了) 项目地址 fry404006308/BicycleEnterpriseWebsite: Bicyc ...

  9. thinkphp5项目--练手--企业单车网站(九)(友情链接)

    thinkphp5项目--练手--企业单车网站(九)(友情链接) 项目地址 fry404006308/BicycleEnterpriseWebsite: Bicycle Enterprise Webs ...

随机推荐

  1. Vue中路由的使用

    在Vue中动态挂载组件 首先需要安装  cnpm install vue-router --save 在main.js中引入VueRouter 并使用 定义一个路由 创建router实例 通过rout ...

  2. C# winform压缩文件夹带进度条

    注意:用了开源的CL.IO.Zip库 pbYSJD是进度条的控件名 btnImport是按钮控件名,当压缩结束之后,使按钮处于激活状态,否则无法点击按钮. /// <summary> // ...

  3. Mybatis Generator for SQL Server

    Mybatis Generator for SQL Server <?xml version="1.0" encoding="UTF-8" ?> & ...

  4. jquery-ui日期时间控件实现

    日期控件和时间控件为独立控件,日期时间控件要同一时候导入日期控件和时间控件的js,然后在日期控件加入时间控件显示參数,没有导入时间控件js.日期控件函数设置的时间控件參将包错 日期控件官网网址:htt ...

  5. 在PyCharm中以root权限运行和调试python代码

    python中有的代码可能需要su权限,如 import os os.mkdir('/media/xxx/disk_a') 如果在交互式环境中使用,需要以sudo的方式启动python.而在PyCha ...

  6. 里根上台时国债只占GDP的30%

    学里根是刻舟求剑,关键是钱从哪来 5  里根主要靠借钱,这是冷战红利,美国打完二战国债占了GDP的120%,然后总量就没怎么增加,但战后GDP快速增长,结果国债占GDP的比例连续下降,打越战登月石油危 ...

  7. Nginx搭建图片服务器

    Nginx搭建图片服务器 标签(空格分隔): linux,nginx Nginx常用命令 ./nginx 启动 ./nginx -s reload 重载配置文件 ./nginx -s stop|sta ...

  8. Laravel-自定全局函数

    Laravel-自定全局函数 标签(空格分隔): php 习惯了 使用 ThinkPHP 框架,有一个公共方法类在代码编写上会快捷很多,所以有必要在此进行配置一番. 实现 在 app 创建文件夹 He ...

  9. 安卓开发--HttpDemo01

    package com.cnn.httpdemo01; import android.app.Activity; import android.content.Intent; import andro ...

  10. 2015合肥网络赛 HDU 5489 Removed Interval LIS+线段树(树状数组)

    HDU 5489 Removed Interval 题意: 求序列中切掉连续的L长度后的最长上升序列 思路: 从前到后求一遍LIS,从后往前求一遍LDS,然后枚举切开的位置i,用线段树维护区间最大值, ...