PHPCMS V9 SQL查詢篇


1.添加查詢條件

{php $sql5 = " pay_type_int = 24"} {pc:content action="lists" catid="11" moreinfo="1" where="$sql5" num="1" order="listorder asc,inputtime asc" page="$page" cache="$cachetime" }

2.同時調用指定欄目和指定推薦位

{pc:get sql="SELECT * FROM v9_house WHERE id IN (SELECT id FROM v9_position_data WHERE posid =2) or catid in(11,12) order by id DESC" num="7" return="data" }

3.同時調用兩個不同表的相同字段

{pc:get sql="(select id,title,catid,inputtime from v9_house where status = 99) union (select id,title,catid,inputtime from v9_qiugou where status = 99) ORDER BY inputtime DESC limit 7 --" return="data"}

4.調用需求:文章范圍為59 60 61三個欄目,並且推送到了27 和28兩個推薦位;

{pc:get sql="SELECT * FROM v9_news WHERE id IN (SELECT id FROM v9_position_data WHERE posid in(27,28) and catid in(59,60,61)) order by listorder DESC" cache="3600" start="3" num="7" return="data" }

 

PHPCMS V9的get標簽調用

1、調用本系統單條數據,示例(調用ID為1的信息,標題長度不超過25個漢字,顯示更新日期):

{get sql="select * from phpcms_content where contentid=1" /}

標題:{str_cut($r[title], 50)} URL:{$r[url]} 更新日期:{date('Y-m-d', $r[updatetime])}

 

2.調用本系統多條數據,示例(調用欄目ID為1通過審核的10條信息,標題長度不超過25個漢字,顯示更新日期):

{get sql="select * from phpcms_content where catid=1 and status=99 order by updatetime desc" rows="10"}

標題:{str_cut($r[title], 50)} URL:{$r[url]} 更新日期:{date('Y-m-d', $r[updatetime])}

{/get}

3.帶分頁,示例(調用欄目ID為1通過審核的10條信息,標題長度不超過25個漢字,顯示更新日期,帶分頁):

{get sql="select * from phpcms_content where catid=1 and status=99 order by updatetime desc" rows="10" page="$page"}

標題:{str_cut($r[title], 50)} URL:{$r[url]} 更新日期:{date('Y-m-d', $r[updatetime])}

{/get}

分頁:{$pages}

4.自定義返回變量,示例(調用欄目ID為1通過審核的10條信息,標題長度不超過25個漢字,顯示更新日期,返回變量為 $v):

{get sql="select * from phpcms_content where catid=1 and status=99 order by updatetime desc" rows="10" return="v"}

標題:{str_cut($v[title], 50)} URL:{$v[url]} 更新日期:{date('Y-m-d', $v[updatetime])}

{/get}

5、調用同一帳號下的其他數據庫,示例(調用數據庫為bbs,分類ID為1的10個最新主題,主題長度不超過25個漢字,顯示更新日期):

{get dbname="bbs" sql="select * from cdb_threads where fid=1 order by dateline desc" rows="10"}

主題:{str_cut($r[subject], 50)} URL:http://bbs.phpcms.cn/viewthread.php?tid={$r[tid]} 更新日期:{date('Y-m-d', $r[dateline])}

{/get}

6、調用外部數據,示例(調用數據源為bbs,分類ID為1的10個最新主題,主題長度不超過25個漢字,顯示更新日期):

{get dbsource="bbs" sql="select * from cdb_threads where fid=1 order by dateline desc" rows="10"}

主題:{str_cut($r[subject], 50)} URL:http://bbs.phpcms.cn/viewthread.php?tid={$r[tid]} 更新日期:{date('Y-m-d', $r[dateline])}

{/get}

7、當前欄目調用父級及以下欄目信息方法

{php $arrchildid = $CATEGORYS[$CAT[parentid]][arrchildid]}

{pc:get sql="SELECT * FROM v9_news where catid in($arrchildid) cache="3600" page="$page" num="12" return="data"}

 

不知道有沒有朋友在使用中遇到這幾種方法不能滿足需求的時候呢? 例如:取出評論數最多的第3條到第10條記錄,有人說我是多此一舉,一般取最多評論沒理由不取第一和第二條,因為PHPCMS的縮略圖對4:3這樣的尺寸比較好,對長條形(如3:4)的圖片縮略效果不好,為了自己手動更新評論最多的第一和第二條記錄,故此不想自動更新讀取前兩條。

<ul>

<!--{get sql="select A.contentid,A.catid,A.title,A.thumb,A.description,A.url,A.status,A.updatetime,B.contentid,B.hits,B.comments from `phpcms_content` as A INNER JOIN `phpcms_content_count` as B ON A.contentid=B.contentid and A.status=99 and LENGTH(A.thumb)>0 order by B.comments desc LIMIT 3,7"}-->

<li><span class="xxnse_fen2">{$r[comments]}人參與評論</span>?<a href="{$r[url]}" title="{$r[title]}">{str_cut($r[title], 28)}</a></li>

<!--{/get}-->

</ul>

 

關鍵點就在 order by B.comments desc LIMIT 3,7 (表示從第3條記錄開始,向下讀取7條數據)這個在MYSQL是經常用,

我抱着試試看的想法,結果是可以的。 phpcms V9 保留了2008的get標簽的使用方法 它包括了2種方式一種是內部數據,

另一種是外部數據 我們先分析下內部數據的使用方法

1、內部數據的調用

{pc:get sql="SELECT * FROM `XX` WHERE fid =$ltid AND digest =2 AND ifupload =1 ORDER BY tid DESC" num="2" cache= "3600" return="data" }

{loop $data $r}

。。。。。

{/loop}

{/pc}

由此可以看出 get 語句支持num的用法但是不支持 limit 5,5.這樣的用法

實在是很遺憾 num是調用的條數

2、外部數據的調用

 

{ pc : get sql = "SELECT * FROM phpcms_member" cache = "3600" page = "$page" dbsource = "discuz" return = "data" } <ul>

{ loop $data $key $val } { $val [ username ]}< br />

{ /loop} </ ul >

{ $pages }

{/ pc }

一個是數據源,一個是產生的pages翻頁效果

 

 

 來源:http://blog.csdn.net/vailook/article/details/53083532


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM