Home  >  Article  >  php教程  >  无限极分类

无限极分类

WBOY
WBOYOriginal
2016-06-07 11:44:001089browse

自己在做酒店管理的时候,直接做成无限极,顺便将代码分享出来
    //酒店和房型信息列表<br>     public function index(){<br>         $Hotel=M('Hotel');<br>         $hotelList=$Hotel->field('id,name,pid,path,concat(path,"-",id) as bpath ,sort')->order('sort desc,bpath')->select();<br>         foreach ($hotelList as $k=>$v){<br>             $hotelList[$k]['count']=intval(count(explode('-',$v['path']))-1);  // 判断无限极层次深度<br>         }<br>         if($hotelList){<br>             $this->hotelList=$hotelList;<br>         }<br>         $this->display();<br>     }<br>     <br> <br>     //添加酒店/房型<br>     public function add(){<br>         if(IS_POST){ //post方法提交<br>             if($_POST['name']==''){<br>                 $this->success('名称不能为空!');die;<br>             }<br>             $Hotel=M('Hotel');<br>             if(intval($_POST['pid']) > 0){<br>                 $parent=$Hotel->where(array('id'=>$this->_post('pid')))->find();<br>                 $_POST['path']=$parent['path'].'-'.$_POST['pid'];<br>             }<br>             $_POST['time']=time();<br>             $data=getPost($_POST);//转义所有进入post方法的非法字符<br>             $Hotel=M('Hotel');<br>             if($Hotel->add($data)){<br>                 $this->success('保存成功!');<br>             }else {<br>                 $this->error('保存失败!');die;<br>             }<br>         }else{<br>             $this->error('非法请求');<br>         }    <br>     }<br>     <br> <br> 数据库:<br> <br> SET FOREIGN_KEY_CHECKS=0;<br> <br> -- ----------------------------<br> -- Table structure for `yh_hotel`<br> -- ----------------------------<br> DROP TABLE IF EXISTS `yh_hotel`;<br> CREATE TABLE `yh_hotel` (<br>   `id` int(11) unsigned NOT NULL auto_increment,<br>   `name` varchar(30) default '' COMMENT '酒店或者房型',<br>   `sort` int(11) unsigned default '0' COMMENT '排序',<br>   `path` varchar(50) default '0',<br>   `pid` int(11) unsigned default '0' COMMENT '父级id',<br>   PRIMARY KEY  (`id`),<br>   KEY `name` (`name`)<br> ) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;<br> <br> -- ----------------------------<br> -- Records of yh_hotel<br> -- ----------------------------<br> INSERT INTO `yh_hotel` VALUES ('1', 'A', '0', '0', '0');<br> INSERT INTO `yh_hotel` VALUES ('2', 'B', '0', '0', '0');<br> INSERT INTO `yh_hotel` VALUES ('3', 'A-1', '0', '0-1', '1');<br> INSERT INTO `yh_hotel` VALUES ('4', 'A-2', '0', '0-1', '1');<br> INSERT INTO `yh_hotel` VALUES ('5', 'B-1', '0', '0-2', '2');<br> INSERT INTO `yh_hotel` VALUES ('6', 'B-2', '0', '0-2', '2');<br> INSERT INTO `yh_hotel` VALUES ('7', 'B-1-1', '0', '0-2-5', '5');<br> INSERT INTO `yh_hotel` VALUES ('8', 'B-1-2', '0', '0-2-5', '5');<br> <br> <img src="/Uploads/editor/2013-12-23/52b7c3ded6751.jpg" alt="无限极分类" >

AD:真正免费,域名+虚机+企业邮箱=0元

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