用户
 找回密码
 入住 CI 中国社区
搜索
查看: 11681|回复: 11
收起左侧

[程序 App] [K]本人写的一个无限级分类,控制器加视图只查一次数据库

[复制链接]
发表于 2008-4-27 22:26:39 | 显示全部楼层 |阅读模式
控制器


PHP复制代码
<?php
 
class Class_Controller extends Controller {
        public $adminDate = FALSE;
        public $classArray = FALSE;
        public $classtree = FALSE;
        function __construct(){
                parent::__construct();
 
        //      $this->session = Passport::chkadmin();
        }
 
        public function index($bclassid = 0){
                $view = new View('class_list');
 
                $db = new Database();
                $this -> classArray = $db -> select("*") -> from('class') -> orderby('myorder','asc') -> orderby('classid','asc') -> get() -> result_array(FALSE);
                if ( count( $this -> classArray ) > 0 ){
                        $tmpTree = array();
                        foreach ( $this -> classArray as  $tmpclass ){
                                $tmpTree[$tmpclass['bclassid']][] = $tmpclass;
                        }
                        $this -> classtree = $tmpTree;
 
                        $myclass = $this -> _listclass($bclassid);
                        $view -> set ( 'list' , $myclass );
                }
 
                $view -> render(TRUE);
        }
 
        public function _listclass($bclassid=0){
                $bclassid = (int)$bclassid;
                if($bclassid > 0){}else{$bclassid=0;}
 
                $tmpclass =$this -> classtree [$bclassid];
 
                if ( count( $tmpclass ) > 0 ){
                        foreach ( $tmpclass as $r ){
                                $classarray[ $r['classid'] ] = $r;
                                $classarray[ $r['classid'] ]['sonclass'] = $this -> _listclass( $r['classid'] );
                        }
                        return $classarray;
                }
        }
       
        public function edit($id=0){
                $view = new View('class_add');
                if ($id>0){
                        $view -> title = '修改栏目';
                        $db = new Database();
                        $result = $db->from('class')->where(array('classid' => $id)) -> limit(1) -> get() -> result_array(FALSE);
                        if ($result){
                                $view -> set('class',$result[0]);
                        }
                }else{
                        $view -> title = '新增栏目';
                }
                $view -> render(TRUE);
        }
       
       
}
 
 
复制代码




视图文件class_list.php
PHP复制代码
<table border="0" cellpadding="3" cellspacing="1" width="96%" align="center" class="tableborder">
<tr>
        <th class="td1" width="40"><b>排序</b></th>
        <th class="td1" width="50"><b>ID</b></th>
        <th class="td1"><b>栏目名称</b></th>
        <th class="td1" width="60"><b>访问次数</b></th>
        <th class="td1" width="200"><b>操作</b></th>
</tr>
<?php
$count_i = 0;
function listclass($list,$spacer=''){
global $count_i;
if($list):
        $countlist = count($list);
        $i = 0;
        foreach ($list as $item):
        $i++;
        $count_i++;
        if ($item['islast'] ==0){
                        if ($item['sonclass']){
                                $spacer1 = $spacer .'<img src="/images/tree_add.gif" align="absmiddle" /> ';
                        }
                        else{
                                $spacer1 = $spacer .'<img src="/images/tree_del.gif" align="absmiddle" /> ';
                        }
        }else{
                $spacer1 = $spacer .' ';
        }
?>
<tr<?php if($count_i%2==0){echo ' class="td3"';} ?>>
        <td class="td1" align="center"><input type="text" size="3" id="paixu_<?=$item['classid']?>" value="<?=$item['myorder']?>" /></td>
        <td class="td1" align="center"><?=$item['classid']?></td>
        <td class="td2"><?=$spacer1?><img src="/images/<?=$item['islast']?'ie':'dir';?>.gif" align="absmiddle" /><a href"<?=$item['id']?>/" target="_blank"><?=$item['classname']?></a></td>
        <td class="td2" align="center"><?=$item['hits']?></td>
        <td class="td2" align="center"><input onclick="goUrl('<?=url::site('class/edit/'.$item['classid']) ?>')" type="button" value="修改栏目" class="btn" /> <input onclick="goUrl('<?=url::site('class/del/'.$item['id'])?>','hiddenFrame')" type="button" value="删除栏目" class="btn" /></td>
</tr>
<?php
//echo ($i+1).'|'.$countlist.'|'.$item['classname'];
        if ($item['sonclass']){
                $spacer .= '  ';
                listclass($item['sonclass'],$spacer);
                $spacer = substr($spacer,0,-strlen('  '));
        }
        endforeach;
endif;
}
listclass($list);
?>
<tr>
        <td class="td1" colspan="5">
        <input onclick="goUrl('<?=url::site('notice/send') ?>')" type="button" value="修改栏目顺序" class="btnl" />
        </td>
