PHP遞歸寫入MySQL實現(xiàn)無限級分類數(shù)據(jù)操作示例
本文實例講述了PHP遞歸寫入MySQL實現(xiàn)無限級分類數(shù)據(jù)操作。分享給大家供大家參考,具體如下:
PHP遞歸寫入MySQL無限級分類數(shù)據(jù),表結(jié)構(gòu):
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 '對應(yīng)開普勒分類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); // 獲取數(shù)據(jù)
$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); // 插入數(shù)據(jù)庫,得到父ID
self::addCategoryFromKepler($value['id'], $parentid_sub); // 遞歸
}
}
return true;
}
調(diào)用代碼:
KeplerCategory::addCategoryFromKepler(0);
遞歸方法讀取代碼:
static public function getCategoryFormatToKepler($parentid, $format_data = array(), $parent_prefix = '', $current_prefix = ''){
$category_list = self::getCategoryByParentid($parentid); // 根據(jù)父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;
}
調(diào)用代碼:
$category_list = KeplerCategory::getCategoryFormatToKepler(0);
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php+mysql數(shù)據(jù)庫操作入門教程》、《php+mysqli數(shù)據(jù)庫程序設(shè)計技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
- 詳解mysql DML語句的使用
- MySQL的DML語言操作實例
- MySQL DML語句整理匯總
- python的mysql數(shù)據(jù)庫建立表與插入數(shù)據(jù)操作示例
- Go語言集成mysql驅(qū)動、調(diào)用數(shù)據(jù)庫、查詢數(shù)據(jù)操作示例
- PHP使用PDO創(chuàng)建MySQL數(shù)據(jù)庫、表及插入多條數(shù)據(jù)操作示例
- PHP5.5基于mysqli連接MySQL數(shù)據(jù)庫和讀取數(shù)據(jù)操作實例詳解
- 使用Python對MySQL數(shù)據(jù)操作
- MySQL數(shù)據(jù)操作-DML語句的使用
相關(guān)文章
如何提高M(jìn)YSQL數(shù)據(jù)庫的查詢統(tǒng)計速度 select 索引應(yīng)用
如何提高M(jìn)YSQL數(shù)據(jù)庫的查詢統(tǒng)計速度 select 索引應(yīng)用2007-04-04
并發(fā)下常見的加鎖及鎖的PHP具體實現(xiàn)代碼
用到了Eaccelerator的內(nèi)存鎖 和 文件鎖,原理如下判斷系統(tǒng)中是否安了EAccelerator 如果有則使用內(nèi)存鎖,如果不存在,則進(jìn)行文件鎖2010-10-10
PHP實現(xiàn)通過正則表達(dá)式替換回調(diào)的內(nèi)容標(biāo)簽
這篇文章主要介紹了PHP實現(xiàn)通過正則表達(dá)式替換回調(diào)的內(nèi)容標(biāo)簽的方法,涉及php正則匹配與替換的相關(guān)技巧,需要的朋友可以參考下2015-06-06
mac系統(tǒng)下為 php 添加 pcntl 擴(kuò)展
pcntl中php實現(xiàn)多進(jìn)程必須要安裝的擴(kuò)展,本文給大家簡單介紹下如何在mac系統(tǒng)中為 php 添加 pcntl 擴(kuò)展2016-08-08
php實現(xiàn)統(tǒng)計網(wǎng)站在線人數(shù)的方法
這篇文章主要介紹了php實現(xiàn)統(tǒng)計網(wǎng)站在線人數(shù)的方法,通過獲取服務(wù)器端網(wǎng)絡(luò)參數(shù)及文本文件讀寫實現(xiàn)統(tǒng)計在線人數(shù)的功能,非常簡單實用,需要的朋友可以參考下2015-05-05
php支持?jǐn)帱c(diǎn)續(xù)傳、分塊下載的類
這篇文章主要介紹了php支持?jǐn)帱c(diǎn)續(xù)傳、分塊下載的類的相關(guān)資料,需要的朋友可以參考下2016-05-05
PHP實現(xiàn)將標(biāo)點(diǎn)符號正則替換為空格的方法
這篇文章主要介紹了PHP實現(xiàn)將標(biāo)點(diǎn)符號正則替換為空格的方法,結(jié)合實例形式分析了php針對符號的正則匹配相關(guān)操作技巧,需要的朋友可以參考下2017-08-08

