thinkphp5有哪些实用入门进阶知识点和各种常用功能代码

免费教程   2024年05月10日 10:32  

本篇内容主要讲解“thinkphp5有哪些实用入门进阶知识点和各种常用功能代码”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“thinkphp5有哪些实用入门进阶知识点和各种常用功能代码”吧!

【ThinkPHP版本查询】dump(THINK_VERSION);模板获取get参数

{$Think.get.pageNumber}或者$Request.param.name(参数名)

【循环嵌套标签】<selectclass="form-controlm-b"name="parentid"><optionvalue="0"selected>〓作为顶级分类〓</option>{volistname='catone'id='vo'}<optionvalue="{$vo.id}"{ifcondition="input('parentid',0)eq$vo.id"}selected{/if}>{$vo.catname}</option>{/volist}</select>模板循环标签{volist}{/volist}标签遍历

【offset 开始遍历的地方】【length 遍历的长度,循环的次数】【mod 与当前数取余】【empty 为空时显示】【key 循环的次数】

  <h2>这是view/index/index.html</h2>  {volistname="list"id="vo"offset="0"length="3"mod="2"empty="这里没有数据"key='s'}<p>{$mod}:{$s}:{$vo.name}</p>  {/volist}{foreach}{/foreach}标签遍历方法一{foreach$listas$vo}  <p>{$vo.name}:{$vo.email}</p>{/foreach}方法二  {foreachname="list"asitem="vo"}    <p>{$key}:{$vo.name}:{$vo.email}</p>【$key数组的下标】  {/foreach}{for}{/for}标签循环<body>  {forstart="1"end="10"step="2"name="i"}【start相当于for循环中的$i=1】【end相当于for循环中的$i<10】【strp步进值】【name默认为i,对应$i】    <p>{$i}</p>  {/for}</body>【多个查询条件判断】非常实用//检查分类名称和分类目录是否重名$count_one=Db::name('category')->where('id','<>',$id)->where('catname',input('post.catname'))->count();$count_two=Db::name('category')->where('id','<>',$id)->where('catdir',input('post.catdir'))->count();if($count_one){returnerror('分类名称重名!');}elseif($count_two){returnerror('分类目录重名!');}【单选框条件判断】<!--IF判断或者三元运算符(更简单,推荐)--><!--注意:三元运算条件判断只能用==,不能用eq(不能解析)--><!--($catinfo.isend==1)?'checked':''可以简写成:$catinfo.isend?'checked':''--><!--开启:--><inputtype="radio"value="1"name="ismenu"{$catinfo.ismenu?'checked':''}><!--隐藏:--><inputtype="radio"value="0"name="ismenu"{$catinfo.ismenu?'':'checked'}>【模板中三层循环】{volistname="menu"id="vo"}<li><ahref="#"rel="externalnofollow"><iclass="fa{$vo.icon}"></i><spanclass="nav-label">{$vo.name}</span><spanclass="faarrow"></span></a>{eqname="vo.child"value="1"}<ulclass="navnav-second-level">{volistname="vo.son"id="voson"}<li><a{eqname="voson.child"value="0"}class="J_menuItem"{/eq}href="{ifcondition='voson.childeq1'}#{else/}{:url($voson.module.'/'.$voson.controller.'/'.$voson.action)}{/if}"rel="externalnofollow">{$voson.name}{eqname="voson.child"value="1"}<spanclass="faarrow"></span>{/eq}</a>{eqname="voson.child"value="1"}<ulclass="navnav-third-level">{volistname="voson.son"id="voend"}<li><aclass="J_menuItem"href="{:url($voend.module.'/'.$voend.controller.'/'.$voend.action)}"rel="externalnofollow">{$voend.name}</a></li>{/volist}</ul>{/eq}</li>{/volist}</ul>{/eq}</li>{/volist}【未定义变量】{$catinfo.catname ?''}//设置异常错误报错级别,关闭notice错误error_reporting(E_ALL^E_NOTICE);获取单个字段值

想直接获取单个字段值,弄了半天,tp5的getField()方法变了,具体如下:TP5中的getField():拆分为value和column了例子:

&bull;&bull;&bull; where("id = 1")->value("title"); 输出:(string) title&bull;&bull;&bull; where("id = 1")->column("title"); 输出:(array)

【对象转数组】

$this->toArray();

【接收表单单个变量值】

input('post.tab');

【接收表单数组】

input('post.order/a');

【接收链接数据】

input('parentid',0)

【模型中新增数据】

save()

【控制器中新增数据】

insert()

【引用模型别名】

use app\admin\model\Category as CategoryModel;

【助手函数】

用助手函数Db,可以不用引用命名空间

【静态方法调用】

外部用类名::方法名,内部用self::方法名

【判断第三层分类下不能勾选子分类条件】只要判断上级分类是第二层,就说明新添加分类为第三层,则不能勾选子分类选项$parentid=Db::name('menu')->where('id',input('post.parentid'))->value('parentid');if($parentid&&input('post.child')){returnerror('不能勾选拥有子菜单项!');}【单选框和复选框默认值】

前台变量如果值为0,提交则没有该变量,存入数据库则为默认值。解决方法有二:方法一:修改数据表的默认值为0方法二:控制器中判断,判断提交数据中是否有该变量,没有则设置该变量值为0

