Tp5.1 unlimited classified foreground display

  • The multi-level classification display of the mall front desk, as shown in the figure, is generally three levels. So the code is only assembled to level three.



  • Database design, a total of 5 fields. The level field is convenient for displaying levels. PID is the parent classification ID, and PID is equal to 0, indicating that it is a primary classification. Path field, for example. Shorts with ID 8 0,1,3,8

It belongs to clothing (1) at the first level, men’s wear (3) at the second level and shorts (8) at the third level. The preceding 0 indicates that it belongs to the root classification, and each classification belongs to the root classification (0). As shown in the second picture below






  • In the controller



$DB = new Db;
        // if(!$data){

          $data = $DB:: table ("Wuxian") - > where ('pid ', 0) - > select(); // get the first level classification array
          foreach($data as $k=>$v){//k=0
            $id = $data[$k]['id'];
            $data2 = $DB:: table ("Wuxian") - > where ('pid ', $ID) - > select(); // get the secondary classification array
            $data [$k] ['child '] = $data2; // assemble the secondary to first level array data
              foreach($data2 as $k2 => $v2){//k2=

                $id2 = $data2[$k2]['id'];
                $data3 = $DB:: table ("Wuxian") - > where ('pid ', $Id2) - > select(); // get the three-level classification array
                $data [$k] ['child '] [$K2] ['child'] = $data3; // first find the second level array, and then assemble the third level to the second level array data2