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>