phpcms v9后台有三个栏目,对应各自己的模型,
名句 ,模型表格为v9_mingju,诗人 ,v9_shiren,古诗全文,v9_shiren ,三个栏目,他们需要先各自建立的模型,其字段建议放在主表中,方便查询调用.
想实现的功能是,在名句文章列表页list.thml中,在显示名句时,后面同步显示,这句诗的作者,与文章来源出处.
当然,作者与出处,在发表这个名句时,就有填写,但是现在想给作者与出处加上链接,因为他们对应另外两个栏目.即诗人栏目,与古诗全文栏目
一,在自定义函数文件中,
phpcms\libs\functions\extention.func.php
增加两个函数,如果因名句中没有查到对应的链接.则用搜索功能替代
//根据名句的作者名称,调用shiren表或gushi表,并把分类与id,
//通过go()默认函数转化得到作者及古诗的url链接. 并返回
//查诗人表
function getShiren($title){
$db = pc_base::load_model('content_model');
$db->table_name ='v9_shiren';
$res = $db->get_one(array('title'=>$title));
$url=go($res['catid'], $res['id']);
if($url==''){
$url='index.php?m=search&c=index&a=init&typeid=55&siteid=1&q='.urlencode($title);
}
echo $url;
}
//查古诗
function getGushi($title){
$db = pc_base::load_model('content_model');
$db->table_name ='v9_gushi';
$res = $db->get_one(array('title'=>$title));
$url=go($res['catid'], $res['id']);
//新增,注意链接中的typeid=55,这个55表示每个文章模型对应的不同搜索编号,这个数字的查看方式为.后台.模块.全文搜索.id,可以查看每个模型自己的id,并替换55
if($url==''){
$url='index.php?m=search&c=index&a=init&typeid=55&siteid=1&q='.urlencode($title);
}
echo $url;
}
//查古诗,联合标题与作者查询,防止有重名导致这句诗的出处出现问题,比如 梅花 这个诗,历史上可能有多个.这里就会出现不对应问题
//查古诗,联合标题与作者查询,防止因为出来有重名的问题,比如 梅花 这个诗,历史上可能有多个.这里就会出现问题
function getGushilianhe($title,$zuozhe){
$db = pc_base::load_model('content_model');
$db->table_name ='v9_gushi';
$res = $db->get_one(array('title'=>$title , 'zuozhe'=>$zuozhe ));//前一个'zuozhe'表示古诗表中的作者字段
$url=go($res['catid'], $res['id']);
if($url==''){
$url='index.php?m=search&c=index&a=init&typeid=55&siteid=1&q='.urlencode($title);
}
echo $url;
}
二,模板参考
列表页,list.html模板,联合查询案例
<a href="{getGushilianhe($r[chuzi],$r[zuozhe] )}">{$r[chuzi]}</a>
列表模板,
{pc:content action="lists" catid="$catid" num="2" order="id DESC" page="$page"}
<ul class="list lh24 f14">
{loop $data $r}
<li><a href="{$r[url]}" target="_blank"{title_style($r[style])}>{$r[title]}</a>
- - <a href="{getShiren($r[zuozhe])}">{$r[zuozhe]}</a> - -
<a href="{getGushi($r[chuzi])}">{$r[chuzi]}</a>
</li>
{if $n%5==0}<li class="bk20 hr"></li>{/if}
{/loop}
</ul>
<div id="pages" class="text-c">{$pages}</div>
{/pc}
三,内容页模板
<br/><br/>相关文章
<ul class="list lh24 f14">
{pc:get sql="select * from v9_mingju where status=99 and zuozhe='$zuozhe' " return="data22" num="20"}
{loop $data22 $r}
<li><a href="{$r[url]}" target="_blank"{title_style($r[style])}>{$r[title]}</a>
- - <a href="{getShiren($r[zuozhe])}">{$r[zuozhe]}</a> - -
<a href="{getGushi($r[chuzi])}">{$r[chuzi]}</a>
</li>
{if $n%5==0}<li class="bk20 hr"></li>{/if}
{/loop}
{/pc}
</ul>