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. JavaScript函数练习

    1. 判断一个数是否是素数 function isSushu (n) { n = n || 0; var isSu = true; for (var i = 2; i <= Math.sqrt( ...

  2. redis 多实例监控

    1.制作redis_low_discovery.sh脚本 mkdir -p /data/service/script/zabbix cd /data/service/script/zabbix cat ...

  3. react-native 运行提示红屏 error: bundling failed: ambiguous resolution: module `/User/xxx/Project/ico/index.js` tries to require `react-native`, but there are several files providing this module. You can de

    运行 react-native start 报错 执行这2个进行清除缓存问题 yarn start -- --reset-cache  npm start -- --reset-cache  

  4. iptables 配置端口及转发

    iptables端口转发指令:iptables -t nat -A PREROUTING -p tcp --dport 443 -j REDIRECT --to-port 8443 iptables配 ...

  5. linux中对socket的理解 socket高并发

    1.socket是什么? 其实准确的来说,socket并不仅仅用于linux而已,它也应用于TCP/IP中.笼统的来说,socket就是指的“IP地址+端口号”.比如我有一个ssh服务器A,这时候我有 ...

  6. 微信小程序的开启授权,当单机取消授权后 再次授权

    //单机去搜索 toSearch:function(){ this.getLocation(res => { console.log('成功') wx.navigateTo({ url: `.. ...

  7. 【codeforces 239B】Easy Tape Programming

    [题目链接]:http://codeforces.com/contest/239/problem/B [题意] 给你一个长度为n的字符串,只包括'<">'以及数字0到9; 给你q ...

  8. IP地址的规划和设计方法(二)

    五,IP地址规划方法           (1)IP地址规划的基本步骤           网络地址规划须要按下面6步进行:           a)推断用户对网络与主机数的需求:           ...

  9. 机器学习规则:ML工程最佳实践----rules_of_ml section 2【翻译】

    作者:黄永刚 ML Phase II: 特征工程 第一阶段介绍了机器学习的一个周期,为学习系统获取训练数据,通过有趣的引导设计指标,创建一个服务框架.在有了一个完整系统之后,就进入了第一阶段. 第二阶 ...

  10. java中"".equals(A)与A.equals("")一样不?

    不一样如果a为nulla = null;a.equals("")出错nullPointerException如果写为"".equals(a)-->就可以防 ...