ThinkPHP使用getlist方法實(shí)現(xiàn)數(shù)據(jù)搜索功能示例
本文實(shí)例講述了ThinkPHP使用getlist方法實(shí)現(xiàn)數(shù)據(jù)搜索功能。分享給大家供大家參考,具體如下:
自己在ThinkPHP之中的model之中書(shū)寫(xiě)getlist方法,其實(shí)所謂的搜索功能無(wú)非就是數(shù)據(jù)庫(kù)查詢之中用到的like %string%,或者其他的 字段名=特定值,這些sql語(yǔ)句拼接在and語(yǔ)句之中;
HTML之中:
<form action="" method="get">
<table class="account_table" width="100%" cellpadding="0" cellspacing="0">
<tr>
<td style="text-align:right">訂單號(hào):</td>
<td>
<input id="Orderid" name="order_sn" class="inp_wid3" type="text" value="{$_GET['order_sn']}"/>
</td>
<td style="text-align:right">
下單日期:
</td>
<td colspan="5">
<input type="text" class="inp_wid2" id="BeginTime" name="begintime" value="{$_GET['begintime']}" />
至
<input type="text" class="inp_wid2" id="EndTime" name="endtime" value="{$_GET['endtime']}" />
交易完成日期
<input type="text" class="inp_wid2" id="txtFinishedBeginTime" name="finishbegintime" value="{$_GET['finishbegintime']}" />
至
<input type="text" class="inp_wid2" id="txtFinishedEndTime" name="finishendtime" value="{$_GET['finishendtime']}" />
訂單金額:
<input type="text" class="inp_wid2" id="txtMoneyMin" name="count_price_min" value="{$_GET['count_price_min']}"/>
至
<input type="text" class="inp_wid2" id="txtMoneyMax" name="count_price_max" value="{$_GET['count_price_max']}" />
</td>
</tr>
<tr>
<td style="text-align:right; width:80px">采購(gòu)商名稱(chēng):</td>
<td style="width:140px">
<input id="SupermarketName" name="user_nick_name" class="inp_wid3" type="text" value="{$_GET['user_nick_name']}" />
</td>
<td style="text-align:right; width:80px">采購(gòu)商賬號(hào):</td>
<td style="width:140px">
<input id="SupermarketZh" name="user_name" class="inp_wid3" type="text" value="{$_GET['user_name']}" />
</td>
</tr>
<tr>
<td colspan="2">
<input class="search_btn1" type="submit" value="搜索" id="Search" />
</td>
</tr>
</table>
</form>
看到?jīng)]GET方法提交表單,這個(gè)是查詢條件填入選項(xiàng);
控制器之中:
$order_msg=$order->getList();
$this->assign('info',$order_msg);//這個(gè)獲取訂單的詳細(xì)信息
Model之中:
public function getList($pagesize=25){
$tableName = $this->getTableName();
$where = $tableName.'.service_id = '.$_SESSION['service_site']['service_id'];
if(!empty($_GET['order_sn'])){//查詢訂單號(hào)
$where.= " and $tableName.`order_sn` like '%".$_GET['order_sn']."%'";
}
if(!empty($_GET['count_price_min'])){//查詢訂單最小金額
$where.= " and $tableName.count_price >=".$_GET['count_price_min']."";
}
if(!empty($_GET['begintime'])){//下單開(kāi)始日期搜索
$_GET['begintime']=strtotime($_GET['begintime']);//將日期轉(zhuǎn)為時(shí)間戳
$where.= " and $tableName.add_time >=".$_GET['begintime']."";
$_GET['begintime']=date('Y-m-d',$_GET['begintime']);//將日期轉(zhuǎn)為時(shí)間戳
}
if(!empty($_GET['endtime'])){//下單結(jié)束日期搜索
$_GET['endtime']=strtotime($_GET['endtime']);//將日期轉(zhuǎn)為時(shí)間戳
$where.= " and $tableName.add_time <=".$_GET['endtime']."";
$_GET['endtime']=date('Y-m-d',$_GET['endtime']);//將時(shí)間戳轉(zhuǎn)換成日期,方便刷新頁(yè)面后前臺(tái)顯示
}
if(!empty($_GET['finishbegintime'])){//交易完成開(kāi)始日期搜索
$_GET['finishbegintime']=strtotime($_GET['finishbegintime']);//將日期轉(zhuǎn)為時(shí)間戳
$where.= " and $tableName.ok_time >=".$_GET['finishbegintime']."";
$_GET['finishbegintime']=date('Y-m-d',$_GET['finishbegintime']);//將日期轉(zhuǎn)為時(shí)間戳
}
if(!empty($_GET['finishendtime'])){//交易完成結(jié)束日期搜索
$_GET['finishendtime']=strtotime($_GET['finishendtime']);//將日期轉(zhuǎn)為時(shí)間戳
$where.= " and $tableName.ok_time <=".$_GET['finishendtime']."";
$_GET['finishendtime']=date('Y-m-d',$_GET['finishendtime']);//將時(shí)間戳轉(zhuǎn)換成日期,方便刷新頁(yè)面后前臺(tái)顯示
}
if(!empty($_GET['send'])){//查詢已發(fā)貨預(yù)警訂單,發(fā)貨時(shí)間距離此刻超過(guò)五天
$where.= " and $tableName.send_time < '".(time()-60*60*24*5)."'";
}
if(!empty($_GET['doingorder'])){//查詢處理中的訂單
$where.= " and $tableName.status in (0,1)";
}
if(!empty($_GET['warningorder'])){//查詢預(yù)警的訂單:已經(jīng)付款且時(shí)間超過(guò)24小時(shí)未發(fā)貨
$where.= " and $tableName.pay_time < '".(time()-60*60*24)."'";
}
if(!empty($_GET['warningorder'])){//查詢預(yù)警的訂單:已經(jīng)付款且時(shí)間超過(guò)24小時(shí)未發(fā)貨
$where.= " and $tableName.is_pay = 1 ";
}
if(!empty($_GET['warningorder'])){//查詢預(yù)警的訂單:已經(jīng)付款且時(shí)間超過(guò)24小時(shí)未發(fā)貨
$where.= " and $tableName.status in (0,1)";
}
if(!empty($_GET['count_price_max'])){//查詢訂單最大金額
$where.= " and $tableName.count_price <=".$_GET['count_price_max']."";
}
if(!empty($_GET['user_nick_name'])){//查詢采購(gòu)商名稱(chēng)
$where.= " and fab_user.nick_name like '".$_GET['user_nick_name']."%'";
}
if(!empty($_GET['user_name'])){//查詢采購(gòu)商賬號(hào)
$where.= " and fab_user.user_name like '".$_GET['user_name']."%'";
}
if(!empty($_GET['supplier_nick_name'])){//查詢供應(yīng)商商名稱(chēng)
$where.= " and fab_supplier.nick_name like '".$_GET['supplier_nick_name']."%'";
}
if(!empty($_GET['supplier_name'])){//查詢供應(yīng)商賬號(hào)
$where.= " and fab_supplier.supplier_name like '".$_GET['supplier_name']."%'";
}
if($_GET['history'] == 1){
$where .= " and {$tableName}.status in (2,3,4) ";
}
if(($_GET['pay_type'])!=""&&($_GET['pay_type'])!=-1){//查詢支付方式
$where.= " and fab_order_info.pay_type = ".$_GET['pay_type']."";
}
if(($_GET['status'])!=""&&($_GET['status'])!=-1){//查詢訂單狀態(tài)
$where.= " and fab_order_info.status = ".$_GET['status']."";
}
if(!empty($_GET['stime']) && !empty($_GET['etime'])){
$stime = strtotime($_GET['stime']);
$etime = strtotime($_GET['etime']) + 24*60*60;
$where.= " and ($tableName.`inputtime` between '$stime' and '$etime')";
}
$count = $this->where($where)->count();
$this->countNum = $count;
$Page = new \Think\Page($count,$pagesize);
$this->page = $Page->show();
$limit = $Page->firstRow.','.$Page->listRows;
$sql="select $tableName.*,fab_supplier.nick_name as supplier_nick_name,fab_user.nick_name as user_nick_name
from ($tableName left join fab_supplier on fab_order_info.supplier_id=fab_supplier.supplier_id)
left join fab_user on fab_order_info.user_id=fab_user.user_id where $where order by $tableName.`order_id` desc limit $limit";
$sqls="select sum(fab_order_info.count_price) as order_price,count(fab_order_info.count_price) as order_count
from $tableName where $where order by $tableName.`order_id` desc limit $limit";
$this->sql_msg=$this->query($sqls);
return $this->query($sql);//訂單詳細(xì)信息
}
你只需要留意那個(gè)GET數(shù)據(jù)獲取,然后進(jìn)行拼接SQL語(yǔ)句;你為何總是拼接錯(cuò)誤呢!??!
<?php
namespace Admin\Model;
use Think\Model;
class KuaidicompanyModel extends Model {
private $page = "";
public function getList($pagesize=25){
$where = '1';
$tableName = $this->getTableName();
$count = $this->where($where)->count();
$Page = new \Think\Page($count,$pagesize);
$this->page = $Page->show();
$limit = $Page->firstRow.','.$Page->listRows;
return $this->query("select * from $tableName where $where order by $tableName.`id` asc limit $limit ");
}
public function getPage(){
return $this->page;
}
}
精簡(jiǎn)通用版getlist,實(shí)用于分頁(yè)。
<?php
namespace Admin\Model;
use Think\Model;
class KuaidicompanyModel extends Model {
private $page = "";
public function getList($pagesize=25){
$where = '1';
$tableName = $this->getTableName();
$count = $this->where($where)->count();
$Page = new \Think\Page($count,$pagesize);
$this->page = $Page->show();
$limit = $Page->firstRow.','.$Page->listRows;
return $this->query("select * from $tableName where $where order by $tableName.`id` asc limit $limit ");
}
public function getPage(){
return $this->page;
}
}
精簡(jiǎn)版MODEL用于數(shù)據(jù)自動(dòng)驗(yàn)證
更多關(guān)于thinkPHP相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《ThinkPHP入門(mén)教程》、《thinkPHP模板操作技巧總結(jié)》、《ThinkPHP常用方法總結(jié)》、《codeigniter入門(mén)教程》、《CI(CodeIgniter)框架進(jìn)階教程》、《Zend FrameWork框架入門(mén)教程》及《PHP模板技術(shù)總結(jié)》。
希望本文所述對(duì)大家基于ThinkPHP框架的PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
thinkPHP商城公告功能開(kāi)發(fā)問(wèn)題分析
這篇文章主要介紹了thinkPHP商城公告功能開(kāi)發(fā)問(wèn)題,結(jié)合實(shí)例形式分析了基于thinkPHP實(shí)現(xiàn)商城公告功能所涉及的ajax交互及數(shù)據(jù)庫(kù)操作相關(guān)技巧,需要的朋友可以參考下2016-12-12
codeigniter中view通過(guò)循環(huán)顯示數(shù)組數(shù)據(jù)的方法
這篇文章主要介紹了codeigniter中view通過(guò)循環(huán)顯示數(shù)組數(shù)據(jù)的方法,實(shí)例分析了codeigniter中view方法與數(shù)組遍歷的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03
基于php偽靜態(tài)的實(shí)現(xiàn)詳細(xì)介紹
本篇文章介紹了,基于php偽靜態(tài)的實(shí)現(xiàn)詳細(xì)分析。需要的朋友參考下2013-04-04
三個(gè)思路解決laravel上傳文件報(bào)錯(cuò):413 Request Entity Too Large問(wèn)題
上傳圖片的時(shí)候,是用laravel自帶的上傳圖片的方法,一下氣上傳了20張,結(jié)果就無(wú)情報(bào)錯(cuò):413 Request Entity Too Large,后面查一下,這個(gè)報(bào)錯(cuò)信息是nginx報(bào)的錯(cuò)誤,不是php報(bào)的錯(cuò)誤。也就是說(shuō)在上傳圖片的時(shí)候被nginx攔截了2017-11-11
YII CLinkPager分頁(yè)類(lèi)擴(kuò)展增加顯示共多少頁(yè)
yii的分頁(yè)類(lèi)CLinkPager默認(rèn)是不支持顯示共x頁(yè)的,那么現(xiàn)在接的項(xiàng)目有這樣的需求,怎么辦呢?下面通過(guò)本文給大家介紹YII CLinkPager分頁(yè)類(lèi)擴(kuò)展增加顯示共多少頁(yè)的實(shí)例代碼,需要的朋友參考下吧2016-01-01
PHP將英文數(shù)字轉(zhuǎn)換為阿拉伯?dāng)?shù)字實(shí)例講解
在本篇文章里小編給大家分享了關(guān)于PHP將英文數(shù)字轉(zhuǎn)換為阿拉伯?dāng)?shù)字實(shí)例內(nèi)容,有興趣的朋友們可以參考學(xué)習(xí)下。2019-01-01
Laravel模糊查詢區(qū)分大小寫(xiě)的實(shí)例
今天小編就為大家分享一篇Laravel模糊查詢區(qū)分大小寫(xiě)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-09-09

