PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/category.php

http://lzwblog.googlecode.com/
PHP | 415 lines | 225 code | 39 blank | 151 comment | 25 complexity | b3b55b7eb694e41a320d7c2aecd24c5c MD5 | raw file
  1. <?php
  2. require(dirname(__FILE__) . '/includes/init.php');
  3. //??id????
  4. if(isset($_GET['id']))
  5. {
  6. $cat_id=intval($_GET['id']);
  7. //echo $cat_id;
  8. }else{
  9. //????
  10. exit;
  11. }
  12. //??id
  13. //isset($_GET['brand'])
  14. if(isset($_GET['brand']))
  15. {
  16. $brand=intval($_GET['brand']);
  17. }else
  18. {
  19. $brand=0;
  20. }
  21. //price_min???
  22. if(isset($_GET['price_min']))
  23. {
  24. $price_min=intval($_GET['price_min']);
  25. }else
  26. {
  27. $price_min=0;
  28. }
  29. //price_max???
  30. if(isset($_GET['price_max']))
  31. {
  32. $price_max=intval($_GET['price_max']);
  33. }else
  34. {
  35. $price_max=0;
  36. }
  37. //??
  38. $page = isset($_GET['page']) && intval($_GET['page']) > 1 ? intval($_GET['page']) : 1 ;
  39. /*if(isset($_GET['page']))
  40. {
  41. $page=intval($_GET['page']);
  42. if($page<1){
  43. $page=1;
  44. }
  45. }else
  46. {
  47. $page=1;
  48. }
  49. echo $page;*/
  50. //??filter_attr
  51. /*if(isset($_GET['filter_attr']))
  52. {
  53. $filter_attr=$_GET['filter_attr'];
  54. //$filter_arprint_r($filter_ar);
  55. }else{
  56. $filter_attr='0.0.0.0';
  57. }
  58. $filter_ar=explode('.',$filter_attr);*/
  59. //???????????,???????????
  60. $cat_info=get_category_info($cat_id);
  61. if(empty($cat_info)){
  62. //????
  63. echo '??????';
  64. exit;
  65. }
  66. //???????id?string
  67. $str_cat_id=get_children($cat_id);
  68. $str_id=$str_cat_id.$cat_id;
  69. //??????
  70. public_data();
  71. //????(return array)
  72. $smarty->assign('current_position',current_p($cat_info['cat_id']));
  73. //????(???????)
  74. $smarty->assign('category',get_category($cat_info['parent_id'],'category'));
  75. //????
  76. $smarty->assign('best_goods',get_best_goods($str_id));
  77. //begin????
  78. $sql="select b.brand_id,b.brand_name from ".$ecs->table('goods')." as g, ".$ecs->table('brand').
  79. " as b where g.brand_id=b.brand_id and g.cat_id in({$str_id}) and ".
  80. "g.brand_id > 0 and g.is_delete = 0 AND g.is_on_sale = 1 AND g.is_alone_sale = 1 group by b.brand_name ";
  81. $brand_filter_arr=$db->getAll($sql);
  82. //print_r($brand_filter_arr);
  83. foreach($brand_filter_arr as $key=>$val)
  84. {
  85. $tem_key=$key+1;
  86. $brand_filter_arr[$tem_key]['brand_id']=$val['brand_id'];
  87. $brand_filter_arr[$tem_key]['brand_name']=$val['brand_name'];
  88. $brand_filter_arr[$tem_key]['b_url']='category.php?id='.$cat_id."&brand=".$val['brand_id']."&price_min=".$price_min."&price_max=".$price_max."&page=1";
  89. if($brand == $val['brand_id'])
  90. {
  91. $brand_filter_arr[$tem_key]['selected']=1;
  92. }else {
  93. $brand_filter_arr[$tem_key]['selected']=0;
  94. }
  95. }
  96. //print_r($brand_filter_arr);
  97. $brand_filter_arr[0]['brand_name']=$_LANG['all_attribute'];
  98. $brand_filter_arr[0]['b_url']='category.php?id='.$cat_id."&price_min=".$price_min."&price_max=".$price_max."&page=1";
  99. $brand_filter_arr[0]['selected'] = empty($brand) ? 1 : 0;
  100. $smarty->assign('brand_list',$brand_filter_arr);
  101. //end????
  102. //begin??????
  103. $filter_grade_arr=array();
  104. if($cat_info['grade']==0 && $cat_info['parent_id']!=0)
  105. {
  106. //???????????
  107. $cat_info['grade']=get_parent_grade($cat_info['cat_id']);
  108. }
  109. if($cat_info['grade']>1){
  110. //??????????
  111. $sql="select min(shop_price) as min,max(shop_price) as max from ".$ecs->table('goods')." as g ".
  112. " where cat_id in({$str_id}) AND is_delete = 0 AND is_on_sale = 1 AND is_alone_sale = 1 ";
  113. $filter_grade_arr=$db->getAll($sql);
  114. //print_r($grade_arr);
  115. if($filter_grade_arr[0]['max']!=$filter_grade_arr[0]['min'])
  116. {
  117. // ???????????????????????100???
  118. $price_grade = 0.0001;
  119. for($i=-2;$i<=log10($filter_grade_arr[0]['max']);$i++)
  120. {
  121. $price_grade*=10;
  122. }
  123. $dx = ceil(($filter_grade_arr[0]['max'] - $filter_grade_arr[0]['min'])/$price_grade/$cat_info['grade'])*$price_grade;
  124. $mi=floor($filter_grade_arr[0]['min']/$price_grade)*$price_grade;
  125. $ma=ceil($filter_grade_arr[0]['max']/$price_grade)*$price_grade;
  126. $mn=$mi;
  127. $i=1;
  128. while($mn<=$ma){
  129. $filter_grade_arr[$i]['min']=$mn;
  130. $mn=$mn+$dx;
  131. $filter_grade_arr[$i]['max']=$mn;
  132. $filter_grade_arr[$i]['price_range']=$filter_grade_arr[$i]['min']."&nbsp;-&nbsp;".$filter_grade_arr[$i]['max'];
  133. $filter_grade_arr[$i]['p_url']='category.php?id='.$cat_id."&brand=".$brand."&price_min=".$filter_grade_arr[$i]['min']."&price_max=".$filter_grade_arr[$i]['max']."&page=1";
  134. if($filter_grade_arr[$i]['min']==$price_min && $filter_grade_arr[$i]['max']==$price_max)
  135. {
  136. $filter_grade_arr[$i]['selected']=1;
  137. }else{
  138. $filter_grade_arr[$i]['selected']=0;
  139. }
  140. $i++;
  141. }
  142. $filter_grade_arr[0]['price_range'] = $_LANG['all_attribute'];
  143. $filter_grade_arr[0]['p_url'] = 'category.php?id='.$cat_id."&brand=".$brand."&price_min=0&price_max=0&page=1";
  144. $filter_grade_arr[0]['selected'] = (empty($price_min)&&empty($price_max)) ? 1 : 0 ;
  145. }else{
  146. $filter_grade_arr=array();
  147. }
  148. }
  149. //print_r($filter_grade_arr);
  150. $smarty->assign('filter_grade_arr',$filter_grade_arr);
  151. //????$row['filter_attr']
  152. $f_a_a=array();
  153. $f_a=array();
  154. /*if(!empty($cat_info['filter_attr']) && $cat_info['filter_attr']>0)
  155. {
  156. //$f_a_a=get_filter_attr($cat_info['filter_attr']);
  157. $sql="select g_a.goods_attr_id,g_a.attr_id,g_a.attr_value,a.attr_name from ".$GLOBALS['ecs']->table('goods_attr')." as g_a, " .$GLOBALS['ecs']->table('attribute'). " as a where g_a.attr_id=a.attr_id and g_a.attr_id in ({$cat_info['filter_attr']}) group by attr_value ";
  158. $re=$GLOBALS['db']->query($sql);
  159. while($row=$GLOBALS['db']->fetchRow($re))
  160. {
  161. $f_a_a[$row['attr_id']]['attr_name']=$row['attr_name'];
  162. //$row['t_url']='category.php?id='.$cat_id."&brand=".$brand."&price_min=0&price_max=0&filter_attr";
  163. $f_a_a[$row['attr_id']]['attr_chr'][]=$row;
  164. //print_r($f_a_a[$row['attr_id']]['attr_chr']);
  165. //foreach($f_a_a[$row['attr_id']]['attr_chr'] as $key=>$val){
  166. //$tem_key_q=$key+1;
  167. //$f_a[$tem_key_q]=$val;
  168. //print_r($val);
  169. //$f_a_a[$row['attr_id']]['attr_chr'][$key]['attr_value']=$val['attr_value'];
  170. //}
  171. //$f_a_a[$row['attr_id']]['attr_chr']=$f_a;
  172. //$f_a=array();
  173. //$f_a[0]['goods_attr_id']=0;
  174. //$f_a[0]['attr_id']=0;
  175. //$f_a[0]['attr_value']=$_LANG['all_attribute'];
  176. //$arr[$row['attr_id']]['attr_name']=$row['attr_name'];
  177. //$arr[$row['attr_id']]['attr_chr'][]=$row;
  178. }
  179. print_r($f_a_a);
  180. $i=0;
  181. foreach($f_a_a as $key=>$val){
  182. $f_a[$key]['attr_name']=$val['attr_name'];
  183. $f_a[$key]['attr_chr'][0]['goods_attr_id']=0;
  184. $f_a[$key]['attr_chr'][0]['attr_id']=0;
  185. $f_a[$key]['attr_chr'][0]['attr_value']=$_LANG['all_attribute'];
  186. if($filter_ar[$i]==0)
  187. {
  188. $f_a[$key]['attr_chr'][0]['selected']=1;
  189. }else{
  190. $f_a[$key]['attr_chr'][0]['selected']=0;
  191. }
  192. foreach($val['attr_chr'] as $key1=>$val1)
  193. {
  194. $tem_key_q=$key1+1;
  195. //echo $filter_ar[$i];
  196. echo $val1['goods_attr_id'];
  197. if($filter_ar[$i]==$val1['goods_attr_id'])
  198. {
  199. $val1['selected']=1;
  200. }else{
  201. $val1['selected']=0;
  202. }
  203. if($filter_ar[$i]==0)
  204. {
  205. $f_a[$key]['attr_chr'][0]['selected']=1;
  206. }else{
  207. $f_a[$key]['attr_chr'][0]['selected']=0;
  208. }
  209. $f_a[$key]['attr_chr'][$tem_key_q]=$val1;
  210. }
  211. $i++;
  212. //$tem_key_q=$key+1;
  213. //$f_a[$tem_key_q]=$val;
  214. //print_r($val);
  215. //$f_a_a[$row['attr_id']]['attr_chr'][$key]['attr_value']=$val['attr_value'];
  216. }
  217. }*/
  218. //print_r($f_a);
  219. $smarty->assign('filter_attr_list',$f_a);
  220. //begin????
  221. $where='';
  222. $limit='';
  223. $page_string='';
  224. if($brand>0){
  225. $where.=' and brand_id='.$brand;
  226. }
  227. if($price_min>0){
  228. $where.=' and shop_price > '.$price_min;
  229. }
  230. if($price_max>0){
  231. $where.=' and shop_price < '.$price_max;
  232. }
  233. //?????
  234. $sql="select count(*) from ".$ecs->table('goods')." where cat_id in({$str_id}) ".$where;
  235. $total_num=$db->getOne($sql);
  236. //echo $total_num;
  237. //??????
  238. $page_size=2;
  239. if($total_num>$page_size)
  240. {//begin??-------
  241. //???
  242. $page_total=ceil($total_num/$page_size);
  243. $page=$page>$page_total?$page_total:$page;
  244. $page_string="<span style=\"margin-right: 10px;\" class=\"f_l f6\">".$_LANG['pager_1']." <b>".$total_num."</b> ".$_LANG['pager_2']."</span>";
  245. $page_current="<span class=\"page_now\">".$page."</span>";
  246. $page_first='';
  247. $page_prev='';
  248. $page_p='';
  249. $page_last='';
  250. $page_next='';
  251. $page_l='';
  252. if($page>1){
  253. $page_first="<a href=\"category.php?id=".$cat_id."&brand=".$brand."&price_min=".$price_min.
  254. "&price_max=".$price_max."&page=1\">".$_LANG['page_first']."</a>";
  255. $page_prev= "<a href=\"category.php?id=".$cat_id."&brand=".$brand."&price_min=".$price_min.
  256. "&price_max=".$price_max."&page=".($page-1)."\">".$_LANG['page_prev']."</a>";
  257. }
  258. if($page<$page_total)
  259. {
  260. $page_last="<a href=\"category.php?id=".$cat_id."&brand=".$brand."&price_min=".$price_min.
  261. "&price_max=".$price_max."&page=".$page_total."\">".$_LANG['page_last']."</a>";
  262. $page_next="<a href=\"category.php?id=".$cat_id."&brand=".$brand."&price_min=".$price_min.
  263. "&price_max=".$price_max."&page=".($page+1)."\">".$_LANG['page_next']."</a>";
  264. }
  265. $i=$page-1;
  266. $j=1;
  267. //??
  268. $p_prev=3;
  269. for($i;$i>0;$i--){
  270. if($j>$p_prev){break;}
  271. $page_p="<a href=\"category.php?id=".$cat_id."&brand=".$brand."&price_min=".$price_min.
  272. "&price_max=".$price_max."&page=".$i."\">[".$i."]</a>".$page_p;
  273. $j++;
  274. }
  275. $i=$page+1;
  276. $j=1;
  277. //??
  278. $p_last=4;
  279. for($i;$i<=$page_total;$i++)
  280. {
  281. if($j>$p_last){break;}
  282. $page_l.="<a href=\"category.php?id=".$cat_id."&brand=".$brand."&price_min=".$price_min.
  283. "&price_max=".$price_max."&page=".$i."\">[".$i."]</a>";
  284. $j++;
  285. }
  286. $page_string.=$page_first.$page_prev.$page_p.$page_current.$page_l.$page_next.$page_last;
  287. //end ??------
  288. $page_start=($page-1)*$page_size;
  289. $limit=' limit '.$page_start.' ,'.$page_size;
  290. }
  291. $sql="select goods_id,cat_id,goods_name,market_price,shop_price,goods_thumb,brand_id from ".$ecs->table('goods')." where cat_id in({$str_id}) ".$where.$limit;
  292. //echo $sql;
  293. $re=$db->query($sql);
  294. $goods_list=array();
  295. while($row=$db->fetchRow($re))
  296. {
  297. $row['g_url']=headle_url('goods', $row['goods_id']);
  298. $row['short_title']=sub_str($row['goods_name'],10,false);
  299. $row['goods_thumb']=empty($row['goods_thumb'])?'images/200905/thumb_img/23_thumb_G_1241971556399.jpg':$row['goods_thumb'];
  300. $goods_list[]=$row;
  301. }
  302. //print_r($goods_list);
  303. //??
  304. $smarty->assign('page_string',$page_string);
  305. $smarty->assign('goods_list',$goods_list);
  306. //end????
  307. $smarty->display('category.html');
  308. //private function
  309. /**
  310. * ??????
  311. * @param string $str_id ???????id?
  312. * @return array
  313. */
  314. function get_best_goods($str_id='')
  315. {
  316. $arr=array();
  317. $where= " where is_best=1 ";
  318. $where .=' and cat_id in ('.$str_id.') order by last_update desc limit 0, 5';
  319. $sql="select goods_id,goods_name,shop_price,goods_img,cat_id from ".$GLOBALS['ecs']->table('goods').$where;
  320. $re=$GLOBALS['db']->query($sql);
  321. while($row=$GLOBALS['db']->fetchRow($re))
  322. {
  323. $row['g_url']=headle_url('goods', $row['goods_id']);
  324. $row['short_title']=sub_str($row['goods_name'],10,false);
  325. $row['shop_price']=$GLOBALS['_LANG']['a'].$row['shop_price'].$GLOBALS['_LANG']['b'];
  326. $arr[]=$row;
  327. }
  328. return $arr;
  329. }
  330. /**
  331. * ????
  332. * @param string ?????????
  333. * @return array
  334. */
  335. function get_filter_attr($str='')
  336. {
  337. $arr=array();
  338. $sql="select g_a.goods_attr_id,g_a.attr_id,g_a.attr_value,a.attr_name from ".$GLOBALS['ecs']->table('goods_attr')." as g_a, " .$GLOBALS['ecs']->table('attribute'). " as a where g_a.attr_id=a.attr_id and g_a.attr_id in ({$str}) group by attr_value ";
  339. $re=$GLOBALS['db']->query($sql);
  340. while($row=$GLOBALS['db']->fetchRow($re))
  341. {
  342. $arr[$row['attr_id']]['attr_name']=$row['attr_name'];
  343. $arr[$row['attr_id']]['attr_chr'][]=$row;
  344. }
  345. return $arr;
  346. }
  347. /**
  348. * ??????
  349. * @param int $str_id ???????id?
  350. * @param int $grade ??????
  351. * @return array
  352. */
  353. function get_filter_grade($str_id='',$grade=2)
  354. {
  355. $price_grade_arr=array();
  356. return $price_grade_arr;
  357. }
  358. /**
  359. * ?????????
  360. * @param int $parent_id //?????cat_id
  361. * @return int
  362. */
  363. function get_parent_grade($parent_id)
  364. {
  365. $grade_arr=array();
  366. $sql="select grade from ".$GLOBALS['ecs']->table('category')." where cat_id = ".$parent_id;
  367. $grade_arr=$GLOBALS['db']->getRow($sql);
  368. return $grade_arr['grade'];
  369. }
  370. ?>