产品分类之属性选择

关联程序:https://github.com/lin3615/attributes

结果关系图

表之间的关系
产品类型表(即产品类型表)表示,
每个产品类型都会有相关的属性(即在产品类型属性表),
每个产品类型属性会有多个属性值,即(产品类型属性值表).
每个产品分类(即产品分类表)都归属一个产品类型(即产品类型表),
从而产品分类就与属性相关系统联系起来,
而产品(即产品表)都归属于一个产品分类表,
而每个产品都有相关的产品属性(即产品属性表)

	// 产品列表
	function cateList()
	{
		// 分类id
		$cid = intval($_GET['id']);
		// 获取所拥有的属性
		$sql_1 = "select * from sz_class where c_id =" .$cid;
		
		$res_1 = self::$model->query($sql_1);
		if(!$res_1)
		{
			header("location:index.php");
		}
		// url
		$prefixUrl = 'index.php?a=Index&m=cateList&id='.$cid;
		$attrStr = $res_1[0]['c_attr'];
		

		// 属性下的产品id
		$attrProduct = array();

		// 产品结果
		$productResult = array();

		// 设置属性选择情况,0为全部为全部或没有选择
		$filterStatus = 1;
		if($attrStr)
		{
			//分解属性为数组
			$attrArr = explode(',',$attrStr);
			//获取筛选的属性值
			$filterStr= empty($_REQUEST['filter']) ? '' : $_REQUEST['filter'];
			$filterArr = explode('.', $filterStr);
			
			// 当没有选择属性时,或都全部为全部时
			if(empty($filterStr))
			{
				$filterStatus = 0;
			}else
			{
				foreach($filterArr as $filterValue)
				{
					// 不为0时
					if($filterValue != 0)
					{
						$filterStatus = 1;
						break;
					}else
					{
						$filterStatus = 0;
					}
				}
			}

			// 当为0时
			if(!$filterStatus)
			{
				// 分类下的产品,当属性全为全部时,即没有选择属性时
				$sqlCate = "select distinct p_id from sz_product where p_cid = " . $cid;
				$resCate = self::$model->query($sqlCate);

				if($resCate)
				{
					foreach($resCate as $v)
					{
						$attrProduct[] = $v['p_id'];
					}
				}
				if($attrProduct)
				{
					$ids = implode(',', $attrProduct);
					$sql = "select * from sz_product where p_id in (" .$ids.")";
					$productResult = self::$model->query($sql);
				}
			}else
			{
				// 相关产品列表
				$productResult = array();
				$pidStr = " p_id in (";
				foreach($filterArr as $attrId)
				{
					if(!$attrId) continue;
					$sql = "select distinct pa_pid from sz_product_attr where pa_value=".$attrId;
					$pidRes = self::$model->query($sql);
					// 产品id
					$productIds = '';
					foreach($pidRes as $vv)
					{
						
						$productIds .= $vv['pa_pid'] .',';
					}
					
					$pidStr .= substr($productIds,0,-1) . ") and p_id in (";
				}
				
				$productSQL = "SELECT * FROM SZ_PRODUCT WHERE " . substr($pidStr, 0, -14);
				$productResult = self::$model->query($productSQL);
				
			}

			// 保存最终的结果
			$list_res = array();
			foreach($attrArr as $key => $val)
			{
				// 保存当前选择的属性
				$tmp_arr_list = array();
				for($i=0; $i<count($attrArr); $i++)
				{
					$tmp_arr_list[$i] = empty($filterArr[$i]) ? 0 : $filterArr[$i];
				}
				// 判断是否已经选择
				if($tmp_arr_list[$key] == 0)
				{
					$list_res[$key]['list'][0]['selected'] = 1;
				}else
				{
					$list_res[$key]['list'][0]['selected'] = 0;
				}
				// 选择设置全部值
				$tmp_arr_list[$key] = 0;
				// 当前的属性值
				$str = implode('.',$tmp_arr_list);
				// 查询名称及全部的选择选项
				$sql_name = "select * from sz_attribute where aid = " . $val;
				$res_name = self::$model->query($sql_name);
				$list_res[$key]['filterName'] = $res_name[0]['aname'];
				$list_res[$key]['list'][0]['name'] = '全部';

				$list_res[$key]['list'][0]['attr'] = $str;
				// 查询全部以后的项目
				// 属性id
				$attrCateId = $res_name[0]['aid'];
				$sql_2 = "select * from sz_attr_value where av_attr_id = " . $attrCateId;
				$res = self::$model->query($sql_2);
				foreach($res as $k => $val)
				{
					for($i=0; $i<count($attrArr); $i++)
					{
						$tmp_arr_list[$i] = empty($filterArr[$i]) ? 0 : $filterArr[$i];
					}
					$kk = $k+1;
					$list_res[$key]['list'][$kk]['name'] = $val['av_value'];
					$list_res[$key]['list'][$kk]['selected'] = $val['av_id'] ==  $tmp_arr_list[$key]? 1 : 0;
					$tmp_arr_list[$key] = $val['av_id'];
					$list_res[$key]['list'][$kk]['attr'] = implode('.', $tmp_arr_list);
				}
				

			}

		}else
		{
			// 分类下的产品,当属性全为全部时,即没有选择属性时
			$sqlCate = "select distinct p_id from sz_product where p_cid = " . $cid;
			$resCate = self::$model->query($sqlCate);

			if($resCate)
			{
				foreach($resCate as $v)
				{
					$attrProduct[] = $v['p_id'];
				}
			}
			if($attrProduct)
			{
				$ids = implode(',', $attrProduct);
				$sql = "select * from sz_product where p_id in (" .$ids.")";
				$productResult = self::$model->query($sql);
			}
		
		}


	
		
		$view['attrName'] = $list_res;
		$view['url'] = $prefixUrl;
		$view['productResult'] = $productResult;
		$this->display('cateLists', $view); 
	}

 关联程序:https://github.com/lin3615/attributes 

原文地址:https://www.cnblogs.com/lin3615/p/5196226.html