YII的controller
public function actionIndex() {
$connection=Yii::app()->db;
$sql="SELECT * FROM category ";
$rows=$connection->createCommand ($sql)->queryAll();
$result = array();
$i=0;
foreach($rows as $v){
if($v['parent_id'] == '0'){
$result[++$i]['cat_name']=$v['cat_name'];
$result[$i]['parent_id'] = $v['parent_id'];
$result[$i]['cat_id'] = $v['cat_id'];
$result[$i]['blank']="";
$result[$i]['has_child'] = $this->hasChild($v['cat_id'],$rows);
$result[$i]['create_time'] = date('F j, Y',strtotime($v['create_time']));
$result[$i]['is_audit'] = $v['is_audit'];
$result[$i]['is_release'] = $v['is_release'];
$result[$i]['goods_number'] = $this->getGoodsNumber($v['cat_id']);
foreach($rows as $v1){
if($v1['parent_id'] == $v['cat_id']){
$result[++$i]['cat_name']=$v1['cat_name'];
$result[$i]['has_child'] = $this->hasChild($v1['cat_id'],$rows);
$result[$i]['blank']=" ";
$result[$i]['parent_id'] = $v1['parent_id'];
$result[$i]['cat_id'] = $v1['cat_id'];
$result[$i]['create_time'] = date('F j, Y',strtotime($v1['create_time']));
$result[$i]['is_audit'] = $v1['is_audit'];
$result[$i]['is_release'] = $v1['is_release'];
$result[$i]['goods_number'] = $this->getGoodsNumber($v1['cat_id']);
foreach($rows as $v2){
if($v2['parent_id'] == $v1['cat_id']){
$result[++$i]['cat_name']=$v2['cat_name'];
$result[$i]['has_child'] = $this->hasChild($v2['cat_id'],$rows);
$result[$i]['parent_id'] = $v2['parent_id'];
$result[$i]['cat_id'] = $v2['cat_id'];
$result[$i]['create_time'] = date('F j, Y',strtotime($v2['create_time']));
$result[$i]['is_audit'] = $v2['is_audit'];
$result[$i]['is_release'] = $v2['is_release'];
$result[$i]['blank']=' ';
$result[$i]['goods_number'] = $this->getGoodsNumber($v2['cat_id']);
foreach($rows as $v3){
if($v3['parent_id'] == $v2['cat_id']){
$result[++$i]['cat_name']=$v3['cat_name'];
$result[$i]['has_child'] = $this->hasChild($v3['cat_id'],$rows);
$result[$i]['parent_id'] = $v3['parent_id'];
$result[$i]['cat_id'] = $v3['cat_id'];
$result[$i]['create_time'] = date('F j, Y',strtotime($v3['create_time']));
$result[$i]['is_audit'] = $v3['is_audit'];
$result[$i]['is_release'] = $v3['is_release'];
$result[$i]['blank']=' ';
$result[$i]['goods_number'] = $this->getGoodsNumber($v3['cat_id']);
foreach($rows as $v4){
if($v4['parent_id'] == $v3['cat_id']){
$result[++$i]['cat_name']=$v4['cat_name'];
$result[$i]['has_child'] = $this->hasChild($v4['cat_id'],$rows);
$result[$i]['parent_id'] = $v4['parent_id'];
$result[$i]['cat_id'] = $v4['cat_id'];
$result[$i]['create_time'] = date('F j, Y',strtotime($v4['create_time']));
$result[$i]['is_audit'] = $v4['is_audit'];
$result[$i]['is_release'] = $v4['is_release'];
$result[$i]['blank']=' ';
$result[$i]['goods_number'] = $this->getGoodsNumber($v4['cat_id']);
}
}
}
}
}
}
}
}
}
}
$this->render ( '../goods/goodsclasses/index', array ('model' => $result,'categorys'=> Category::model()->getAllOrderCategory()) );
}
public function hasChild($cat_id,$model){
foreach($model as $v){
if($v['parent_id']==$cat_id){
return 1;
}
}
return 0;
}
public function getGoodsNumber($cat_id){
if(!isset($count)){
$count=0;
}
$connection=Yii::app()->db;
$sql="SELECT count(*) as count FROM goods where cat_id = '".$cat_id."'";
$rows=$connection->createCommand ($sql)->queryRow();
if($rows['count']==0){
$sql="SELECT cat_id FROM category where parent_id = '".$cat_id."'";
$row=$connection->createCommand ($sql)->queryAll();
if(!empty($row)){
foreach($row as $value){
if($value['cat_id']!=''){
$count += $this->getGoodsNumber($value['cat_id']);
}
}
}
}else{
$sql="SELECT cat_id FROM category where parent_id = '".$cat_id."'";
$row=$connection->createCommand ($sql)->queryAll();
if(empty($row)){
$count += $rows['count'];
}else{
foreach($row as $value){
if($value['cat_id']!=''){
$count += $this->getGoodsNumber($value['cat_id']);
}
}
$count += $rows['count'];
}
}
return $count ;
}
YII中的view
<div id="admin-grid" class="grid-view">
<div class="summary">Displaying 1-2 of 2 results.</div>
<table class="items">
<thead>
<tr>
<th width="33px" id="admin-grid_c0"><input type="checkbox" value="1"
id="select_all"></th>
<th>分类名称</th>
<th width="80">录入时间</th>
<th width="50">审核状态</th>
<th width="50">发布状态</th>
<th width="50">商品数量</th>
<th width="160">操作</th>
</tr>
</thead>
<tbody>
<?php
$flag = 0;
foreach ( $model as $v ) {
echo "<tr class=\"";
if ($flag ++ % 2 == 0) {
echo 'odd';
} else {
echo 'even';
}
echo "\"><td class=\"checkbox-column\"><input value=\"" . $v ['cat_id'] . "\" type=\"checkbox\" name=\"selectdel[]\" parent_id=\"" . $v ['parent_id'] . "\" has_child=\"" . $v ['has_child'] . "\" goods_number=\"" . $v ['goods_number'] . "\" cat_id=\"" . $v ['cat_id'] . "\"></td><td>" . $v ['blank'];
echo "<img src=\"".Yii::app ()->request->baseUrl."/images/minus.gif\" class=\"rowClicked\" action=\"minus\"> ";
echo $v ['cat_name'] . "</td><td>" . date('F,j,Y',strtotime($v ['create_time'])) . "</td><td>";
echo $v ['is_audit'] == 1 ? "通过" : "未通过";
echo "</td><td>";
echo $v ['is_release'] == 1 ? "发布" : "未发布";
echo "</td><td>" . $v ['goods_number'] . "</td><td class=\"checkbox-column\"><input οnclick=\"window.location.href='../goodsClasses/view/" . $v ['cat_id'] . "'\" type=\"button\" value=\"查看\"/><input οnclick=\"window.location.href='../goodsClasses/update/" . $v ['cat_id'] . "'\" type=\"button\" value=\"修改\"/> <input οnclick=\"return doDelete('" . $v ['cat_id'] . "','" . $v ['has_child'] . "','" . $v ['goods_number'] . "')\" type=\"button\" value=\"删除\"/>" . "</td></tr>";
}
?>
</tbody>
</table>
</div>
YII的js文件
$(document).ready(function(){
$('.rowClicked').click(function(){
$parent_id=$(this).parent().parent().find('input').attr('cat_id');
$all = $(this).parent().parent().parent().find('input[type=checkbox]');
if($(this).attr('action')=='minus'){
for($i = 0;$i < $all.length;$i++){
if($($all.get($i)).attr('parent_id')==$parent_id){
$($all.get($i)).parent().parent().hide();
}
}
$src=$(this).attr('src');
$src=$src.substr(0,$src.lastIndexOf('/'));
$(this).attr('src',$src+'/plus.gif');
$(this).attr('action','plus');
}else{
for($i = 0;$i < $all.length;$i++){
if($($all.get($i)).attr('parent_id')==$parent_id){
$($all.get($i)).parent().parent().show();
}
}
$src=$(this).attr('src');
$src=$src.substr(0,$src.lastIndexOf('/'));
$(this).attr('src',$src+'/minus.gif');
$(this).attr('action','minus');
}
})
})
效果图