roveinlove 发表于 2008-4-27 22:26:39

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

控制器


<?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);
                        }
                }else{
                        $view -> title = '新增栏目';
                }
                $view -> render(TRUE);
        }
       
       
}





视图文件class_list.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
# 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 */;




再发一个效果图上来

roveinlove 发表于 2008-4-27 22:28:35

只做了列出的部分,修改和添加正在完善中,欢迎大家一起完善

Hex 发表于 2008-4-28 09:50:59

顶起~~~~~加分~~

gwpxjb 发表于 2008-4-28 10:40:07

我以前也写过无限级分类,是用ajax,可以任意添加节点,删除节点:lol

chouqiuqiu 发表于 2008-5-5 08:42:24

不错哦,顶!
要是把输出的部分也整合到library里,样式分离,那就更好喽

le_el 发表于 2008-9-18 20:26:10

Fatal error: Class 'View' not found :L

Hex 发表于 2008-9-19 00:19:27

这是 Kohana!帖子里说明了

Midnight 发表于 2009-6-18 13:37:48

确实不错

Midnight 发表于 2009-6-18 13:48:56

最好能整理成文件发上来, 让大家学习学习

chen_arou 发表于 2009-7-21 16:55:30

谢谢楼主分享
页: [1] 2
查看完整版本: [K]本人写的一个无限级分类,控制器加视图只查一次数据库