</tr>
</table>
 
复制代码



MYSQL
SQL复制代码
# MySQL-Front 3.2  (Build 13.16)
 
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES */;
 
/*!40101 SET NAMES utf8 */;
/*!40103 SET TIME_ZONE='SYSTEM' */;
 
# Host: localhost    DATABASE: myqeeunion
# ------------------------------------------------------
# Server version 5.0.41-community-nt
 
/*!40101 SET NAMES utf8 */;
 
#
# TABLE STRUCTURE FOR TABLE my_class
#
 
DROP TABLE IF EXISTS `my_class`;
CREATE TABLE `my_class` (
  `classid` INT(11) NOT NULL AUTO_INCREMENT,
  `bclassid` INT(11) DEFAULT NULL,
  `classname` VARCHAR(50) DEFAULT NULL,
  `sonclass` text,
  `islast` tinyint(1) DEFAULT NULL,
  `hits` INT(11) DEFAULT NULL,
  `classpath` text,
  `classtype` VARCHAR(20) DEFAULT NULL,
  `timeline` INT(11) DEFAULT NULL,
  `myorder` SMALLINT(6) DEFAULT NULL,
  PRIMARY KEY  (`classid`),
  KEY `bclassid` (`bclassid`)
) ENGINE=MyISAM AUTO_INCREMENT=16 DEFAULT CHARSET=utf8;
 
#
# Dumping DATA FOR TABLE my_class
#
 
INSERT INTO `my_class` VALUES (1,0,'手机小说','|10|11|12|13|14|15|',0,0,'book','.html',NULL,0);
INSERT INTO `my_class` VALUES (2,1,'玄幻·奇幻','|10|11|12|13|14|15|',0,0,'book/xuanhuan','.html',NULL,0);
INSERT INTO `my_class` VALUES (3,1,'武侠·仙侠','',0,0,'book/wuxia','.html',NULL,0);
INSERT INTO `my_class` VALUES (4,1,'都市·言情','',0,0,'book/yanqing','.html',NULL,0);
INSERT INTO `my_class` VALUES (5,1,'历史·军事','',0,0,'book/junshi','.html',NULL,0);
INSERT INTO `my_class` VALUES (6,1,'游戏·竞技','',0,0,'book/youxi','.html',NULL,0);
INSERT INTO `my_class` VALUES (7,1,'科幻·灵异','',0,0,'book/lingyi','.html',NULL,0);
INSERT INTO `my_class` VALUES (8,1,'美文·同人','',0,0,'book/meiwen','.html',NULL,0);
INSERT INTO `my_class` VALUES (9,1,'剧本·全文','',0,0,'book/juben','.html',NULL,0);
INSERT INTO `my_class` VALUES (10,2,'异世大陆','',1,0,'book/xuanhuan/yijie','.html',NULL,0);
INSERT INTO `my_class` VALUES (11,2,'西方奇幻','',1,0,'book/xuanhuan/xifang','.html',NULL,0);
INSERT INTO `my_class` VALUES (12,2,'东方玄幻','',1,0,'book/xuanhuan/dongfang','.html',NULL,0);
INSERT INTO `my_class` VALUES (13,2,'异术超能','',1,0,'book/xuanhuan/yishu','.html',NULL,0);
INSERT INTO `my_class` VALUES (14,2,'远古神话','',1,0,'book/xuanhuan/yuangu','.html',NULL,0);
INSERT INTO `my_class` VALUES (15,2,'魔法校园','',1,0,'book/xuanhuan/mofa','.html',NULL,0);
 
/*!40101 SET NAMES utf8 */;
 
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
 
复制代码




再发一个效果图上来
Snap1.jpg

评分

参与人数 1威望 +5 收起 理由
Hex + 5 原创内容

查看全部评分

 楼主| 发表于 2008-4-27 22:28:35 | 显示全部楼层
只做了列出的部分,修改和添加正在完善中,欢迎大家一起完善
发表于 2008-4-28 09:50:59 | 显示全部楼层
顶起~~~~~加分~~
发表于 2008-4-28 10:40:07 | 显示全部楼层
我以前也写过无限级分类,是用ajax,可以任意添加节点,删除节点
发表于 2008-5-5 08:42:24 | 显示全部楼层
不错哦,顶!
要是把输出的部分也整合到library里,样式分离,那就更好喽
发表于 2008-9-18 20:26:10 | 显示全部楼层
Fatal error: Class 'View' not found
发表于 2008-9-19 00:19:27 | 显示全部楼层
这是 Kohana!帖子里说明了[K]
发表于 2009-6-18 13:37:48 | 显示全部楼层
确实不错
发表于 2009-6-18 13:48:56 | 显示全部楼层
最好能整理成文件发上来, 让大家学习学习
发表于 2009-7-21 16:55:30 | 显示全部楼层
谢谢楼主分享

本版积分规则