【插入数据调整信息:修改器】protected$insert=['addtime'];//addtime修改器protectedfunctionsetAddtimeAttr($value){returndate('Y-m-dH:i:s');}【读取磁盘文件】constnewModelSql='./data/sfox_newmodel.sql';$newModelSql=file_get_contents(self::newModelSql);【获取模板文件名】$handle=opendir('../template/default/temp/');while($file=readdir($handle)){if($file!='.'&&$file!='..'){$files[]['name']=$file;}}【原生态删除数据表】$dbPrefix=config('database.prefix');Db::execute("DROPTABLE`{$dbPrefix}{$tablename}`;");【原生态重命名数据表】$dbPrefix=config('database.prefix');Db::execute("RENAMETABLE`{$dbPrefix}{$oldTableName}`TO`{$dbPrefix}{$newTableName}`;");【原生态更改数据表某字段值】UPDATEtp_models_fieldSETissystem=0WHEREmodelid=35;【原生态修改数据表字段名称】ALTERTABLE`ps_test`DROPCOLUMN`{$info['field']}`;【原生态添加数据表字段名称】ALTERTABLE`ps_test`ADD`{$fieldname}`VARCHAR(255)NOTNULLDEFAULT'{$defaultvalue}'【insert into table 插入多条数据】INSERTINTOtablenameVALUES(item1,price1,qty1),(item2,price2,qty2),(item3,price3,qty3);【转数组格式】

方法一:$settings = array('setting'=>$data_setting);方法二:$settings['setting'] = $data_setting;(推荐)

模型专题字符串查询(预处理机制)$models=newModelsModel;//判断模型是否存在,采用字段串条件查询,配合预处理机制if($models::where("id!=:idAND(tablename=:tablenameORname=:name)")->bind(['id'=>$id,'tablename'=>$data['tablename'],'name'=>$data['name']])->count()){returnerror('模型已经存在!');exit;}【多个条件或判断】whereOr()//判断新模型是否存在$models=newModelsModel;if($models::where('tablename',$data['tablename'])->whereOr('name',$data['name'])->count()){returnerror('模型已经存在!');exit();}【多个条件或判断】where()//判断新模型是否存在$models=newModelsModel;if($models::where('tablename',$data['tablename'])->where('name',$data['name'])->count()){returnerror('模型已经存在!');exit();}前台指定调用条数

offset=0 length=4(从第一条开始,总共调用4条数据

<ulclass="qy-mod-ul">{volistname="today_hot_list"id="thl_vo"offset=0length=4}<liclass="qy-mod-li"><divclass="qy-mod-imghorizon"><divclass="qy-mod-link-wrap"><ahref="/index/play?id={$thl_vo.id}"rel="externalnofollow"rel="externalnofollow"target="_blank"title="{$thl_vo.title}"class="qy-mod-link"><imgsrc="{$thl_vo.img}"rseat="712211_focus_juchangimage"alt="{$thl_vo.title}"class="qy-mod-cover"><divclass="icon-tl"></div><divclass="icon-bl"></div></a></div><divclass="title-wrap"><pclass="main"><atarget="_blank"title="{$thl_vo.title}"href="/index/play?id={$thl_vo.id}"rel="externalnofollow"rel="externalnofollow"rseat="712211_focus_juchangtitle"class="link-txt">{$thl_vo.title}</a></p></div></div></li>{/volist}</ul>奇偶循环调用

$key:是从0开始的$i:是从1开始的思路:取模运算,当是奇数的时候,循环输出奇数和偶数内容

{volistname="channel_list"id="cvo"}{ifcondition="$i%2eq1"}<divclass="nav-list"><divclass="nav-list-item"><atarget="_blank"rseat="712211_channel_yule"href="/index.php/index/index/cate?label_channel={$cvo.id}"rel="externalnofollow"class="nav-list-link">{$cvo.title}</a></div><divclass="nav-list-item"><atarget="_blank"rseat="712211_channel_zixun"href="/index.php/index/index/cate?label_channel=<?phpecho$channel_list[$key+1]['id']?>"rel="externalnofollow"class="nav-list-link"><?phpecho$channel_list[$key+1]['title']?></a></div></div>{/if}{/volist}自动切换1、前端模板<divid="piclist"class="qy-focus-index-list"><ulclass="focus-index-list">{volistname="data"id="ivo"}<liclass="focus-index-item"rseat="fcs_0_p<?phpecho$i;?>"><atarget="_blank"href="{$ivo.url}"rel="externalnofollow"class="focus-index-itemLink"><imgsrc="{$ivo.img}"></a></li>{/volist}</ul></div><divclass="qy-focus-side-panel"><divclass="focus-side-inner"><ulid="txtlist"class="focus-side-list">{volistname="data"id="vo"}<liclass="focus-side-item<?phpif($i==1){echo'selected';}?>"><atitle="{$vo.title}"rseat="{$i}"class="focus-side-itemLink">{$vo.title}</a></li>{/volist}</ul></div></div>2、JS功能实现<scripttype="text/javascript"src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script><scripttype="text/javascript">$('.focus-side-itemLink').on('mouseover',function(){$(this).parent('li').addClass('selected').siblings('li').removeClass('selected');vari=$(this).attr('rseat');$('.focus-index-listli[rseat="fcs_0_p'+i+'"]').show().siblings('li').hide();});</script>加红关键字<ahref="">{$v.username|str_replace=$keyword,'<fontexternalnofollow"color:red">'.$keyword.'</font>',###}</a>

到此,相信大家对“thinkphp5有哪些实用入门进阶知识点和各种常用功能代码”有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

域名注册
购买VPS主机

您或许对下面这些文章有兴趣:                    本月吐槽辛苦排行榜

看贴要回贴有N种理由!看帖不回贴的后果你懂得的!


评论内容 (*必填):
(Ctrl + Enter提交)   

部落快速搜索栏

各类专题梳理

网站导航栏

X
返回顶部