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. 堆(Heap)-c实现

    这个堆的实现采用数组存储的完全二叉树实现. 最近有点烦躁,先是跳槽到了一个外包公司,感觉2016有点坑,另外一件事就是老婆怀孕了,但是在家里没人照顾,很担心. 这个堆的实现就暂时不优化了,基本的插入, ...

  2. 紫书 例题8-11 UVa 10954 (优先队列)

    解法和合并果子是一样的, 每次取最小的两个, 更新答案, 加入队列 #include<cstdio> #include<queue> #define REP(i, a, b) ...

  3. #error 、 #line 和 #pragma 的使用

    1. #error 的用法 (1)#error 是一种预编译器指示字,用于生成一个编译错误消息 (2)用法:#error message //注意:message 不需要用双引号包围 (3)#erro ...

  4. ios的notification机制是同步的还是异步的

    与javascript中的事件机制不同.ios里的事件广播机制是同步的,默认情况下.广播一个通知,会堵塞后面的代码: -(void) clicked { NSNotificationCenter *c ...

  5. google在线測试练习题3

    Problem The Latin alphabet contains 26 characters and telephones only have ten digits on the keypad. ...

  6. Hibernate_9_Person和IdCard实例_一对一关系:基于主键

    1)建立Person类:(与8同样) 2)建立IdCard类:(与8同样) 3)建立持久化类:  1>保存方法(与8同样)  2>获取方法(与8同样)  3>删除方法(与8同样)   ...

  7. vue2.0 路由学习笔记

    昨天温故了一下vue2.0的路由 做个笔记简单记录一下! 1.首相和vue1.0一样 要使用vuejs的路由功能需要先引入vue-router.js 2.然后修改原有a标签处代码 这里以一个ul li ...

  8. angularjs作用域和函数调用

    <!DOCTYPE HTML> <html ng-app> <head> <meta http-equiv="Content-Type" ...

  9. spark scala word2vec 和多层分类感知器在情感分析中的实际应用

    转自:http://www.cnblogs.com/canyangfeixue/p/7227998.html 对于威胁检测算法使用神经网络训练有用!!!TODO待实验 /** * Created by ...

  10. zzulioj--1804--ZY学长的密码(字符串)

    1804: ZY学长的密码 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 140  Solved: 53 SubmitStatusWeb Board ...