PHP recursive write MySQL implementation infinite class data operation example _php tips

Source: Internet
Author: User
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);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.