public function category() { $id = Q('get.id', 0, 'intval'); $article = Db::table('article')->where('cid', $id)->get(); $page = Page::row(5)->make(count($article)); $article = Db::table('article')->where('cid', $id)->limit(Page::limit())->get(); $field = Db::table('category')->where('id', $id)->first(); // p($field);exit; View::with('page', $page)->with('cms', $field)->with('article', $article); View::make($this->tpl . '/category.html'); }
protected function getGoods() { //读取当前路径参数条件下的所有商品 $args = explode('-', $_GET['s']); //拆解路径 $args = array_filter($args); //过滤没有筛选条件属性 $brand = Q('brand_id') ? ' and brand_id=' . Q('brand_id') : ''; //添加品牌筛选条件 //添加价格筛选条件 if (!Q('price_level')) { //价格等级为0时,不添加筛选条件 $price = ''; } else { $price_level = explode('-', Q('price_level')); if (sizeof($price_level) == 1) { $price = ' and goods_price>' . $price_level[0]; } else { $price = ' and goods_price between ' . $price_level[0] . ' and ' . $price_level[1]; } } //A:如果规格中有相同的属性值,可能会出现问题 //B:如果多个分组公用同一个类型时,可能会出现重复的情况 if ($args) { $v = "('" . implode("','", $args) . "')"; $sql = "select * from hd_goods_attr ga \n left join hd_goods g \n on ga.goods_id=g.goods_id \n where ga.goods_attr_value in " . $v . " \n and g.cate_id=" . Q('cate_id') . $brand . $price . " \n group by g.goods_id \n having count(*)=" . sizeof($args); $goods_count = sizeof(Db::select($sql)); $page = Page::make($goods_count); //分页 $sql = "select * from hd_goods_attr ga \n left join hd_goods g \n on ga.goods_id=g.goods_id \n where ga.goods_attr_value in " . $v . " \n and g.cate_id=" . Q('cate_id') . $brand . $price . " \n group by g.goods_id \n having count(*)=" . sizeof($args) . " \n limit " . Page::limit(); // echo $sql; $data = Db::select($sql); // //查询商品属性表,找出当前分类、满足筛选条件的记录 // $goods = Db::table('goods_attr')->whereIn('goods_attr_value',$args)->groupBy('goods_id')->having('count(*)','=',sizeof($args))->get(); // $page = Page::make(sizeof($goods)); //分页 // //根据分页限制获取商品 // $goods_limit = Db::table('goods_attr')->whereIn('goods_attr_value',$args)->groupBy('goods_id')->having('count(*)','=',sizeof($args))->limit(Page::limit())->get(); // foreach ($goods_limit as $k => $v) { // //根据查出的ID反查出商品信息 // $data[$k]= Db::table('goods')->where('goods_id',$v['goods_id'])->where('cate_id',$_GET['cate_id'])->first(); // } } else { $brand = Q('brand_id') ? ' and brand_id=' . Q('brand_id') : ''; //添加品牌筛选条件 $sql = "select * from hd_goods where cate_id=" . Q('cate_id') . $brand . $price; $goods_count = sizeof(Db::select($sql)); $page = Page::make($goods_count); //分页 $sql = "select * from hd_goods where cate_id=" . Q('cate_id') . $brand . $price . " limit " . Page::limit(); $data = Db::select($sql); // $goods_count = sizeof(Db::select($sql)); // $page = Page::row(2)->make($goods_count); //分页 // $sql = "select * from hd_goods_attr ga // left join hd_goods g // on ga.goods_id=g.goods_id // where ga.goods_attr_value in ".$v." // and g.cate_id=".Q('cate_id').$brand." // group by g.goods_id // having count(*)=".sizeof($args)." // limit ".Page::limit(); // echo $sql; // $data = Db::select($sql); // //查询商品找出当前分类的记录 // $goods = Db::table('goods')->where('cate_id',$_GET['cate_id'])->get(); // $page = Page::make(sizeof($goods)); //分页 // //根据分页限制获取商品 // $goods_limit = Db::table('goods')->where('cate_id',$_GET['cate_id'])->limit(Page::limit())->get(); // foreach ($goods_limit as $k => $v) { // //根据查出的ID反查出商品信息 // $data[$k]= Db::table('goods')->where('goods_id',$v['goods_id'])->first(); // } } $goodsData['page'] = $page; $goodsData['data'] = $data; return $goodsData; }
public function search() { $search_type = I('search_type'); $keywords = I('keyword'); $tid = I('tid'); switch ($search_type) { case 'title': $map['title'] = array('like', '%' . $keywords . '%'); $count = D('ArticleRelation')->relation(true)->where($map)->count(); import('Common/Org/Util/Page'); $page = new \Page($count, 2); $search = D('ArticleRelation')->relation(true)->where($map)->limit($page->limit())->select(); foreach ($search as $k => $v) { $search[$k]['keywords'] = explode(',', $v['keywords']); $search[$k]['title'] = str_replace($keywords, '<b style="color:red">' . $keywords . '</b>', $v['title']); } break; case 'key_words': $search = M('article')->select(); foreach ($search as $k => $v) { $v['keywords'] = explode(',', $v['keywords']); if (in_array($keywords, $v['keywords'])) { foreach ($v['keywords'] as $key => $value) { if ($value == $keywords) { $values[] = str_replace($value, '<span style="color:red">' . $value . '</span>', $value); } else { $values[] = $value; } } $v['keywords'] = $values; $search_keywords[] = $v; } } $search = $search_keywords; break; case 'tag': $sql = "SELECT aid FROM think_tag WHERE tagname='{$keywords}'"; $aid = M()->query($sql); foreach ($aid as $k => $v) { $aids[] = $v['aid']; } $map['id'] = array('in', $aids); $count = D('ArticleRelation')->relation(true)->where($map)->count(); import('Common/Org/Util/Page'); $page = new \Page($count, 2); $search = D('ArticleRelation')->relation(true)->where($map)->limit($page->limit())->select(); foreach ($search as $k => $v) { $values = array(); foreach ($v['tag'] as $key => $value) { if ($keywords) { $values[$key]['tagname'] = str_replace($keywords, '<b style="color:red">' . $value['tagname'] . '</b>', $value['tagname']); } else { $values[$key]['tagname'] = $value; } } $search[$k]['tag'] = $values; } break; } $this->pageshow = $page->show(); $this->keyword = $keywords; $this->assign('search', $search); $this->display('search'); }