Rumah > Tutorial CMS > PHPCMS > teks badan

phpcms文章如何筛选分类?

coldplay.xixi
Lepaskan: 2020-06-10 10:55:48
asal
2950 orang telah melayarinya

phpcms文章如何筛选分类?

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;  
        }  
}  
  
   
?>
Salin selepas log masuk

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

d892bac2523657303fe71ee5f3f5cc3.png

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

<span>性别:</span>    
{loop filters(&#39;xingbie&#39;,$modelid) $r}  
{$r[menu]}  
{/loop}
Salin selepas log masuk
{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" height="112" 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}
Salin selepas log masuk

推荐教程:《phpcms

Atas ialah kandungan terperinci phpcms文章如何筛选分类?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel terbaru oleh pengarang
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan