Home > CMS Tutorial > PHPCMS > body text

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

coldplay.xixi
Release: 2020-06-10 09:47:48
Original
1872 people have browsed it

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['limit'] && !in_array($id, $key_array)) $key_array[$id] = $v;
$i++;
}
if($data['limit']<$number) break;
}
}
if($data['id']) unset($key_array[$data['id']]);
return $key_array;
}
Copy after login

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}
Copy after login

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:

set_model($category[$catid]['modelid']);
$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 = $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 = $db->select($sql2, '*', $limit, $order,'','id');
$number += count($r);
foreach ($r as $id=>$v) {
if($i<= $data['limit'] && !in_array($id, $key_array)) $key_array[$id] = $v;
$i++;
}
if($data['limit']<$number) break;
}
}
if($data['id']) unset($key_array[$data['id']]);
return $key_array;
}
?>
Copy after login

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

The code is as follows:

{php $data = mk1_content_tag_relation(array('relation'=>$relation,'id'=>$id,'catid'=>$catid,'keywords'=>$rs['keywords'],'order'=>'id DESC','limit'=>'4')); }
{loop $data $r}
{/loop}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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 [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!