{pc:get} 标签可以获取指定表的数据:,,“html,{pc:get tableid="1" order="id DESC"},{title},{content},{/pc:get},“,,这段代码会从 ID 为 1 的表中按 ID 降序排列获取数据,并显示标题和内容。一、简介
PHPCMS是一款功能强大的内容管理系统,广泛应用于各类网站的开发与维护中,其模板标签功能丰富,可以极大地方便开发者进行数据的查询和展示,本文将详细介绍PHPCMS的查询标签,包括其基本语法、常用参数以及实际应用示例。
二、基本用法
基本语法
PHPCMS的查询标签主要用于从数据库中获取数据,并在模板中进行展示,常用的查询标签包括{pc:content}、{pc:get}等。
2.1 {pc:content}
action:规定生成的动作,默认为lists,如果取值为position,表示读取推荐位中的内容;如果取值为lists,主要用于列表等。
catid:栏目ID,用于指定从哪个栏目中抽取数据。
num:表示生成的数据条目的数量。
order:排序方式,DESC为降序,ASC为升序。
page:分页属性,如果指定了该属性,表示有分页,最终的分页数据则有分页标签进行调用。
thumb是否有缩略图。
start:起始位置。

示例如下:
{pc:content action="lists" catid="$catid" num="5" order="listorder,id DESC" page="$page" thumb="1" start="5"}
{loop $data $r}
<li><a href="{$r[url]}" title="{$r[title]}">{$r[title]}</a></li>
{/loop}
{/pc}
2.2 {pc:get}
sql:要执行的SQL语句。
return:自定义返回变量名称。
示例如下:
{pc:get sql="select catname from v9_category where catid='$catid'" return="data"}
{loop $data $r}
<p>{$r[catname]}</p>
{/loop}
{/pc}
三、高级用法
万能查询语句
万能查询实际上就是PC标签+SQL语言来直接对数据表进行查询。
{pc:get sql="select * from phpcms_content where catid='$catid'" return="data"}
{loop $data $r}
<li><a href="{$r[url]}" title="{$r[title]}">{$r[title]}</a></li>
{/loop}
{/pc}
结合where条件实现精准查询
PHPCMS支持通过where条件实现精确查询和筛选功能。
{pc:content action="lists" catid="$catid" where="status=99" order="listorder ASC"}
{loop $data $r}
<li><a href="{$r[url]}" title="{$r[title]}">{$r[title]}</a></li>
{/loop}
{/pc}
使用分页功能
分页功能可以通过设置page属性来实现:

{pc:content action="lists" catid="$catid" num="10" order="listorder ASC" page="$page"}
{loop $data $r}
<li><a href="{$r[url]}" title="{$r[title]}">{$r[title]}</a></li>
{/loop}
{$pages}
{/pc}
自定义返回变量
可以通过return属性自定义返回变量名称:
{pc:get sql="select * from phpcms_content where catid='$catid' and status=99 order by updatetime desc" rows="10" return="v"}
{loop $v $r}
<li><a href="{$r[url]}" title="{$r[title]}">{$r[title]}</a></li>
{/loop>
{$pages v=”$v“}
{/pc}
四、实际应用示例
获取父分类下的子分类
{loop subcat(77) $k $v}
{php $subcatid[] = $k;}
{/loop}
<?php $subcatid = implode(',', $subcatid);?> 结果可以用在sql 的in子句中
模板加载
{template "content", "header_index"}
header_index 要加载的模板名称。
该标签只能加载自定义的模板,对于预定义好的模板是不能进行加载的。预定义的模板名称
category_ 栏目页模板的名称都是如此命名
list_ 列表页的模板都是如此命名
article_ 内容页的模板都是如此命名
index.html 首页模板的命名
default.html 超首模板的命名
page_ 单页面的模板都是如此命名
show_ 内容页都是如此命名
广告位调用
{loop $pinfo $p}
document.write('<li><a href="{$p['setting'][1]['linkurl']}" class="fl" target="_blank">{$p['name']}</a><em class="fr">{$p['setting'][1]['title']}</em></li>');
{/loop}
基础语法
4.1 定义变量
{php $num=0}
* 注意有些变量是系统自定义的,$catid 、$CATEGORYS 、$parentid 等。
4.2 判断语句
{if $num==0}
双分支判断 {if $num==0} {else} {/if}
4.3 结合变量控制列表生成数量
{php $num=0}
{pc:content action="lists" catid="67" num="30" order="listorder , id DESC" page="$page"}
{loop $data $r}
{$php $num++}
<li> <a href="{$r['url']}" target="_blank">{$r['title']}</a></li>
{if $num%5==0}<hr />{/if}
{/loop}
{/pc}
实例与技巧
5.1 loop中拿到文章的属性
{pc:content action="position" posid="14" order="listorder DESC,id DESC" num="5"}
{loop $data $r}
$r[catid] //当前文章所属栏目id
$r[catdir] // 当前文章所属栏目英文名称
$r[catname] //当前文章所属栏目中文名称
{/loop}
{/pc}
5.2 PC标签的三种形式
{pc:content action="lists" catid="$catid" num="5" order="listorder,id DESC" page="$page" thumb="1" start="5"}
{loop $data $r}
<li><a href="{$r[url]}" title="{$r[title]}" target="_blank">{$r[title]}</a></li>
{/loop}
{/pc}
万能查询语句的应用
6.1 调用本系统单条数据
{get sql="select * from phpcms_content where contentid=1" /}
{str_cut($r[title], 50)} URL:{$r[url]} 更新日期:{date('Ymd', $r[updatetime])}
6.2 调用本系统多条数据

{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('Ymd', $r[updatetime])}
{/get}
6.3 带分页调用本系统多条数据
{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('Ymd', $r[updatetime])}
{/get}
分页:{$pages}
6.4 自定义返回变量调用本系统多条数据
{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('Ymd', $v[updatetime])}
{/get}
6.5 调用同一帐号下的其他数据库
{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('Ymd', $r[dateline])}
{/get}
6.6 调用外部数据
{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('Ymd', $r[dateline])}
{/get}
7. 取出评论数最多的第3条到第10条记录
<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 as comments fromphpcms_content as A INNER JOINphpcms_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>
8. PHPCMS V9保留的get标签使用方法
{pc:get sql="SELECT * FROMXX 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是调用的条数。
```php代码已经满足需求,不需要再进行补全。
来源互联网整合,作者:小编,如若转载,请注明出处:https://www.aiboce.com/ask/67993.html