<?php
function buy($ar)
{
	global $cache,$cachetime,$dpid,$dcid,$db,$config,$tpl;
	useCahe();
	$flag=md5(implode(",",$ar));
	$ar['temp']=empty($ar['temp'])?'buy_default':$ar['temp'];
	$tmpdir=$config['webroot']."/templates/".$config['temp']."/label/".$ar['temp'].".htm";
	if(file_exists($tmpdir))
		$tpl->template_dir = $config['webroot']."/templates/".$config['temp']."/label/";
	else
		$tpl->template_dir = $config['webroot']."/templates/default/label/";
	if(!$tpl->is_cached($ar['temp'].".htm",$flag))
	{
		$limit=$ar['limit'];	//条数
		$tj=$ar['rec'];			//是否推荐
		$catid=$ar['catid'];	//类别ＩＤ
		$startrecord=$ar['startrecord'];
		$buyid=$ar['buyid'];
		$userid=$ar['userid'];
			
		if(!empty($dpid))
			$sub_sql=" and province='$dpid' ";
		if(!empty($dcid))
			$sub_sql=" and city='$dcid' ";
		if($tj)
			$sub_sql.=" and statu='$tj'";
		else
			$sub_sql.=" and statu>=1 ";
		if($ud)
			$sub_sql.=" and userid=$ud ";
		if($catid)
			$sub_sql.=" and catid like '%$catid%' ";
		if($userid)
			$sub_sql.=" and userid='$userid'";
		if($buyid)
			$sub_sql=" and id in ($buyid) ";
		if($oder_by)
			$sub_sql.=' ORDER BY $oder_by desc';
		else
			$sub_sql.=' ORDER BY id desc';
			
		if(!empty($startrecord)&&is_numeric($startrecord))
			$sp=$startrecord;
		else
			$sp=0;
		$sql="SELECT * FROM ".BUY." WHERE 1 $sub_sql  limit $sp,$limit";
		$db->query($sql);
		$res=$db->getRows();
		foreach($res as $pl)
		{
			$pic = explode( ',',$pl['pic'] );
			$pl['img'] =$pic[0];
			$re[] = $pl;
		}
		//---------------------------------------------------------------------------
		$tpl->assign("config",$config);
		$tpl->assign("buy",$re);
	}
	return $tpl->fetch($ar['temp'].'.htm',$flag);
}
function buy_sitemap($ar)
{
	global $cache,$cachetime,$dpid,$dcid,$config,$db,$tpl;
	useCahe();
	
	$flag=md5(implode(",",$ar));

	$tmpdir=$config['webroot']."/templates/".$config['temp']."/label/".$ar['temp'].".htm";
	if(file_exists($tmpdir))
		$tpl->template_dir = $config['webroot']."/templates/".$config['temp']."/label/";
	else
		$tpl->template_dir = $config['webroot']."/templates/default/label/";
	$ar['temp']=empty($ar['temp'])?'sitemap':$ar['temp'];
	
	if(!$tpl->is_cached($ar['temp'].".htm",$flag))
	{    
		$limit=$ar['limit']?$ar['limit']:20;
        $sql="select catid,cat from ".OCAT." order by  catid desc";
		$db->query($sql);
		$re=$db->getRows();
		$de=array();
		foreach($re as $key=>$val)
		{
			$sql="SELECT  id,title  FROM  ".INFO." where catid='$val[catid]' order by  id  desc limit 0,10";
			$db->query($sql);
			$pro=$db->getRows();
			$num=count($pro);
			if($num>0)
			{   
			 	$de[$key]['catid']=$val['catid'];
				$de[$key]['cat']=$val['cat'];
				$de[$key]['num']=$num;
				$de[$key]['detail']=$pro;
			}
		}
		//==================================================
		$tpl->assign("urlcat","$config[weburl]/?m=product&s=product_list");
		$tpl->assign("urldetail","$config[weburl]/?m=product&s=product_detail");
		$tpl->assign("config",$config);
		$tpl->assign("sitemap",$de);
	}
	
	return $tpl->fetch($ar['temp'].'.htm',$flag);

}

?>