1:關聯查詢並有的字段名相同查詢全部字段
$goodsNum = $cartlistM->field('*,a.id as cid')->join('Goods as b on a.goods_id = b.id')->where(array('a.id' =>$_POST['cartID'] ))->find();
2:查詢按條件去處重複值的語句
M('propertytake')->group('batchnum')->where($w)->select();
3:在循環中去除查詢selsect()中不符合條件的數組
foreach ($propertyArr as $key=>$val)
{
$w['mid'] = $this->mid;
$w['property_id'] = $val['id'];
//xxxxxx
$hasGetYear = count(M('tablename')->group('batchnum')->where($w)->select());
//查詢當前資產在本期已收成的消耗資源數量
$w['batchnum'] = $setBatch;
$temp_num = M('tablename')->where($w)->sum('nums');
if(empty($temp_num))
{
$temp_num = 0;
}
if ($hasGetYear >= $goodsArr['getmaxnum']||$temp_num>=$val['totaltree']||$val['totaltree']<=0) { unset($propertyArr[$key]); }else { $treeNum += $val['totaltree']; } }
4:常用函數
由於內容增加單開一篇
5:thinkPHP的事務處理 例://開啓事務處理本地數據
M()->startTrans();
//提交
M()->commit();
//不提交
M()->rollback();
6:json解析json_decode();json加密json_encode(value);
7:調用父類(被繼承類)
parent::memaddress($list['province'],$list['city'],$list['district']);//parent::是父的意思,memaddress是父類中的函數
8:查詢方法之過濾查詢
distanct(true)
例:
$data=M('user')->distanct(true)->field('score')->order('score asc')->select();
9:thinkPHP在字段值上自增或自減
$newM->where('id='.$cid)->setInc('browser',1); //browser字段值自增1,setDec是自減
10:各種循環(for forech ….) 跳出循環return,break,continue三者區別
break用於完全結束一個循環,跳出循環體。不管是哪種循環,一旦在循環體中遇到break,系統將完全結束循環,開始執行循環之後的代碼。 break不僅可以結束其所在的循環,還可結束其外層循環。此時需要在break後緊跟一個標籤,這個標籤用於標識一個外層循環。Java中的標籤就是一個緊跟着英文冒號(:)的標識符。且它必須放在循環語句之前纔有作用。
public class BreakTest2
{
public static void main(String[] args){
// 外層循環,outer作爲標識符
outer:
for (int i = 0 ; i < 5 ; i++ ){
// 內層循環
for (int j = 0; j < 3 ; j++ ){
System.out.println("i的值爲:" + i + " j的值爲:" + j);
if (j == 1){
// 跳出outer標籤所標識的循環。
break outer;
}
}
}
}
}
continue的功能和break有點類似,區別是continue只是中止本次循環,接着開始下一次循環。而break則是完全中止循環。
public class ContinueTest
{
public static void main(String[] args){
// 一個簡單的for循環
for (int i = 0; i < 3 ; i++ ){
System.out.println("i的值是" + i);
if (i == 1){
// 忽略本次循環的剩下語句
continue;
}
System.out.println("continue後的輸出語句");
}
}
}
return關鍵字並不是專門用於跳出循環的,return的功能是結束一個方法。 一旦在循環體內執行到一個return語句,return語句將會結束該方法,循環自然也隨之結束。與continue和break不同的是,return直接結束整個方法,不管這個return處於多少層循環之內。
public class ReturnTest
{
public static void main(String[] args){
// 一個簡單的for循環
for (int i = 0; i < 3 ; i++ ){
System.out.println("i的值是" + i);
if (i == 1){
return;
}
System.out.println("return後的輸出語句");
}
}
}
11:登錄失敗後,出現驗證碼
用$_SESSION['err_num'] = $err_num;
記錄並傳出錯誤次數以進行判斷
12:內置if標籤condition接收session
<if condition="$Think session.err_num neq null">
</if>
PHP過濾器
驗證郵箱是否符合郵箱格式
filter_var($email[$i], FILTER_VALIDATE_EMAIL)
filter_var() - 通過一個指定的過濾器來過濾單一的變量
filter_var_array() - 通過相同的或不同的過濾器來過濾多個變量
filter_input - 獲取一個輸入變量,並對它進行過濾
filter_input_array - 獲取多個輸入變量,並通過相同的或不同的過濾器對它們進行過濾
轉載 更多過濾函數
thinkphp設置默認訪問模塊
// 綁定訪問Admin模塊
define('BIND_MODULE','Mobile');
清除 X-Powered-By:ThinkPHP
修改文件 xxxxxx\ThinkPHP\Library\Think\View.class.php
修改
header('X-Powered-By:ThinkPHP');
爲
header('X-Powered-By:https://mp.csdn.net/mdeditor/79099716');
時間轉爲時間戳
$mytime=date("Y-m-d 0:0:0", strtotime("-1 day")); //獲取前一天的時間
echo $mytime.'<br/>';
echo strtotime($mytime);//轉爲時間戳
thinkphp I()方法過濾
I('post.email','',FILTER_VALIDATE_EMAIL);//表示 會對$_POST['email'] 進行 格式驗證,如果不符合要求的話,返回空字符串。
sql 排序一個字段值的後4位
SQL中還有一個SUBSTRING
SUBSTRING( ) 從字符串中提取一個或多個字符
//示例一顯示電話號碼的前三個字符(區號)
SELECT SUBSTRING(phone,1,3) FROM employee
//示例二:SQL中一個字段的值由漢字+4位數字組成的,查詢結果不想包含後面的4位數字
select left(a, (len(a)-4) ) from 表名
//示例三:假如傳進SQL需要查詢的是 ‘ABC1234’
select * from Table where 列名 = Substring(‘ABC1234’,1,len(Substring)-4)
//示例1,2顯示所需字符 示例3查詢所需字符
SQL不規則排序,ORDER BY 不規則排序,case的使用
ORDER BY
case DisplaySize
when 'S' then 1
when 'M' then 2
when 'L' then 3
when 'XL' then 4
when 'XXL' then 5
when '3XL' then 6
end
model模型內表單驗證
前端HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<form action="__URL__/add" method="post">
<input type="text" name="username"/>
<input type="text" name="password"/>
<input type="text" name="repassword"/>
<input type="submit" value="提交"/>
<!--{__NOTOKEN__}-->
</form>
</body>
</html>
調用的model模型進行驗證
<?php
class UserModel extends Model{
//參數:
//1.驗證字段(表單中的名稱或者輔助字段例如驗證碼)
//2.驗證規則(結合附加規則一起使用)
//3.錯誤提醒
//4.驗證條件(0:1:2:)最好是選擇手冊中的常量來填寫
//5.附加規則;驗證方式:regex;function名;callback;confirm(驗證兩個字段是否相同);equal;in;unique;
// 常用規則:require:必須存在;email郵箱;url;currency貨幣;number;
//6.驗證時間
// 是指數據庫進行數據庫操作時的驗證時機(增加數據時,編輯時,全部清空下驗證)
// Model::MODEL_INSERT
// Model::MODEL_UPDATE
// Model::MODEL_BOTH
protected $patchValidate = true; // 系統支持數據的批量驗證功能,
//自動驗證開始
protected $_validate=array(
array('username','require','用戶名必填',0,0,1),
array('username','checklen','用戶名長度不合法',0,'callback',3),
array('username','unique','用戶名已存在',0,'callback',3),
array('password','require','用戶名必填',0,0,1),
array('repassword','require','用戶名必填',0,0,1),
array('password','repassword','密碼不一致',0,'confirm',1),
// array('createip','email','郵箱格式不對',0,'regex',1),
);
public function checklen($data){
if (strlen($data)>15||strlen($data)<5)
return FALSE;
return true;
}
function modelTest(){
echo '測試的跨模型操作,調用模型中的方法';
}
}
?>
控制器調用模型完成主體
<?php
/**
* ThinkPHP中的
* 自動驗證(Action中create方法時生效)
* 主要寫在自定義模型中,完成輸入信息的時候對錶單或者數據庫中的字段進行驗證
* 自動完成
* 用戶輸入的字段並不是用戶手動填寫的
* 字段映射
* 防止用戶從前臺看到的字段名稱而猜出數據庫表中的字段名稱
*
*/
class AutoAction extends Action{
public function index(){
$this->display();
}
function add(){
//經過自定義模型
$user=D('user');
if ($user->create()) {
if ($user->add()){
$this->success("註冊成功");
}else{
$this->error($user->getError());
}
}else{
$this->error($user->getError());
}
}
}
?>