This article mainly introduces the PHP recursive writing to achieve unlimited class data operation, involving the creation of MySQL database and PHP recursive write, read database classification related operations skills, the need for friends can refer to the next
This article describes the PHP recursive write to MySQL to achieve unlimited class data operation. Share to everyone for your reference, as follows:
PHP recursively writes MySQL infinite-level categorical data, table structure:
CREATE TABLE ' kepler_goods_category ' (' id ' int unsigned not NULL PRIMARY KEY auto_increment, ' parentid ' int unsigned not Null default 0 Comment ' Parent class id ', ' name ' varchar (255) Not NULL default ' comment ' class name ', ' kepler_fid ' int unsigned not NUL L default 0 Comment ' corresponds to Kepler class id ', ' create_time ' timestamp not NULL default current_timestamp on UPDATE current_timestamp) E Ngine=innodb DEFAULT Charset=utf8;
The recursive method writes the code:
static public Function Addcategoryfromkepler ($fid, $parentid = 0) { $category _list = kepler::querygoodscategorylist ($FID); Get Data $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); Insert database, get parent ID self::addcategoryfromkepler ($value [' id '], $parentid _sub);//Recursive } } return true;}
Calling code:
Keplercategory::addcategoryfromkepler (0);
The recursive method reads the code:
static public Function Getcategoryformattokepler ($parentid, $format _data = Array (), $parent _prefix = ", $current _prefix = ') { $category _list = Self::getcategorybyparentid ($parentid);//Get if (!empty ($category _list) based on parent ID) { 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;}
Calling code:
$category _list = keplercategory::getcategoryformattokepler (0);