Home  >  Article  >  CMS Tutorial  >  What should I do if the order of phpcms related articles remains unchanged?

What should I do if the order of phpcms related articles remains unchanged?

coldplay.xixi
coldplay.xixiOriginal
2020-06-10 09:41:312078browse

What should I do if the order of phpcms related articles remains unchanged?

#What should I do if the sorting of phpcms remains unchanged?

How to modify the problem of unchanged sorting of phpcms related articles:

Open phpcms/modules/content/classes/content_tag.class.php content model tag class , it is found that this tag will be sorted according to the order parameter only when the content has artificially set related readings. When the content does not have artificially set related readings, the query is performed based on keywords, but at this time, the order parameter is not sorted. Instead, it is not sorted. This is why the related readings called by articles are always so old.

The method to correct this problem is as follows:
Modify the phpcms/modules/content/classes/content_tag.class.php content model tag class file, and modify the relation method in the content_tag class to :

The code is as follows:

/**
* 相关文章标签
* @param $data
*/
public function relation($data) {
$catid = intval($data['catid']);
if(!$this->set_modelid($catid)) return false;
$order = $data['order'];
$sql = "`status`=99";
$limit = $data['id'] ? $data['limit']+1 : $data['limit'];
if($data['relation']) {
$relations = explode('|',trim($data['relation'],'|'));
$relations = array_diff($relations, array(null));
$relations = implode(',',$relations);
$sql = " `id` IN ($relations)";
$key_array = $this->db->select($sql, '*', $limit, $order,'','id');
} elseif($data['keywords']) {
$keywords = str_replace('%', '',$data['keywords']);
$keywords_arr = explode(' ',$keywords);
$key_array = array();
$number = 0;
$i =1;
foreach ($keywords_arr as $_k) {
$sql2 = $sql." AND `keywords` LIKE '%$_k%'".(isset($data['id']) && intval($data['id']) ? " AND `id` != '".abs(intval($data['id']))."'" : '');
$r = $this->db->select($sql2, '*', $limit, $order,'','id');
$number += count($r);
foreach ($r as $id=>$v) {
if($i<= $data[&#39;limit&#39;] && !in_array($id, $key_array)) $key_array[$id] = $v;
$i++;
}
if($data[&#39;limit&#39;]<$number) break;
}
}
if($data[&#39;id&#39;]) unset($key_array[$data[&#39;id&#39;]]);
return $key_array;
}

In fact, it is just $r = $this->db->select($sql2, '*', $limit, '','', 'id'); replaced by $r = $this->db->select($sql2, '*', $limit, $order,'','id'); and let the order parameter be passed into the query method.
In the template, use the following tags and add the order parameter to achieve sorting.

The code is as follows:

{pc:content action="relation" relation="$relation" id="$id" catid="$catid" num="5" keywords="$rs[keywords]" order="id DESC"}
{loop $data $r}
{/loop}
{/pc}

If you are a mysophobic friend and are worried that directly modifying the PC will affect future upgrades, you can extract it separately. Put it in the template and use it as a function. The code is as follows:

The code is as follows:

<?php
/**
* 内容模型 - 相关文章标签(修正排序异常问题)
* @param $data
*/
function mk1_content_tag_relation($data) {
$db = pc_base::load_model(&#39;content_model&#39;);
$catid = intval($data[&#39;catid&#39;]);
$siteids = getcache(&#39;category_content&#39;,&#39;commons&#39;);
if(!$siteids[$catid]) return false;
$siteid = $siteids[$catid];
$category = getcache(&#39;category_content_&#39;.$siteid,&#39;commons&#39;);
if(empty($category)) return false;
if($category[$catid][&#39;type&#39;]!=0) return false;
$db->set_model($category[$catid][&#39;modelid&#39;]);
$order = $data[&#39;order&#39;];
$sql = "`status`=99";
$limit = $data[&#39;id&#39;] ? $data[&#39;limit&#39;]+1 : $data[&#39;limit&#39;];
if($data[&#39;relation&#39;]) {
$relations = explode(&#39;|&#39;,trim($data[&#39;relation&#39;],&#39;|&#39;));
$relations = array_diff($relations, array(null));
$relations = implode(&#39;,&#39;,$relations);
$sql = " `id` IN ($relations)";
$key_array = $db->select($sql, &#39;*&#39;, $limit, $order,&#39;&#39;,&#39;id&#39;);
} elseif($data[&#39;keywords&#39;]) {
$keywords = str_replace(&#39;%&#39;, &#39;&#39;,$data[&#39;keywords&#39;]);
$keywords_arr = explode(&#39; &#39;,$keywords);
$key_array = array();
$number = 0;
$i =1;
foreach ($keywords_arr as $_k) {
$sql2 = $sql." AND `keywords` LIKE &#39;%$_k%&#39;".(isset($data[&#39;id&#39;]) && intval($data[&#39;id&#39;]) ? " AND `id` != &#39;".abs(intval($data[&#39;id&#39;]))."&#39;" : &#39;&#39;);
$r = $db->select($sql2, &#39;*&#39;, $limit, $order,&#39;&#39;,&#39;id&#39;);
$number += count($r);
foreach ($r as $id=>$v) {
if($i<= $data[&#39;limit&#39;] && !in_array($id, $key_array)) $key_array[$id] = $v;
$i++;
}
if($data[&#39;limit&#39;]<$number) break;
}
}
if($data[&#39;id&#39;]) unset($key_array[$data[&#39;id&#39;]]);
return $key_array;
}
?>

In the template, use the following PHP code to obtain it.

The code is as follows:

{php $data = mk1_content_tag_relation(array(&#39;relation&#39;=>$relation,&#39;id&#39;=>$id,&#39;catid&#39;=>$catid,&#39;keywords&#39;=>$rs[&#39;keywords&#39;],&#39;order&#39;=>&#39;id DESC&#39;,&#39;limit&#39;=>&#39;4&#39;)); }
{loop $data $r}
{/loop}

In fact, it is just a small problem. The PC should be corrected in the future. The above method is provided to those anxious webmaster friends.

Recommended tutorial: "PHP Video Tutorial"

The above is the detailed content of What should I do if the order of phpcms related articles remains unchanged?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn