- // Obtain all descendant classes without recursion
- Function get_offspring ($ pids, $ list)
- {
- $ Npid = array ();
- $ Offspring = array ();
- $ Has_child = true;
- While ($ has_child)
- {
- $ Has_child = false;
- Foreach ($ list as $ lk => $ lv)
- {
- If (in_array ($ lv ['pid'], $ pids ))
- {
- $ Offspring [] = $ lv;
- $ Npid [] = $ lv ['CID'];
- Unset ($ list [$ lk]);
- $ Has_child = true;
- }
- }
- $ Pids = $ npid;
- }
- Return $ offspring;
- }
- // Use the path field to obtain the generation class
- Function get_offspring ($ pid)
- {
- $ Offspring = array ();
- $ Cats = $ this-> getList ('cat _ id, cat_name, parent_id, cat_path ', array (), 0,-1 );
- Foreach ($ cats as $ cv)
- {
- If (in_array ($ pid, explode (',', $ cv ['cat _ path'])
- {
- $ Offspring [] = $ cv;
- }
- }
- Return $ offspring;
- }
- // Update the classification path of the later generation
- Function update_offspring_path ($ pid, $ ppath)
- {
- If ($ ppath = ',')
- {
- $ Ppath = '';
- }
- $ Offspring = $ this-> get_offspring ($ pid );
- Foreach ($ offspring as $ ov)
- {
- $ Ov ['cat _ path'] = substr ($ ov ['cat _ path'], 0, strlen ($ ov ['cat _ path'])-1 );
- $ Old_path = explode (',', $ ov ['cat _ path']);
- Foreach ($ old_path as $ oldk => $ oldv)
- {
- If ($ oldv = $ pid)
- {
- Break;
- }
- Unset ($ old_path [$ oldk]);
- }
- $ New_path = $ ppath. implode (',', $ old_path ).',';
- If (! $ This-> update (array ('cat _ path' => $ new_path), array ('cat _ id' => $ ov ['cat _ id'])
- {
- Return false;
- }
- }
- Return true;
- }
- // Organize the list into a tree
- Function get_tree_list ($ pid, $ arr, & $ r)
- {
- Foreach ($ arr as $ k => $ v)
- {
- If ($ v ['parent _ id'] = $ pid)
- {
- If (isset ($ r [$ pid]) // you can specify a flag containing a subclass.
- {
- $ R [$ pid] ['has _ child '] = 1;
- }
- $ V ['cat _ path'] = trim ($ v ['cat _ path']); // calculate the depth
- $ Path_str = substr ($ v ['cat _ path'], 0, strlen ($ v ['cat _ path'])-1 );
- If ($ path_str = '')
- {
- $ V ['deep '] = 0;
- }
- Else
- {
- $ V ['deep '] = count (explode (', ', $ path_str ));
- }
- $ V ['format'] = str_repeat ('', 6 * $ v ['deep ']); // calculate indentation
- $ R [$ v ['cat _ id'] = $ v; // add to the ordered list
- Unset ($ arr [$ k]); // added to remove from unordered list
- $ This-> get_tree_list ($ v ['cat _ id'], $ arr, $ r );
- }
- }
- }
|