Home  >  Article  >  CMS Tutorial  >  The most practical method to implement cross-table query related links in Empire CMS

The most practical method to implement cross-table query related links in Empire CMS

silencement
silencementforward
2019-11-29 14:04:312369browse

The most practical method to implement cross-table query related links in Empire CMS

The example in this article describes the implementation method of cross-table query related links in Imperial CMS. Share it with everyone for your reference. The specific analysis is as follows:

Official related links are not flexible, the style cannot be modified arbitrarily and cannot cross tables. Many people want to make related links across tables. For example, news and info have several articles with keywords. 'Beijing', then how to call these articles with the same keyword? I wrote a custom function for this purpose.

It is recommended to study "Empire cms tutorial"

A small range of calls can be used in this category, and a slightly larger range can call the same table If the article is larger, you can call the article of the specified table. You can choose the number of articles to be called. In fact, you can also upgrade, such as specifying the number of articles to call a certain table, the order of that table, etc. The function has no style. You can understand it a little bit. The code can be modified by yourself, it is not particularly difficult. If anyone has upgraded this function, you can share it with everyone.

The code is as follows:

<?php
function user_OtherLink($tbname,$num,$ck){
global $navinfor,$empire,$dbtbpre,$class_r ;
$ck=(int)$ck ;
if($ck==1||$ck==2){
$tbname=$class_r[$navinfor[&#39;classid&#39;]][&#39;tbname&#39;] ;
}
$num= $num==&#39;&#39; ? 5 : $num ; //缺省获取数量为5
$tag_all=explode(&#39;,&#39;,$navinfor[&#39;infotags&#39;]);
$tbname_num=explode(&#39;,&#39;,$tbname) ;
$eq_num=ceil($num/count($tag_all));
for($i=0;$i<count($tag_all);$i++){
$r_1_1 =$empire->fetch1("select tagid,tagname,num,isgood,cid from {$dbtbpre}enewstags where tagname=&#39;".
$tag_all[$i]."&#39; order by tagid limit 1") ;
$tag_id[$i]=$r_1_1[&#39;tagid&#39;] ; //tag的ID
$tag_num[$i]=$r_1_1[&#39;num&#39;] ; //tag下的文章数量
if($tag_num[$i]>=$eq_num){//如果TAG下的文章数量比平均的数量大或者等于,那么TAG的文章数量取平均值
$tag_num[$i] = $eq_num ;//重新赋值
}else{//如果TAG下的文章小于平均值,那么TAG取原有的文章数量。同时重新赋值平均值。
$eg_tag= $eq_num-$tag_num[$i] ;
$eq_num += $eg_tag ;
}
//比如说有3个TAG,每个TAG下有4篇文章,现在指定要显示10篇文章,那么每个TAG要拿出4篇,共有12篇文章
//3个TAG,每个TAG下有3篇文章,现在指定要显示10篇文章,那么每个TAG要拿出3篇文章,共有9篇文章
//3个TAG,A有2篇文章,B有5篇文章,C有3篇文章,现在要显示10篇文章,那么A拿出2篇文章,B拿出5篇文章,C拿出3篇文章,
共有10篇文章
//如果 A、B、C中除了当前文章,还有相同的文章,那么会扣除重复的那篇文章,只取一次。所以指定的数量会有偏差
for($i_n=0;$i_n<count($tbname_num);$i_n++){
$sql=$empire->query("select tid,classid,id,mid from {$dbtbpre}enewstagsdata where tagid=&#39;".$tag_id[$i]."&#39; order 
by classid ");
$ri=1 ;
while($r=$empire->fetch($sql))
{
$tbname_all[$r[&#39;tid&#39;]]=$class_r[$r[&#39;classid&#39;]][&#39;tbname&#39;] ;
if($tbname_all[$r[&#39;tid&#39;]]==$tbname_num[$i_n] && $ri <= $tag_num[$i] && $navinfor[&#39;classid&#39;]!=$r[&#39;classid&#39;] && 
$navinfor[&#39;id&#39;]!=$r[&#39;id&#39;]){
if($ck==1&&$navinfor[&#39;classid&#39;]==$r[&#39;classid&#39;]){
$tbname_all_r[$r[&#39;tid&#39;]]=$class_r[$r[&#39;classid&#39;]][&#39;tbname&#39;] ;
$classid_id[$r[&#39;tid&#39;]]=$r[&#39;classid&#39;] . &#39;_&#39; . $r[&#39;id&#39;] ;
$classid_all[$r[&#39;tid&#39;]]=$r[&#39;classid&#39;] ;
$id_all[$r[&#39;tid&#39;]][$r[&#39;classid&#39;]]=$r[&#39;id&#39;] ;
}elseif($ck==2&&$class_r[$navinfor[&#39;classid&#39;]][&#39;tbname&#39;]==$class_r[$r[&#39;classid&#39;]][&#39;tbname&#39;]){
$tbname_all_r[$r[&#39;tid&#39;]]=$class_r[$r[&#39;classid&#39;]][&#39;tbname&#39;] ;
$classid_id[$r[&#39;tid&#39;]]=$r[&#39;classid&#39;] . &#39;_&#39; . $r[&#39;id&#39;] ;
$classid_all[$r[&#39;tid&#39;]]=$r[&#39;classid&#39;] ;
$id_all[$r[&#39;tid&#39;]][$r[&#39;classid&#39;]]=$r[&#39;id&#39;] ;
}elseif($ck==3){
$tbname_all_r[$r[&#39;tid&#39;]]=$class_r[$r[&#39;classid&#39;]][&#39;tbname&#39;] ;
$classid_id[$r[&#39;tid&#39;]]=$r[&#39;classid&#39;] . &#39;_&#39; . $r[&#39;id&#39;] ;
$classid_all[$r[&#39;tid&#39;]]=$r[&#39;classid&#39;] ;
$id_all[$r[&#39;tid&#39;]][$r[&#39;classid&#39;]]=$r[&#39;id&#39;] ;
}
$ri++;
}
}
}
}
$classid_id_x=array_unique($classid_id);//去除重复的值
//去除重复的值,不同的TAG会有相同的文章。所以去除重复的值。比如A篇文章里面都有"北京","奥运",两个关键词,
B篇文章里面也有"北京","奥运"在不去除重复的情况下,A篇的相关链接会出现两次B文章。所以必须去除其中一个。
arsort($classid_id_x); //按键名排列,倒序
$tid_tid=array_keys($classid_id_x) ;//取出键名重新排列,键名即为TAG的ID:tid
shuffle($tid_tid);//顺序打乱,重新排
$tid_tid_num=count($tid_tid) ;
if($num<$tid_tid_num) $tid_tid_num=$num+1 ;//由于去除了本文章,所以再加一条弥补
for($i_tid=0;$i_tid<$tid_tid_num;$i_tid++){
$tid=$tid_tid[$i_tid];
$classid=$classid_all[$tid];
$id=$id_all[$tid][$classid];
if($id==$navinfor[&#39;id&#39;]) continue ; //如果是本文章,相关链接里面就不用再放了。
$tbname=$tbname_all_r[$tid];
$r_1_2 =$empire->fetch1("select title,smalltext,titleurl,dp_jt,dp_dwz from {$dbtbpre}ecms_$tbname where 
id=&#39;".$id."&#39; order by newstime limit 1") ;
////////显示样式在这修改
echo $r_1_2[&#39;title&#39;].&#39; &#39; ;
////////
}
}
?>
/**
<?php
OtherLink(&#39;news,info&#39;,7,3) ;
?>
**/

I hope this article will be helpful to everyone The Empire CMS website builder helps.

The above is the detailed content of The most practical method to implement cross-table query related links in Empire CMS. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:www.word666.com. If there is any infringement, please contact admin@php.cn delete