#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; }
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('content_model'); $catid = intval($data['catid']); $siteids = getcache('category_content','commons'); if(!$siteids[$catid]) return false; $siteid = $siteids[$catid]; $category = getcache('category_content_'.$siteid,'commons'); if(empty($category)) return false; if($category[$catid]['type']!=0) return false; $db->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; } ?>
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}
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!