关于分类过多的时候解决方案。

创建时间:2020-08-20 14:27发布者:二次开发,模板制作点击数:1284
这个问题很多年了,客户是IC行业,分类大概有几百个或者上千个,用douphp来做发现非常卡。
发现瓶颈出现在获取分类这里,所以优化了一下,优化完了以后基本秒开页面。

代码修改如下:

/**
* +----------------------------------------------------------
* 获取当前分类下所有子分类
* +----------------------------------------------------------
* $table 数据表名
* $parent_id 父类ID
* $child 子类ID零时存储器
* +----------------------------------------------------------
*/
function dou_child_id($table, $parent_id = '0', &$child_id = '') {

$data = $this->fn_query("SELECT * FROM " . $this->table($table) . " WHERE parent_id = '{$parent_id}' ORDER BY sort ASC, cat_id ASC");
foreach ((array) $data as $value) {
if ($value['parent_id'] == $parent_id) {
$child_id .= ',' . $value['cat_id'];
$this->dou_child_id($table, $value['cat_id'], $child_id);
}
}
return $child_id;
}

/**
* +----------------------------------------------------------
* 获取无层次商品分类,将所有分类存至同一级数组,用$mark作为标记区分
* +----------------------------------------------------------
* $table 数据表名
* $parent_id 默认获取一级导航
* $level 无限极分类层次
* $current_id 当前页面栏目ID
* $category_nolevel 储存分类信息的数组
* $mark 无限极分类标记
* +----------------------------------------------------------
*/
function get_category_nolevel($table, $parent_id = 0, $level = 0, $current_id = '', &$category_nolevel = array(), $mark = '-') {
$data = $this->fn_query("SELECT * FROM " . $this->table($table) . " WHERE parent_id = {$parent_id} ORDER BY sort ASC, cat_id ASC");
foreach ((array) $data as $value) {
if ($value['parent_id'] == $parent_id && $value['cat_id'] != $current_id) {
$value['url'] = $this->rewrite_url($table, $value['cat_id']);
$value['mark'] = str_repeat($mark, $level);
$category_nolevel[] = $value;
$this->get_category_nolevel($table, $value['cat_id'], $level + 1, $current_id, $category_nolevel);
}
}

return $category_nolevel;
}



/**
* +----------------------------------------------------------
* 获取有层次的栏目分类,有几层分类就创建几维数组
* +----------------------------------------------------------
* $table 数据表名
* $parent_id 默认获取一级导航
* $current_id 当前页面栏目ID
* +----------------------------------------------------------
*/
function get_category($table, $parent_id = 0, $current_id = '') {
$category = array ();
// $cache_key = "cache_".$table.'_'.$parent_id.'_'.$current_id;
// //缓存分类结果
// $cache_data = cache_read($cache_key);
// if (!empty($cache_data)){
// return $cache_data;
// }
// $cache_data = cache_read('cache_'.$table);
// if (empty($cache_data)){
// $data = $this->fn_query("SELECT * FROM " . $this->table($table) . " ORDER BY sort ASC, cat_id ASC");
// cache_write('cache_'.$table,(array)$data);
// }else{
// $data = $cache_data;
// }

//另外一种解决方案

$data = $this->fn_query("SELECT * FROM " . $this->table($table) . " WHERE parent_id = {$parent_id} ORDER BY sort ASC, cat_id ASC");

if (empty($data)){
return false;
}
foreach ((array) $data as $value) {
// $parent_id将在嵌套函数中随之变化
if ($value['parent_id'] == $parent_id) {
$value['url'] = $this->rewrite_url($table, $value['cat_id']);
$value['cur'] = $value['cat_id'] == $current_id ? true : false;
$value['image'] = $this->dou_file($value['image']);
$child = $this->get_category($table, $value['cat_id'], $current_id);
$value['child'] = $child;
// foreach ($data as $child) {
// // 筛选下级导航
// if ($child['parent_id'] == $value['cat_id']) {
// // 嵌套函数获取子分类
// $value['child'] = $this->get_category($table, $value['cat_id'], $current_id);
// break;
// }
// }
$category[] = $value;
}
}
// cache_write($cache_key,$category);
return $category;
}


回答 我也遇到这个问题(0)