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. hadoop-04-mysql安装

    hadoop-04-mysql安装 su root 1,rpm -qa|grep mysql 2, rpm -e --nodeps `rpm -qa|grep mysql` 3,rpm -ivh co ...

  2. [Transducer] Create a Sequence Helper to Transduce Without Changing Collection Types

    A frequent use case when transducing is to apply a transformation to items without changing the type ...

  3. 物理读之LRU(近期最少被使用)的深入解析

    转载请注明出处: http://blog.csdn.net/guoyjoe/article/details/38264883 一组LRU链表包含LRU主链.LRU辅助链.LRUW主链,LRUW辅助链, ...

  4. Centos7+httpd+fastcgi+rails安装

    搭建的环境: centos7 Apache/2.4.6 fastcgi2.4.6 rails4 在安装fastcgi的时候遇到了问题: 问题: .... .. In file included fro ...

  5. 使用React Hook后的一些体会

    一.前言 距离React Hook发布已经有一段时间了,笔者在之前也一直在等待机会来尝试一下Hook,这个尝试不是像文档中介绍的可以先在已有项目中的小组件和新组件上尝试,而是尝试用Hook的方式构建整 ...

  6. 纳德拉再造微软:市值如何重回第一阵营(思维确实变了,不再是以windows为中心,拥抱其它各种平台,敢在主战场之外找到适合自己的新战场)

    有人说,现在的美国硅谷充满了“咖喱味”.也有人说,硅谷已经变成“印度谷”.原因就在于,以微软CEO萨提亚·纳德拉.谷歌CEO桑达尔·皮查伊为代表的印度人,近年以来掌控了全世界最令人望而生畏的科技巨头. ...

  7. 自定义标签 Unable to find setter method for attribute

    变量的首字母不能大写 http://blog.csdn.net/looksun/article/details/7690601

  8. 安卓4.3以上版本已经完美支持BLE(英文版)

    Android 4.3 (API Level 18) introduces built-in platform support for Bluetooth Low Energy in the cent ...

  9. pandas groupby 分组操作

    最一般化的groupby 方法是apply. tips=pd.read_csv('tips.csv') tips[:5] 新生成一列 tips['tip_pct']=tips['tip']/tips[ ...

  10. mDNS原理的简单理解——每个进入局域网的主机,如果开启了mDNS服务的话,都会向局域网内的所有主机组播一个消息,我是谁,和我的IP地址是多少。然后其他也有该服务的主机就会响应,也会告诉你,它是谁,它的IP地址是多少

    MDNS协议介绍 mDNS multicast DNS , 使用5353端口,组播地址 224.0.0.251.在一个没有常规DNS服务器的小型网络内,可以使用mDNS来实现类似DNS的编程接口.包格 ...