Home  >  Article  >  Backend Development  >  PHP recursively writes to MySQL to implement infinite level classification data operation example_php skills

PHP recursively writes to MySQL to implement infinite level classification data operation example_php skills

无忌哥哥
无忌哥哥Original
2018-07-12 14:29:012497browse

This article mainly introduces PHP recursive writing to MySQL to achieve unlimited classification data operations, involving the creation of mysql database and PHP recursive writing and reading database classification related operating skills. Friends in need can refer to the following

The example of this article describes the recursive writing of PHP to MySQL to achieve unlimited classification data operations. Share it with everyone for your reference, the details are as follows:

PHP recursively writes MySQL unlimited classification 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 '父级分类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;

Recursive method writing code:

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;
}

Calling code:

KeplerCategory::addCategoryFromKepler(0);

Recursive method reading code:

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;
}

Calling code:

$category_list = KeplerCategory::getCategoryFormatToKepler(0);

The above is the detailed content of PHP recursively writes to MySQL to implement infinite level classification data operation example_php skills. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn