Home  >  Article  >  CMS Tutorial  >  How to filter and classify phpcms articles?

How to filter and classify phpcms articles?

coldplay.xixi
coldplay.xixiOriginal
2020-06-10 10:55:482928browse

How to filter and classify phpcms articles?

phpcms文章如何筛选分类

phpcms文章筛选分类的方法:

首先,用下面这些代码替换掉phpcms/libs/functions/extention.func.php的内容

<?php  
/** 
 *  extention.func.php 用户自定义函数库 
 * 
 * @copyright           (C) 2005-2010 PHPCMS 
 * @license             http://www.phpcms.cn/license/ 
 * @lastmodify          2010-10-27 
 */  
   
  
 /** 
 * 通过指定keyid形式显示所有联动菜单 
 * @param  $keyid 菜单主id 
 * @param  $linkageid  联动菜单id,0调用顶级 
 * @param  $modelid 模型id 
 * @param  $fieldname  字段名称 
 */  
function show_linkage($keyid, $linkageid = 0, $modelid = &#39;&#39;, $fieldname=&#39;zone&#39;) {  
        $datas = $infos = $array = array();  
        $keyid = intval($keyid);  
        $linkageid = intval($linkageid);  
        //当前菜单id  
        $field_value = intval($_GET[$fieldname]);  
        $urlrule = structure_filters_url($fieldname,$array,1,$modelid);  
        if($keyid == 0) return false;  
        $datas = getcache($keyid,&#39;linkage&#39;);  
        $infos = $datas[&#39;data&#39;];  
  
        foreach($infos as $k=>$v){  
                if($v[&#39;parentid&#39;]==$field_value){  
                        $array[$k][&#39;name&#39;] = $v[&#39;name&#39;];  
                        $array[$k][&#39;value&#39;] = $k;  
                        $array[$k][&#39;url&#39;] = str_replace(&#39;{&#39;.$fieldname.&#39;}&#39;,$k,$urlrule);  
                        $array[$k][&#39;menu&#39;] = $field_value == $k ? &#39;<em>&#39;.$v[&#39;name&#39;].&#39;</em>&#39; : &#39;<a href=&#39;.$array[$k][&#39;url&#39;].&#39;>&#39;.$v[&#39;name&#39;].&#39;</a>&#39; ;  
                        }  
                        }  
                        return $array;  
                        }  
                          
/** 
 * 构造筛选URL 
 */                       
function structure_filters_url($fieldname,$array=array(),$type = 1,$modelid) {  
        if(empty($array)) {  
                $array = $_GET;  
                } else {  
                        $array = array_merge($_GET,$array);  
                        }  
        //TODO  
        $fields = getcache(&#39;model_field_&#39;.$modelid,&#39;model&#39;);  
        if(is_array($fields) && !empty($fields)) {  
                        ksort($fields);  
                        foreach ($fields as $_v=>$_k) {  
                                if($_k[&#39;filtertype&#39;] || $_k[&#39;rangetype&#39;]) {  
                                        if(strpos(URLRULE,&#39;.html&#39;) === FALSE) $urlpars .= &#39;&&#39;.$_v.&#39;={$&#39;.$_v.&#39;}&#39;;  
                                        else $urlpars .= &#39;-{$&#39;.$_v.&#39;}&#39;;  
                                        }  
                                        }  
                                        }  
        //后期增加伪静态等其他url规则管理,apache伪静态支持9个参数  
        if(strpos(URLRULE,&#39;.html&#39;) === FALSE) $urlrule =APP_PATH.&#39;index.php?m=content&c=index&a=lists&catid={$catid}&#39;.$urlpars.&#39;&page={$page}&#39; ;  
        else $urlrule =APP_PATH.&#39;list-{$catid}&#39;.$urlpars.&#39;-{$page}.html&#39;;  
        //根据get传值构造URL  
        if (is_array($array)) foreach ($array as $_k=>$_v) {  
                        if($_k==&#39;page&#39;) $_v=1;  
                        if($type == 1) if($_k==$fieldname) continue;  
                        $_findme[] = &#39;/{\$&#39;.$_k.&#39;}/&#39;;  
                        $_replaceme[] = $_v;  
                        }  
     //type 模式的时候,构造排除该字段名称的正则  
        if($type==1) $filter = &#39;(?!&#39;.$fieldname.&#39;.)&#39;;  
        $_findme[] = &#39;/{\$&#39;.$filter.&#39;([a-z0-9_]+)}/&#39;;  
        $_replaceme[] = &#39;&#39;;  
        $urlrule = preg_replace($_findme, $_replaceme, $urlrule);  
        return         $urlrule;  
}  
  
/** 
 * 生成分类信息中的筛选菜单 
 * @param $field   字段名称 
 * @param $modelid  模型ID 
 */  
function filters($field,$modelid,$diyarr = array()) {  
        $fields = getcache(&#39;model_field_&#39;.$modelid,&#39;model&#39;);  
        $options = empty($diyarr) ?  explode("\n",$fields[$field][&#39;options&#39;]) : $diyarr;  
        $field_value = intval($_GET[$field]);  
        foreach($options as $_k) {  
                $v = explode("|",$_k);  
                $k = trim($v[1]);  
                $option[$k][&#39;name&#39;] = $v[0];  
                $option[$k][&#39;value&#39;] = $k;  
                $option[$k][&#39;url&#39;] = structure_filters_url($field,array($field=>$k),2,$modelid);  
                $option[$k][&#39;menu&#39;] = $field_value == $k ? &#39;<em>&#39;.$v[0].&#39;</em>&#39; : &#39;<a href=&#39;.$option[$k][&#39;url&#39;].&#39;>&#39;.$v[0].&#39;</a>&#39; ;  
        }  
        $all[&#39;name&#39;] = &#39;全部&#39;;  
        $all[&#39;url&#39;] = structure_filters_url($field,array($field=>&#39;&#39;),2,$modelid);  
        $all[&#39;menu&#39;] = $field_value == &#39;&#39; ? &#39;<em>&#39;.$all[&#39;name&#39;].&#39;</em>&#39; : &#39;<a href=&#39;.$all[&#39;url&#39;].&#39;>&#39;.$all[&#39;name&#39;].&#39;</a>&#39;;  
  
        array_unshift($option,$all);  
        return $option;  
}  
  
/** 
 * 获取联动菜单层级 
 * @param  $keyid     联动菜单分类id 
 * @param  $linkageid 菜单id 
 * @param  $leveltype 获取类型 parentid 获取父级id child 获取时候有子栏目 arrchildid 获取子栏目数组 
 */  
function get_linkage_level($keyid,$linkageid,$leveltype = &#39;parentid&#39;) {  
        $child_arr = $childs = array();  
        $leveltypes = array(&#39;parentid&#39;,&#39;child&#39;,&#39;arrchildid&#39;,&#39;arrchildinfo&#39;);  
        $datas = getcache($keyid,&#39;linkage&#39;);  
        $infos = $datas[&#39;data&#39;];  
        if (in_array($leveltype, $leveltypes)) {  
                if($leveltype == &#39;arrchildinfo&#39;) {  
                        $child_arr = explode(&#39;,&#39;,$infos[$linkageid][&#39;arrchildid&#39;]);  
                        foreach ($child_arr as $r) {  
                                $childs[] = $infos[$r];  
                        }  
                        return $childs;  
                } else {  
                        return $infos[$linkageid][$leveltype];  
                }  
        }          
}  
  
// 根据linkageid递归到父级  
function get_parent_url($modelid,$field,$linkageid=0,$array = array()){  
        $modelid = intval($modelid);  
        if(!$modelid || empty($field)) return false;  
        $fields = getcache(&#39;model_field_&#39;.$modelid,&#39;model&#39;);  
        $keyid = $fields[$field][&#39;linkageid&#39;];  
        $datas = getcache($keyid,&#39;linkage&#39;);  
        $infos = $datas[&#39;data&#39;];  
                  
        if(empty($linkageid)){  
                $linkageid = intval($_GET[$field]);  
                if(!$linkageid) return false;  
                }  
                  
                $urlrule = structure_filters_url($field,array(),1,$modelid);  
                $urlrule = str_replace(&#39;{$&#39;.$field.&#39;}&#39;,$infos[$linkageid][&#39;parentid&#39;],$urlrule);  
                array_unshift($array,array(&#39;name&#39;=> $infos[$linkageid][&#39;name&#39;],&#39;url&#39;=>$urlrule));  
                if($infos[$linkageid][&#39;parentid&#39;]){  
                        return get_parent_url($modelid,$field,$infos[$linkageid][&#39;parentid&#39;],$array);  
                        }  
                        return $array;  
                        }  
/** 
 * 构造筛选时候的sql语句 
 */  
function structure_filters_sql($modelid) {  
        $sql = $fieldname = $min = $max = &#39;&#39;;  
        $fieldvalue = array();  
        $modelid = intval($modelid);  
        $model =  getcache(&#39;model&#39;,&#39;commons&#39;);  
        $fields = getcache(&#39;model_field_&#39;.$modelid,&#39;model&#39;);  
        $fields_key = array_keys($fields);  
        //TODO  
        $sql = &#39;`status` = \&#39;99\&#39;&#39;;  
        foreach ($_GET as $k=>$r) {  
                if(in_array($k,$fields_key) && intval($r)!=0 && ($fields[$k][&#39;filtertype&#39;] || $fields[$k][&#39;rangetype&#39;])) {  
                        if($fields[$k][&#39;formtype&#39;] == &#39;linkage&#39;) {  
                                $datas = getcache($fields[$k][&#39;linkageid&#39;],&#39;linkage&#39;);  
                                $infos = $datas[&#39;data&#39;];  
                                if($infos[$r][&#39;arrchildid&#39;]) {  
                                        $sql .=  &#39; AND `&#39;.$k.&#39;` in(&#39;.$infos[$r][&#39;arrchildid&#39;].&#39;)&#39;;  
                                        }  
                                        } elseif($fields[$k][&#39;rangetype&#39;]) {  
                                                if(is_numeric($r)) {  
                                                        $sql .=" AND `$k` = &#39;$r&#39;";  
                                                        } else {  
                                                                $fieldvalue = explode(&#39;_&#39;,$r);  
                                                                $min = intval($fieldvalue[0]);  
                                                                $max = $fieldvalue[1] ? intval($fieldvalue[1]) : 999999;  
                                                                $sql .=" AND `$k` >= &#39;$min&#39; AND  `$k` < &#39;$max&#39;";  
                                                                }  
                                                                } else {  
                                                                        $sql .=" AND `$k` = &#39;$r&#39;";  
                                                                        }  
                                                                        }  
                                                                        }  
                                                                        return $sql;  
                                                                        }  
  
/** 
 * 分页,如去掉则分页会有问题 
 */  
function makeurlrule() {  
        if(strpos(URLRULE,&#39;.html&#39;) === FALSE) {  
                return url_par(&#39;page={$&#39;.&#39;page}&#39;);  
        }  
        else {  
                $url = preg_replace(&#39;/-[0-9]+.html$/&#39;,&#39;-{$page}.html&#39;,get_url());  
                return $url;  
        }  
}  
  
   
?>

然后,内容——模型管理——选择一个模型添加新字段,如图。

How to filter and classify phpcms articles?

最后,在要调用产品筛选的栏目模板页添加标签 ,如下。

<span>性别:</span>    
{loop filters(&#39;xingbie&#39;,$modelid) $r}  
{$r[menu]}  
{/loop}
{php $sql = structure_filters_sql($modelid)}  
{php $urlrule = makeurlrule()}  
    {pc:content action="lists" where="$sql" catid="$catid" num="10" order="id DESC" page="$page"  urlrule="$urlrule"}  
      
        <ul class="photo-list picbig">  
            {loop $data $r}  
            <li>  
            <div class="img-wrap">  
            <a href="{$r[url]}"><img src="{thumb($r[thumb],150,112)}" width="150"    style="max-width:90%" alt="{$r[title]}"/></a>  
            </div>  
            <span style="color:{$r[style]}">{str_cut($r[title],28)}</span>  
            </li>  
            {/loop}  
        </ul>  
        <div id="pages" class="text-c">{$pages}</div>  
    {/pc}

推荐教程:《phpcms

The above is the detailed content of How to filter and classify phpcms articles?. 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