///内容管理
public function content(){
if(IS_POST){
$cate_id = I('post.cate_id',0); //提交的类型id
$cate = M('category')->order('sort asc')->select();
$this->assign('cate',$cate);
////提交的属性id
$attr = M('cate_attr')->where('cate_id=' . $cate_id)->find();
$attr_id = explode(',', $attr['attr_id']);//dump($attr_id);
//查找字段名称 表标题名称
if($attr_id[0] != ''){ //二维数组 该属性下有字段分配
$where['id'] = array('in',$attr['attr_id']);
$attr_name = M('attribute')->where($where)->order('sort asc ,id asc')->getField('id,attr_name');
$this->assign('title',$attr_name);
// dump($attr_name);
//查找内容
$sign = M('content')->distinct(true)->where('cate_id='.$cate_id)->getField('sign',true);
// dump($sign);
$content = array();
$map['cate_id'] = $cate_id;
foreach($sign as $v){
$map['sign'] = $v;
foreach($attr_id as $id){
$map['attr_id'] = $id;
$attrname = M('attribute')->getFieldById($id,'attr_name');
for($i=0;$i<count($attr_id);$i++){ //每行数据关联属性名称 属性名称作为表的标题行
// $temp[$attrname] = M('content')->alias('a')->join('left join attribute b on a.attr_id = b.id')->field('a.*,b.attr_name')->order('b.sort asc ,b.id asc')->where('cate_id='.$cate[0]['id'])->where('attr_id='.$id)->where('sign='.$v)->fetchSql(true)->find();
$temp[$attrname] = M('content')->alias('a')->join('left join attribute b on a.attr_id = b.id')->field('a.id,a.sign,a.content,a.create_time,b.attr_name')->order('b.sort asc ,b.id asc')->where($map)->find();
if(count($temp[$attrname]) == 0){ //如果是空的 没有过记录
$temp[$attrname] = array('id'=>'','sign'=>'','cate_id'=>'','attr_id'=>'','content'=>'','create_time'=>'');
}
}
}
$content[] =$temp;
// $content[] = M('content')->alias('a')->join('left join attribute b on a.attr_id = b.id')->field('a.*,b.attr_name')->order('b.sort asc ,b.id asc')->where('cate_id='.$cate[0]['id'])->where('sign='.$v)->select();
}
// dump($content);
$this->assign('content',$content);
$this->assign('cate_id',$cate_id); //post回来的类型id传回去
$this->assign('count' , count($content));
}
}else{
$cate = M('category')->order('sort asc')->select();
$this->assign('cate',$cate);
////查找默认的类型的属性值 即第一个
$attr = M('cate_attr')->where('cate_id=' . $cate[0]['id'])->find();
$attr_id = explode(',', $attr['attr_id']);//dump($attr_id);
//dump($attr);
//查找字段名称 表标题名称
$where['id'] = array('in',$attr['attr_id']);
$attr_name = M('attribute')->where($where)->order('sort asc ,id asc')->getField('id,attr_name');
$this->assign('title',$attr_name);
// dump($attr_name);
//查找内容
$sign = M('content')->distinct(true)->where('cate_id='.$cate[0]['id'])->getField('sign',true);
// dump($sign);
$content = array();
$map['cate_id'] = $cate[0]['id'];
foreach($sign as $v){
$map['sign'] = $v;
foreach($attr_id as $id){
$map['attr_id'] = $id;
$attrname = M('attribute')->getFieldById($id,'attr_name');
for($i=0;$i<count($attr_id);$i++){ //每行数据关联属性名称 属性名称作为表的标题行
// $temp[$attrname] = M('content')->alias('a')->join('left join attribute b on a.attr_id = b.id')->field('a.*,b.attr_name')->order('b.sort asc ,b.id asc')->where('cate_id='.$cate[0]['id'])->where('attr_id='.$id)->where('sign='.$v)->fetchSql(true)->find();
$temp[$attrname] = M('content')->alias('a')->join('left join attribute b on a.attr_id = b.id')->field('a.id,a.sign,a.content,a.create_time,b.attr_name')->order('b.sort asc ,b.id asc')->where($map)->find();
if(count($temp[$attrname]) == 0){ //如果是空的 没有过记录
$temp[$attrname] = array('id'=>'','sign'=>'','cate_id'=>'','attr_id'=>'','content'=>'','create_time'=>'');
}
}
}
$content[] =$temp;
// $content[] = M('content')->alias('a')->join('left join attribute b on a.attr_id = b.id')->field('a.*,b.attr_name')->order('b.sort asc ,b.id asc')->where('cate_id='.$cate[0]['id'])->where('sign='.$v)->select();
}
$this->assign('content',$content);
$this->assign('cate_id',''); //post回来的类型id传回去
$this->assign('count' , count($content));
}
$this->display();
}数据库见截图有好的方法没 可以提供整站解决
QQ图片20170120165243.png
( 24.05 KB 下载:12 次 )
最佳答案