這篇文章主要介紹了PHP遞歸寫入MySQL實現無限級分類資料操作,涉及mysql資料庫的創建以及php遞歸寫入、讀取資料庫分類相關操作技巧,需要的朋友可以參考下
本文實例敘述了PHP遞歸寫入MySQL實現無限級分類資料操作。分享給大家供大家參考,具體如下:
PHP遞歸寫入MySQL無限級分類數據,表結構:
CREATE TABLE `kepler_goods_category` ( `id` int unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT, `parentid` int unsigned NOT NULL default 0 comment '父级分类ID', `name` varchar(255) NOT NULL default '' comment '分类名称', `kepler_fid` int unsigned NOT NULL default 0 comment '对应开普勒分类ID', `create_time` timestamp NOT NULL default CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
############################################ #遞歸方法寫入程式碼:############
static public function addCategoryFromKepler($fid, $parentid = 0){ $category_list = Kepler::queryGoodsCategoryList($fid); // 获取数据 $category_list = $category_list['jd_kepler_item_querycategoriesbyfid_response']; if($category_list['total'] > 0){ foreach ($category_list['categories'] as $key => $value) { $parentid_sub = KeplerCategory::addCategory($value['name'], $value['id'], $parentid); // 插入数据库,得到父ID self::addCategoryFromKepler($value['id'], $parentid_sub); // 递归 } } return true; }
KeplerCategory::addCategoryFromKepler(0);
static public function getCategoryFormatToKepler($parentid, $format_data = array(), $parent_prefix = '', $current_prefix = ''){ $category_list = self::getCategoryByParentid($parentid); // 根据父ID获取 if(!empty($category_list)){ foreach ($category_list as $key => $value) { $format_data = self::getCategoryFormatToKepler($value['id'], $format_data, $parent_prefix . ',' . $current_prefix, $value['kepler_fid']); } }else{ $format_data[] = trim($parent_prefix . ',' . $current_prefix, ','); } return $format_data; }
$category_list = KeplerCategory::getCategoryFormatToKepler(0);
以上是PHP遞歸寫入MySQL實作無限級分類資料操作範例_php技巧的詳細內容。更多資訊請關注PHP中文網其他相關文章!