thinkphp5實用入門進階知識點和各種常用功能代碼匯總
【ThinkPHP版本查詢】
dump(THINK_VERSION);
模板獲取get參數(shù)
{$Think.get.pageNumber}
或者$Request.param.name(參數(shù)名)
【循環(huán)嵌套標簽】
<select class="form-control m-b" name="parentid"> <option value="0" selected>〓 作為頂級分類 〓</option> {volist name='catone' id='vo'} <option value="{$vo.id}" {if condition="input('parentid',0) eq $vo.id"}selected{/if}>{$vo.catname}</option> {/volist} </select>
模板循環(huán)標簽
{volist}{/volist}標簽遍歷
【offset 開始遍歷的地方】
【length 遍歷的長度,循環(huán)的次數(shù)】
【mod 與當前數(shù)取余】
【empty 為空時顯示】
【key 循環(huán)的次數(shù)】
<h1>這是view/index/index.html</h1> {volist name="list" id="vo" offset="0" length="3" mod="2" empty="這里沒有數(shù)據(jù)" key ='s'} <p>{$mod}:{$s}:{$vo.name}</p> {/volist}
{foreach}{/foreach}標簽遍歷
方法一
{foreach $list as $vo} <p>{$vo.name}:{$vo.email}</p> {/foreach}
方法二
{foreach name="list" as item="vo"} <p>{$key} : {$vo.name} : {$vo.email}</p> 【$key 數(shù)組的下標】 {/foreach}
{for}{/for}標簽循環(huán)
<body> {for start="1" end="10" step="2" name="i"} 【start 相當于for循環(huán)中的$i=1】【end 相當于for循環(huán)中的$i<10】【strp 步進值】【name 默認為i,對應(yīng)$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){ return error('分類名稱重名!'); }else if($count_two){ return error('分類目錄重名!'); }
【單選框條件判斷】
<!--IF判斷或者三元運算符(更簡單,推薦)--> <!--注意:三元運算條件判斷只能用==,不能用eq(不能解析)--> <!--($catinfo.isend == 1) ? 'checked' : '' 可以簡寫成:$catinfo.isend ? 'checked' : ''--> <!--開啟:--> <input type="radio" value="1" name="ismenu" {$catinfo.ismenu ? 'checked' : ''}> <!--隱藏:--> <input type="radio" value="0" name="ismenu" {$catinfo.ismenu ? '' : 'checked'}>
【模板中三層循環(huán)】
{volist name="menu" id="vo"} <li> <a href="#" rel="external nofollow" ><i class="fa {$vo.icon}"></i> <span class="nav-label">{$vo.name}</span><span class="fa arrow"></span></a> {eq name="vo.child" value="1"} <ul class="nav nav-second-level"> {volist name="vo.son" id="voson"} <li> <a {eq name="voson.child" value="0"}class="J_menuItem"{/eq} href="{if condition='voson.child eq 1'}#{else /}{:url($voson.module.'/'.$voson.controller.'/'.$voson.action)}{/if}" rel="external nofollow" >{$voson.name} {eq name="voson.child" value="1"}<span class="fa arrow"></span>{/eq}</a> {eq name="voson.child" value="1"} <ul class="nav nav-third-level"> {volist name="voson.son" id="voend"} <li><a class="J_menuItem" href="{:url($voend.module.'/'.$voend.controller.'/'.$voend.action)}" rel="external nofollow" >{$voend.name}</a></li> {/volist} </ul> {/eq} </li> {/volist} </ul> {/eq} </li> {/volist}
【未定義變量】{$catinfo.catname ?''}
// 設(shè)置異常錯誤報錯級別,關(guān)閉notice錯誤 error_reporting(E_ALL ^ E_NOTICE);
獲取單個字段值
想直接獲取單個字段值,弄了半天,tp5的getField()方法變了,具體如下:
TP5中的getField():拆分為value和column了
例子:
••• where("id = 1")->value("title"); 輸出:(string) title
••• where("id = 1")->column("title"); 輸出:(array)
【對象轉(zhuǎn)數(shù)組】
$this->toArray();
【接收表單單個變量值】
input('post.tab');
【接收表單數(shù)組】
input('post.order/a');
【接收鏈接數(shù)據(jù)】
input('parentid',0)
【模型中新增數(shù)據(jù)】
save()
【控制器中新增數(shù)據(jù)】
insert()
【引用模型別名】
use app\admin\model\Category as CategoryModel;
【助手函數(shù)】
用助手函數(shù)Db,可以不用引用命名空間
【靜態(tài)方法調(diào)用】
外部用類名::方法名,內(nèi)部用self::方法名
【判斷第三層分類下不能勾選子分類條件】
只要判斷上級分類是第二層,就說明新添加分類為第三層,則不能勾選子分類選項
$parentid = Db::name('menu')->where('id',input('post.parentid'))->value('parentid'); if($parentid && input('post.child')){ return error('不能勾選擁有子菜單項!'); }
【單選框和復(fù)選框默認值】
前臺變量如果值為0,提交則沒有該變量,存入數(shù)據(jù)庫則為默認值。解決方法有二:
方法一:修改數(shù)據(jù)表的默認值為0
方法二:控制器中判斷,判斷提交數(shù)據(jù)中是否有該變量,沒有則設(shè)置該變量值為0
【插入數(shù)據(jù)調(diào)整信息:修改器】
protected $insert = ['addtime']; //addtime修改器 protected function setAddtimeAttr($value){ return date('Y-m-d H:i:s'); }
【讀取磁盤文件】
const newModelSql = './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; } }
【原生態(tài)刪除數(shù)據(jù)表】
$dbPrefix = config('database.prefix'); Db::execute("DROP TABLE `{$dbPrefix}{$tablename}`;");
【原生態(tài)重命名數(shù)據(jù)表】
$dbPrefix = config('database.prefix'); Db::execute("RENAME TABLE `{$dbPrefix}{$oldTableName}` TO `{$dbPrefix}{$newTableName}` ;");
【原生態(tài)更改數(shù)據(jù)表某字段值】
UPDATE tp_models_field SET issystem=0 WHERE modelid=35;
【原生態(tài)修改數(shù)據(jù)表字段名稱】
ALTER TABLE `ps_test` DROP COLUMN `{$info['field']}` ;
【原生態(tài)添加數(shù)據(jù)表字段名稱】
ALTER TABLE `ps_test` ADD `{$fieldname}` VARCHAR(255) NOT NULL DEFAULT '{$defaultvalue}'
【insert into table 插入多條數(shù)據(jù)】
INSERT INTO tablename VALUES(item1, price1, qty1),(item2, price2, qty2),(item3, price3, qty3);
【轉(zhuǎn)數(shù)組格式】
方法一:$settings = array('setting'=>$data_setting);
方法二:$settings['setting'] = $data_setting;(推薦)
模型專題
字符串查詢(預(yù)處理機制)
$models = new ModelsModel; //判斷模型是否存在,采用字段串條件查詢,配合預(yù)處理機制 if($models::where("id!=:id AND (tablename=:tablename OR name=:name)") ->bind([ 'id'=>$id, 'tablename'=>$data['tablename'], 'name'=>$data['name'] ])->count()){ return error('模型已經(jīng)存在!'); exit; }
【多個條件或判斷】whereOr()
//判斷新模型是否存在 $models = new ModelsModel; if($models::where('tablename',$data['tablename'])->whereOr('name',$data['name'])->count()){ return error('模型已經(jīng)存在!'); exit(); }
【多個條件或判斷】where()
//判斷新模型是否存在 $models = new ModelsModel; if($models::where('tablename',$data['tablename'])->where('name',$data['name'])->count()){ return error('模型已經(jīng)存在!'); exit(); }
前臺指定調(diào)用條數(shù)
offset=0 length=4(從第一條開始,總共調(diào)用4條數(shù)據(jù)
<ul class="qy-mod-ul"> {volist name="today_hot_list" id="thl_vo" offset=0 length=4} <li class="qy-mod-li"> <div class="qy-mod-img horizon"> <div class="qy-mod-link-wrap"> <a href="/index/play?id={$thl_vo.id}" rel="external nofollow" rel="external nofollow" target="_blank" title="{$thl_vo.title}" class="qy-mod-link"> <img src="{$thl_vo.img}" rseat="712211_focus_juchangimage" alt="{$thl_vo.title}" class="qy-mod-cover"> <div class="icon-tl"></div> <div class="icon-bl"></div> </a></div> <div class="title-wrap"><p class="main"> <a target="_blank" title="{$thl_vo.title}" href="/index/play?id={$thl_vo.id}" rel="external nofollow" rel="external nofollow" rseat="712211_focus_juchangtitle" class="link-txt"> {$thl_vo.title} </a> </p></div> </div> </li> {/volist} </ul>
奇偶循環(huán)調(diào)用
$key:是從0開始的
$i:是從1開始的
思路:取模運算,當是奇數(shù)的時候,循環(huán)輸出奇數(shù)和偶數(shù)內(nèi)容
{volist name="channel_list" id="cvo"} {if condition="$i%2 eq 1"} <div class="nav-list"> <div class="nav-list-item"><a target="_blank" rseat="712211_channel_yule" href="/index.php/index/index/cate?label_channel={$cvo.id}" rel="external nofollow" class="nav-list-link">{$cvo.title}</a> </div> <div class="nav-list-item"><a target="_blank" rseat="712211_channel_zixun" href="/index.php/index/index/cate?label_channel=<?php echo $channel_list[$key + 1]['id']?>" rel="external nofollow" class="nav-list-link"><?php echo $channel_list[$key + 1]['title']?></a> </div> </div> {/if} {/volist}
自動切換
1、前端模板
<div id="piclist" class="qy-focus-index-list"> <ul class="focus-index-list"> {volist name="data" id="ivo"} <li class="focus-index-item" rseat="fcs_0_p<?php echo $i;?>" style=" opacity: 1;<?php if($i>1){echo 'display: none;';}?>"> <a target="_blank" href="{$ivo.url}" rel="external nofollow" class="focus-index-itemLink"><img src="{$ivo.img}"></a> </li> {/volist} </ul> </div> <div class="qy-focus-side-panel"> <div class="focus-side-inner"> <ul id="txtlist" class="focus-side-list"> {volist name="data" id="vo"} <li class="focus-side-item<?php if($i==1){echo ' selected';}?>"> <a title="{$vo.title}" rseat="{$i}" class="focus-side-itemLink">{$vo.title}</a> </li> {/volist} </ul> </div> </div>
2、JS功能實現(xiàn)
<script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script> <script type="text/javascript"> $('.focus-side-itemLink').on('mouseover',function(){ $(this).parent('li').addClass('selected').siblings('li').removeClass('selected'); var i = $(this).attr('rseat'); $('.focus-index-list li[rseat="fcs_0_p'+i+'"]').show().siblings('li').hide(); }); </script>
加紅關(guān)鍵字
<a href="">{$v.username|str_replace=$keyword, '<font style=" rel="external nofollow" color:red">' . $keyword . '</font>', ###}</a>
到此這篇關(guān)于thinkphp5實用入門進階知識點和各種常用功能代碼匯總的文章就介紹到這了,更多相關(guān)thinkphp5常用功能代碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
php操作JSON格式數(shù)據(jù)的實現(xiàn)代碼
php操作JSON格式數(shù)據(jù)的實現(xiàn)代碼,需要的朋友可以參考下。2011-12-12php中ob_flush函數(shù)和flush函數(shù)用法分析
這篇文章主要介紹了php中ob_flush函數(shù)和flush函數(shù)用法,實例分析了ob_flush函數(shù)和flush函數(shù)的功能及相關(guān)的使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-03-03PHP中 empty() 和 isset() 的區(qū)別介紹
作為PHP中經(jīng)常用來判斷變量是否為空的函數(shù):empty()和isset() ,二者其實在很多方面還是存在區(qū)別的,本文將為大家詳細介紹一下他們的區(qū)別之處,需要的朋友可以了解一下2021-12-12PHP網(wǎng)頁緩存技術(shù)優(yōu)點及代碼實例
這篇文章主要介紹了PHP網(wǎng)頁緩存技術(shù)優(yōu)點及代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-07-07PHP中date()日期函數(shù)有關(guān)參數(shù)整理
PHP中date()日期函數(shù)有關(guān)參數(shù)整理,需要的朋友可以參考下。2011-07-07原生JavaScript+PHP多圖上傳實現(xiàn)示例
這篇文章主要為大家介紹了原生JavaScript+PHP多圖上傳實現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-08-08