您好,登錄后才能下訂單哦!
如何篩選分類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 = '', $fieldname='zone') { $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,'linkage'); $infos = $datas['data']; foreach($infos as $k=>$v){ if($v['parentid']==$field_value){ $array[$k]['name'] = $v['name']; $array[$k]['value'] = $k; $array[$k]['url'] = str_replace('{'.$fieldname.'}',$k,$urlrule); $array[$k]['menu'] = $field_value == $k ? '<em>'.$v['name'].'</em>' : '<a href='.$array[$k]['url'].'>'.$v['name'].'</a>' ; } } 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('model_field_'.$modelid,'model'); if(is_array($fields) && !empty($fields)) { ksort($fields); foreach ($fields as $_v=>$_k) { if($_k['filtertype'] || $_k['rangetype']) { if(strpos(URLRULE,'.html') === FALSE) $urlpars .= '&'.$_v.'={$'.$_v.'}'; else $urlpars .= '-{$'.$_v.'}'; } } } //后期增加偽靜態等其他url規則管理,apache偽靜態支持9個參數 if(strpos(URLRULE,'.html') === FALSE) $urlrule =APP_PATH.'index.php?m=content&c=index&a=lists&catid={$catid}'.$urlpars.'&page={$page}' ; else $urlrule =APP_PATH.'list-{$catid}'.$urlpars.'-{$page}.html'; //根據get傳值構造URL if (is_array($array)) foreach ($array as $_k=>$_v) { if($_k=='page') $_v=1; if($type == 1) if($_k==$fieldname) continue; $_findme[] = '/{\$'.$_k.'}/'; $_replaceme[] = $_v; } //type 模式的時候,構造排除該字段名稱的正則 if($type==1) $filter = '(?!'.$fieldname.'.)'; $_findme[] = '/{\$'.$filter.'([a-z0-9_]+)}/'; $_replaceme[] = ''; $urlrule = preg_replace($_findme, $_replaceme, $urlrule); return $urlrule; } /** * 生成分類信息中的篩選菜單 * @param $field 字段名稱 * @param $modelid 模型ID */ function filters($field,$modelid,$diyarr = array()) { $fields = getcache('model_field_'.$modelid,'model'); $options = empty($diyarr) ? explode("\n",$fields[$field]['options']) : $diyarr; $field_value = intval($_GET[$field]); foreach($options as $_k) { $v = explode("|",$_k); $k = trim($v[1]); $option[$k]['name'] = $v[0]; $option[$k]['value'] = $k; $option[$k]['url'] = structure_filters_url($field,array($field=>$k),2,$modelid); $option[$k]['menu'] = $field_value == $k ? '<em>'.$v[0].'</em>' : '<a href='.$option[$k]['url'].'>'.$v[0].'</a>' ; } $all['name'] = '全部'; $all['url'] = structure_filters_url($field,array($field=>''),2,$modelid); $all['menu'] = $field_value == '' ? '<em>'.$all['name'].'</em>' : '<a href='.$all['url'].'>'.$all['name'].'</a>'; 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 = 'parentid') { $child_arr = $childs = array(); $leveltypes = array('parentid','child','arrchildid','arrchildinfo'); $datas = getcache($keyid,'linkage'); $infos = $datas['data']; if (in_array($leveltype, $leveltypes)) { if($leveltype == 'arrchildinfo') { $child_arr = explode(',',$infos[$linkageid]['arrchildid']); 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('model_field_'.$modelid,'model'); $keyid = $fields[$field]['linkageid']; $datas = getcache($keyid,'linkage'); $infos = $datas['data']; if(empty($linkageid)){ $linkageid = intval($_GET[$field]); if(!$linkageid) return false; } $urlrule = structure_filters_url($field,array(),1,$modelid); $urlrule = str_replace('{$'.$field.'}',$infos[$linkageid]['parentid'],$urlrule); array_unshift($array,array('name'=> $infos[$linkageid]['name'],'url'=>$urlrule)); if($infos[$linkageid]['parentid']){ return get_parent_url($modelid,$field,$infos[$linkageid]['parentid'],$array); } return $array; } /** * 構造篩選時候的sql語句 */ function structure_filters_sql($modelid) { $sql = $fieldname = $min = $max = ''; $fieldvalue = array(); $modelid = intval($modelid); $model = getcache('model','commons'); $fields = getcache('model_field_'.$modelid,'model'); $fields_key = array_keys($fields); //TODO $sql = '`status` = \'99\''; foreach ($_GET as $k=>$r) { if(in_array($k,$fields_key) && intval($r)!=0 && ($fields[$k]['filtertype'] || $fields[$k]['rangetype'])) { if($fields[$k]['formtype'] == 'linkage') { $datas = getcache($fields[$k]['linkageid'],'linkage'); $infos = $datas['data']; if($infos[$r]['arrchildid']) { $sql .= ' AND `'.$k.'` in('.$infos[$r]['arrchildid'].')'; } } elseif($fields[$k]['rangetype']) { if(is_numeric($r)) { $sql .=" AND `$k` = '$r'"; } else { $fieldvalue = explode('_',$r); $min = intval($fieldvalue[0]); $max = $fieldvalue[1] ? intval($fieldvalue[1]) : 999999; $sql .=" AND `$k` >= '$min' AND `$k` < '$max'"; } } else { $sql .=" AND `$k` = '$r'"; } } } return $sql; } /** * 分頁,如去掉則分頁會有問題 */ function makeurlrule() { if(strpos(URLRULE,'.html') === FALSE) { return url_par('page={$'.'page}'); } else { $url = preg_replace('/-[0-9]+.html$/','-{$page}.html',get_url()); return $url; } } ?>
然后,內容——模型管理——選擇一個模型添加新字段,如圖。
最后,在要調用產品篩選的欄目模板頁添加標簽 ,如下。
<span>性別:</span> {loop filters('xingbie',$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" 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}
看完上述內容,你們掌握如何篩選分類phpcms文章的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。