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. BZOJ 3325 [SCOI2013]密码 (逆模拟Manacher+构造)

    题目大意:给你一个字符串每个位置和相邻两个位置为回文中心的最长回文串长度,让你构造一个合法的字典序最小的字符串 挺有意思的构造题 首先按照$Manacher$的思想还原$p$数组 定义$f_{ij}$ ...

  2. 【CS-4476-project 6】Deep Learning

    AlexNet / VGG-F network visualized by mNeuron. Project 6: Deep LearningIntroduction to Computer Visi ...

  3. The Zen of Python, by Tim Peters

    Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Comp ...

  4. POJ——T 1469 COURSES

    http://poj.org/problem?id=1469 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24197   ...

  5. WinCE的C#中使用StreamReader 来读取TXT文档,读取文本文档。

    using System.IO; private void button1_Click(object sender, EventArgs e) { string strFilePath = " ...

  6. 插入排序、冒泡排序、选择排序、希尔排序、高速排序、归并排序、堆排序和LST基数排序——C++实现

    首先是算法实现文件Sort.h.代码例如以下: <pre name="code" class="java">/* * 实现了八个经常使用的排序算法: ...

  7. Android控件篇

    Android中提供了丰富的UI空间.为了最大限度地发挥平台的性能.每个开发人员必须熟练掌握UI控件尤其是经常使用的UI控件.并能依据须要呈现的内容选择最恰当的控件. Android提供了XML配置和 ...

  8. POJ 2739 Sum of Consecutive Prime Numbers(素数)

    POJ 2739 Sum of Consecutive Prime Numbers(素数) http://poj.org/problem? id=2739 题意: 给你一个10000以内的自然数X.然 ...

  9. Atitit.html解析器的选型&#160;jsoup&#160;nsoup&#160;,java&#160;c#&#160;.net&#160;版本号

    Atitit.html解析器的选型 jsoup nsoup ,java c# .net 版本号 1. 框架选型的要求 1 1.1. 文档多 1 1.2. 跨平台 1 2. html解析器特性: 1 2 ...

  10. svn服务器的搭建过程 主要为服务端

    yum -y install subversion 查看版本信息 svnserve --version 创建svn仓库目录 mkdir -p /var/svn/svnrepos 创建svn仓库 svn ...