laravel default value in the three-level classification drop-down box

 

This is a commodity table that records the classification of commodities, divided into three categories, top-level category, first-level category, and second-level category

 

The view of modifying the information of the product in the editor

 

First, pass the id of the product to the controller, obtain all the information of the product according to the id, and obtain all the classification information

 

Then perform a family tree search, the parameters are classification information-array, product classification id-integer

 

In this way, the two-dimensional array of the child to the parent is obtained,

 

This two-dimensional array is infinitely classified to get a three-dimensional array

 

The first dimension is the top-level classification, the second dimension is the first-level classification, and the third dimension is the second-level classification

 

Pass the obtained array to the view, loop through it, and output the value to the option

 

Family tree code:

 

/** 
* Family tree, find parent id by child id
* @param array $data The data to be classified
* @param int /string $pid The ancestor node to be found
* @return array
*/
function Ancestry($data , $pid ) {
static $ancestry = array();

foreach($data as $key => $value) {
if($value['id'] == $pid) {
$ancestry[] = $value;

Ancestry($data , $value['parent_id']);
}
}
return $ancestry;
}


infinity is classified as a multidimensional array

/** 
* Reassemble infinite classes into multidimensional arrays
* @param $array
* @param int $pid
* @return array
*/
function dataTree($array, $pid = 0){
$arr = [];
foreach ($ array as $item) {
if ($item['parent_id'] == $pid){
$tmp = dataTree($array,$item['id']);
$tmp && $item['son'] = $ tmp;
$arr[] = $item;
}
}
return $arr;
}
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324804786&siteId=291194637