mysql 预查询-无限极分类

时间:2025-04-03 10:04:32
  • class Test extends Controller{
  • public function index(){
  • $id = input('id');
  • //$this->setparent();
  • $this->setchild($id);
  • }
  • public function setparent(){
  • $list = [];
  • $list = db('ce')->insert([
  • 'cname'=>rand(1000,9999),
  • 'f_id'=>0,
  • 'left_num'=>1,
  • 'right_num'=>2,
  • 'level' =>1
  • ]);
  • }
  • public function setchild($fid){
  • $p_data = db('ce')->where('id',$fid)->find();
  • $max_data = db('ce')->where('f_id',$fid)->max('right_num');
  • if(empty($max_data)){
  • db('ce')->where('right_num','>=',$p_data['right_num'])->setInc('right_num',2);
  • }else{
  • db('ce')->where('left_num','>=',$p_data['left_num'])->setInc('left_num',2);
  • db('ce')->where('right_num','>=',$p_data['right_num'])->setInc('right_num',2);
  • }
  • $list = [];
  • $list = db('ce')->insert([
  • 'cname'=>rand(1000,9999),
  • 'f_id'=>$p_data['id'],
  • 'left_num'=>$p_data['left_num']+1,
  • 'right_num'=>$p_data['right_num']+1,
  • 'level' =>1
  • ]);
  • }
  • public function query($id){
  • $p_data = db('ce')->where('id',$id)->find();
  • $data = db('ce')->where('pt_left','>',$p_data['left_num'])->where('pt_right','<',$p_data['right_num'])->select();
  • }
  • }