<?php
	require_once('_approot.php');
	
	$admininfo = $admin->getInfo();
	
	if ($_GET['ls_msg']==""){
		$ls_msg="";
	}else{
		$ls_msg=$_GET['ls_msg'];	
	}
	$ls=$_GET['ls'];
	$id=$_GET['id'];
	 
	$perpage=$_POST['perpage']!=""?$_POST['perpage']:$_SESSION['perpage'];
	$_SESSION['perpage']=$perpage;
	

	$nownum=$_GET['Num'];
	if($_GET['skipup1']>1){
		$skipup=$_GET['skipup1'];	
	}else{
		$skipup=$_GET['skipup'];	
	}//编号排序时用到的数字索引	升序
	
	if($_GET['skipdown1']>1){
		$skipdown=$_GET['skipdown1'];
	}else{
		$skipdown=$_GET['skipdown']	;
	}//编号排序时用到的数字索引	降序

	//print_r($_GET);
	$cateid=$_GET['cateid'];
	$catename=$_GET['catename'];
	$midcateid=$_GET['midcateid'];
	$midcatename=$_GET['midcatename'];
	$subcateid=$_GET['subcateid'];
	$subcatename=$_GET['subcatename'];
	$productID=$_GET['productID'];
	$searchproductName=trim(($_POST['searchproductName']==""?$_GET['searchproductName']:$_POST['searchproductName']));
	$searchrange=$_POST['searchrange']==""?$_GET['searchrange']:$_POST['searchrange']  ;
	//echo $searchproductName."---".$searchrange;
	//var_dump($HTTP_POST_VARS);
	//修正分类指示
	if(($cateid!="")&&($catename=="")){
		$getNameSql="select cateName from ls_products where ID=".$cateid;
		$getNameQuery=$opendb->query($getNameSql);
		$getNameRow=mysqli_fetch_assoc($getNameQuery);
		$catename=$getNameRow['cateName'];
	}
	if(($midcateid!="")&&($midcatename=="")){
		$getNameSql="select midcateName from ls_products where ID=".$midcateid;
		$getNameQuery=$opendb->query($getNameSql);
		$getNameRow=mysqli_fetch_assoc($getNameQuery);
		$midcatename=$getNameRow['midcateName'];
	}
	if(($subcateid!="")&&($subcatename=="")){
		$getNameSql="select subcateName from ls_products where ID=".$subcateid;
		$getNameQuery=$opendb->query($getNameSql);
		$getNameRow=mysqli_fetch_assoc($getNameQuery);
		$subcatename=$getNameRow['subcateName'];
	}
	//获取并定义排序用参数
	$sortKind=$_POST['SortKind'];
	$sortMethod=$_POST['SortMethod'];
	$sortIgnoreCate=($_POST['IgnoreCate']=="Ignore")?true:false;
	$sortCateOnly=($_POST['onlyGrandCate']=="OnlyCate")?true:false;
		
	if(isset($_GET['page'])&&$_GET['page']!=0){
	   $page = intval( $_GET['page'] );
	}else{
		$page = 1;
	}
	if($_POST['gopage']!=""){$page =(ctype_digit($_POST['gopage'])?$_POST['gopage']:1);}
		//更新的产品类别指示
		if($subcateid=="" && $midcateid=="" && $cateid==""){
			$ls_msg2="相关产品 >> 可添加 ";	
		}
		if(($_GET['catename']!="")||($catename!="")){
			$ls_msg2="<a href=\"".$_SERVER['PHP_SELF']."\">相关产品</a>  >> <a href=\"?cateid=".$cateid."&catename=".$catename."\">".(($_GET['catename']!="")?$_GET['catename']:$catename)."</a> >> 可添加 "	;
		}
		if(($_GET['midcatename']!="")||($midcatename!="")){
			$ls_msg2="<a href=\"".$_SERVER['PHP_SELF']."\">相关产品</a>  >> <a href=\"?cateid=".$cateid."&catename=".$catename."\">".(($_GET['catename']!="")?$_GET['catename']:$catename)."</a> >> <a href=\"?cateid=".$cateid."&catename=".$catename."&midcateid=".$midcateid."&midcatename=".$midcatename."\">".(($_GET['midcatename']!="")?$_GET['midcatename']:$midcatename)."</a> >> 可添加 "	;
		}

		if(($_GET['subcatename']!="")||($subcatename!="")){
			$ls_msg2="<a href=\"".$_SERVER['PHP_SELF']."\">相关产品</a>  >> <a href=\"?cateid=".$cateid."&catename=".$catename."\">".(($_GET['catename']!="")?$_GET['catename']:$catename)."</a> >> <a href=\"?cateid=".$cateid."&catename=".$catename."&midcateid=".$midcateid."&midcatename=".$midcatename."\">".(($_GET['midcatename']!="")?$_GET['midcatename']:$midcatename)."</a> >> <a href=\"?cateid=".$cateid."&catename=".$catename."&subcateid=".$subcateid."&subcatename=".$subcatename."\">".(($_GET['subcatename']!="")?$_GET['subcatename']:$subcatename)."</a> >> 可添加 "	;
		}
 
$Action=$_POST['Action'];
$relastr=",";
$relastrold="";
if($Action=="prorela"){
	$l=0;
	for($j=0;$j<$related_products_num*2; $j++){			
				$selectid=$_POST['selectidold'.$j];
				//echo $selectid."==";
				if ($selectid!=""){
					$relastrold.=$selectid.",";
					$l++;
				} 
	}
	$k=1;
	if($l<$related_products_num*2) //不超过 $related_products_num*2
	{
		for($i=0;$i<$PageNum; $i++){			
			$selectid=$_POST['selectid'.$i];
			//echo $selectid."--";
			if ($selectid!=""){
				$relastr.=$selectid.",";
				$k++;
				//echo $k."--";
			}
			if(($k+$l)>$related_products_num*2) { $ls_msg.= "相关产品已达到".($related_products_num*2)."个，多出的已舍弃";break;}
		}
	}
	else{
		$ls_msg.= "所选相关产品已达到".($related_products_num*2)."个，多出的已舍弃"; 
	}
	//echo "===".$k."--".$l."--".$relastr."==".$relastrold;
	 	$relastr.=$relastrold;
	
		//if(substr($relastr,-1)==",")$relastr=substr($relastr,0,(strlen($relastr)-1)) ; //去掉最后,
		$rela_sql="update ls_products set relatedpros ='".$relastr."' where ID=(".$id.")"; //相关产品
		//echo $rela_sql;
		$rela_query=$opendb->query($rela_sql);
}

if($id!=""){
	$rela_sql1="select * from ls_products where ID=".$id." "; //相关产品
	$rela_query1=$opendb->query($rela_sql1);
	$rela_row=mysqli_fetch_assoc($rela_query1);
	$rela=$rela_row['relatedpros'];
	$productName=$rela_row['productName'];
}
$ls_msg2= "还没人工选择，程序自动关联6个。最多选".($related_products_num*2)."个 &nbsp; &nbsp; &nbsp;<a href='products.php'>返回</a>";

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<title>无标题文档</title>
<script type="text/javascript" src="js/jquery.js"></script>
<script   language="JavaScript" src="js/ls.js" ></script>
<script   language="JavaScript" src="js/ls2.js" ></script>
<link href="css/admin.css" rel="stylesheet" type="text/css">
 <link rel="stylesheet" href="css/style2.css" />
 <style>
 button {
    padding:5px 15px;
    cursor: pointer;
    background-color: #00c0ef;
    border-color: #00acd6;
    border-radius: 5px;
    border: 1px;
    color: #fff;
}
 </style>
</head>
<body>
<form name="del" method="Post" action="relationselect.php?id=<?=$id?>" onSubmit="">
<table width="100%" border="0" align="center" cellpadding="1" cellspacing="1">
  <tr>
    <td height="18" colspan="2" style="border-bottom:2px solid #00c0ef; padding-bottom:5px;"><div align="center"><font color="#3c8dbc" style="font-size:16px;">
      <?=$productName?>
      相关产品</font></div>
	</td>
  </tr>
   <?php
	  //var_dump($rela);
	  if($rela!=""){
	  	$rela1=substr($rela,1,(strlen($rela)-2));
		$relaa=explode(",",$rela1);
		$ls_msg2= "已选择".count($relaa)."个相关产品，还可选".($related_products_num*2-count($relaa))."个 &nbsp; &nbsp; &nbsp;<a href='products.php'>返回</a>";
?> 
  <tr>
 	  <tr>
     <td height="21" valign="top" style='padding-left:20px;' ><?=$ls_msg?></td>
    <td height="21" align="right" valign="top" style='padding-right:20px;'><?=$ls_msg2?></td>
 </tr>
    <td height="213" colspan="2" valign="top">
      <?php
	  //var_dump($rela);
 

		for($j=0;$j<count($relaa);$j++)
		{
			//var_dump($relaa[$j]);
			$rela_sql1="select * from ls_products where ID=".$relaa[$j]." "; //相关产品
			$products_query=$opendb->query($rela_sql1);
			
	  ?>
      <table width="98%" border="0" align="center" cellpadding="3" cellspacing="1" style="background-color: #f0f0ee;">
        
        <?php
	
	 
	while($products_rows=mysqli_fetch_assoc($products_query)){
		
		 	//查询大类
		 	$search_cate_sql="select subcateID from ls_products where ID = ".$products_rows['productID']."";
		 	$search_cate_sql="select midcateID from ls_products where ID=(".$search_cate_sql.")";
			$search_cate_sql="select * from ls_products where ID=(".$search_cate_sql.")";
		 	$search_cate_query=$opendb->query($search_cate_sql);
		 	$search_cate_rows=mysqli_fetch_assoc($search_cate_query);
			
			//查询中类
		 	$search_midcate_sql="select subcateID from ls_products where ID = ".$products_rows['productID']."";
		 	$search_midcate_sql="select * from ls_products where ID=(".$search_midcate_sql.")";
		 	$search_midcate_query=$opendb->query($search_midcate_sql);
		 	$search_midcate_rows=mysqli_fetch_assoc($search_midcate_query);
		
		 	//查询小类
		 	$search_subcate_sql="select * from ls_products where ID = ".$products_rows['productID']."";
		 	$search_subcate_query=$opendb->query($search_subcate_sql);
		 	$search_subcate_rows=mysqli_fetch_assoc($search_subcate_query);
		 	
		 	//echo $search_subcate_sql."<br>";
		if(($search_cate_rows['enable']==1 || $search_cate_rows['enable']=="")&&($search_midcate_rows['enable']==1 || $search_midcate_rows['enable']=="")){
		if($search_subcate_rows['enable']==1){ ?>
        <tr class="preview" bgcolor='#f9f9f9'height='37' 
			onmouseover="
			this.style.backgroundColor='#e5e5e5';"
			 onmouseout=
			"this.style.backgroundColor='#f9f9f9'" 
			>
          <td width="3%" align='center'><?=$products_rows['ID']?></td>
          <td style='padding-left:10px;'><span style="padding-left:4px;">
            <?php 				$picsrc= $products_rows['wholepathpic']; //  21-7-11 换后台，新传图片优先使用，新图为空，使用老图
				if($picsrc!=NULL){
			?>
            <img src='<?=$_SESSION['dda']?>../<?=$picsrc?>?i=<?=rand()?>' alt="" width="<?=($SmallPicW/3)?>" height="<?=($SmallPicH/3)?>" border='0' />
            <?php }else{ ?>
            <img src='<?=$_SESSION['dda']?>../<?=getproductfolder($products_rows['productID'])?>/<?=$SmallFoldersProducts?>/<?=$products_rows['productPic']?>?i=<?=rand()?>' alt="" width="<?=($SmallPicW/3)?>" height="<?=($SmallPicH/3)?>" border='0' />
            <?php } ?>
            </span></td>
          <td width="7%" align="center" style='padding-left:10px;'><input name='selectidold<?=$j?>' type='checkbox' onclick="unselectall()" id="select_oldid<?=$j?>" value='<?=$products_rows['ID']?>'  checked  /></td>
          <td width="17%" align="left"><span style="padding-left:10px;">
            <?=$products_rows['productName']?>
            </span></td>
          <td width="19%"><a class='proTil' href='javascript:void(0);' title='<?=$products_rows['productTitle']?>' >
            <?=$products_rows['productTitle']?>
            </a></td>
          <td width="26%"><a class='proDesc' href='javascript:void(0);' title='<?=strip_tags($products_rows['productDescription'])?>' >
            <?=strip_tags($products_rows['productDescription'])?>
            </a></td>
          <td width="7%" align='center' style='padding-left:4px;'><?=$products_rows['hits']?></td>
         
        </tr>
        <?php $productID2=$products_rows['productID'];
		 }
	 }
	
	} ?>
      </table>
      <?php
	
			} //for
	  	} // if($rela!=""){
		else
		{ ?>
	  <tr>
     <td height="21" valign="top" style='padding-left:20px;' ><?=$ls_msg?></td>
    <td height="21" align="right" valign="top" style='padding-right:6px;'><?=$ls_msg2?></td>
 </tr>
		
		<?php }
		?>
      <br /> 
  <tr bgcolor="">
          <td colspan="4" align="right" style="padding-left:6px;"><button name="submitdel" type='submit' value='确定'>确定</button>
            <input name="Action" type="hidden" id="Action" value="prorela" /></td>
  </tr>
      <tr>
        <td height="18" colspan="2" style="border-bottom:1px solid #00c0ef; padding-bottom:5px;"><div align="center"><font color="#3c8dbc" style="font-size:16px;">
          
          还可添加</font></div>
        </td>
  </tr>        
        <br />
      <?php
		//大类//////////////////////////////////////////////////////////////////////////
		$cate_sql="select * from ls_products where midcateName is null and midcateID is null and subcateName is null and subcateFolders is null and subcateBrief is null and subcateID is null and productPic is null and productItemNo is null and productName is null and productSpecification is null and productID is null  and enable=1 ".$sortstr;
		$cate_query=$opendb->query($cate_sql);
		$cate_num=mysqli_num_rows($cate_query); 
		if($cate_num!=0)
		{
			$i=1;
			?>
      <table width="98%" border="0" align="center" cellpadding="3" cellspacing="1" >
        <tr>
          <td>
            <?php
			while($cate_rows=mysqli_fetch_assoc($cate_query))
			 {
if($i==1)
{  
    ?> 大类: <?php
	echo "&nbsp;<a href=\"?id=".$id."&cateid=".$cate_rows['ID']."&catename=".$cate_rows['cateName']."\" >";
	if($cateid==$cate_rows['ID'])
	{
		echo "<font color=\"#FF0000\"><b>".$cate_rows['cateName']."</b></font>";
	}
	else
	{
	    
		echo $cate_rows['cateName'];
	}
	echo "</a> ";
			 	}
else
{
	echo "| <a href=\"?id=".$id."&cateid=".$cate_rows['ID']."&catename=".$cate_rows['cateName']."\" >";
	if($cateid==$cate_rows['ID'])
	{
		echo "<font color=\"#FF0000\"><b>".$cate_rows['cateName']."</b></font>";
	}
	else
	{
		echo $cate_rows['cateName'];
	}
	echo "</a> ";
	}
	$i++;
	}
?>
            
            | <a href="?id=<?=$id?>">全部</a>
            
          </td>
        </tr>
  </table>
  <?php
		
		//中类/////////////////////////////////////////////////////////////////////////
		if($cateid!="")
		{
			$midcate_sql="select * from ls_products where cateName is null and cateFolders is null and subcateName is null and subcateID is null and cateBrief is null and productPic is null and productItemNo is null and productName is null and productSpecification is null and productID is null and enable=1 and midcateID=".$cateid." order by Num asc";
			$midcate_query=$opendb->query($midcate_sql);
			$midcate_query2=$opendb->query($midcate_sql);
			$midcate_num=mysqli_num_rows($midcate_query);
			$midcate_id = mysqli_fetch_assoc($midcate_query2);
			if($midcateid == '')
				$midcateid = $midcate_id['ID'];
			if($midcate_num!=0)
			{
			$i=1;
			?>
      <table width="98%" border="0" align="center" cellpadding="3" cellspacing="1">
        <tr>
          <td>
            <?php
		while($midcate_rows=mysqli_fetch_assoc($midcate_query))
		 {
		if($i==1)
		{  
		?>   中类 : 
            <?php
		echo "&nbsp;<a href=\"?id=".$id."&midcateid=".$midcate_rows['ID']."&cateid=".$cateid."&catename=".$_GET['catename']."&midcatename=".$midcate_rows['midcateName']."\">";
		$j=0;
		$firstmidcateid=$midcate_rows['ID'];
		if($midcateid==$midcate_rows['ID'])
		{
		echo "<font color=\"#880812\"><b>".$midcate_rows['midcateName']."</b></font>";
		$j++;
	}
	else
	{
		echo $midcate_rows['midcateName'];
	}
			
	echo "</a>";
}
else
{
	echo " | <a href=\"?id=".$id."&midcateid=".$midcate_rows['ID']."&cateid=".$cateid."&catename=".$_GET['catename']."&midcatename=".$midcate_rows['midcateName']."\">";
	if($midcateid==$midcate_rows['ID'])
	{
		echo "<font color=\"#880812\"><b>".$midcate_rows['midcateName']."</b></font>";
		$j++;
	}
	else
	{
		echo $midcate_rows['midcateName'];
	}
	echo "</a>";
}
$i++;
			 }
			 if($j==0)
			{
			$midcateid=$firstmidcateid;
			}
			 ?>
          </td>
        </tr>
  </table>
      <?php
		}
	}
		
			//小类///////////////////////////////////////////////////////////////////////////////
		//echo $cateid."--".$midcateid."--".$subcateid;	
		if($cateid!="" && $midcateid!="")
		{
				$subcate_sql="select * from ls_products where cateName is null and cateFolders is null  and cateBrief is null and midcateName is null and midcateID is null and productPic is null and productItemNo is null and productName is null and productSpecification is null and productID is null and enable=1 and subcateID=".$midcateid." order by Num asc";
				$subcate_query=$opendb->query($subcate_sql);
				$subcate_query2=$opendb->query($subcate_sql);
				$subcate_num=mysqli_num_rows($subcate_query); 
				$subcate_id=mysqli_fetch_assoc($subcate_query2);
				if($subcateid =='')
					$subcateid = $subcate_id['ID'];
				if($subcate_num!=0)
				{
					$i=1;
				?>
      <table width="98%" border="0" align="center" cellpadding="3" cellspacing="1">
        <tr>
          <td>
            <?php
					while($subcate_rows=mysqli_fetch_assoc($subcate_query))
					 {
						if($i==1)
						{  
						?>   小类 : 
            <?php
							echo "&nbsp;<a href=\"?id=".$id."&subcateid=".$subcate_rows['ID']."&cateid=".$cateid."&catename=".$catename."&midcatename=".$midcatename."&midcateid=".$midcateid."&subcatename=".$subcate_rows['subcateName']."\">";
							$k=0;
							$firstsubcatetid=$subcate_rows['ID'];
							if($subcateid==$subcate_rows['ID'])
							{
								echo "<font color=\"#880812\"><b>".$subcate_rows['subcateName']."</b></font>";
								$k++;
							}
							else
							{
								echo $subcate_rows['subcateName'];
							}
							echo "</a>";
						}
						else
						{
							echo " | <a href=\"?id=".$id."&subcateid=".$subcate_rows['ID']."&cateid=".$cateid."&catename=".$catename."&midcatename=".$midcatename."&midcateid=".$midcateid."&subcatename=".$subcate_rows['subcateName']."\">";
							if($subcateid==$subcate_rows['ID'])
							{
								echo "<font color=\"#880812\"><b>".$subcate_rows['subcateName']."</b></font>";
								$k++;
							}
							else
							{
								echo $subcate_rows['subcateName'];
							}
							echo "</a>";
						}
						$i++;
					 }
					 if($k==0)
					 {
					 	$subcatetid=$firstsubcatetid;
					 }
					?>				 			  <a href="addrelationselect.php" target="right"></a></td>
        </tr>
  </table>
      <?php
				}
		}
		
			
?>
      <?php 
	// 每页数量
	$PageSize =  $perpage==0?$PageNum:$perpage;
	
	// 获取总数据量
	/********************************************************************************/
		$sql="select * from ls_products where midcateName is null and cateName is null and cateFolders is null  and cateBrief is null and subcateName is null and subcateFolders is null and subcateBrief is null and subcateID is null ";
	if($admininfo['branchID']==0){
			
		}else{
			$sql .= " and branchID=".$admininfo['branchID']." ";
		}
	//大类
	$sqlID="";
	$displaydownup="";//显示上移下移箭头
	if($cateid!=""  && $midcateid=="" && $subcateid=="" && $searchproductName=="")
	{
		//大类下有中类
		$sqlstr="select ID from ls_products where midcateID =".$cateid." ";
		$query1=$opendb->query($sqlstr);
		$mynum=mysqli_num_rows($query1);
		if($mynum!=0)
		{
			$i=1;
			while($myrows=mysqli_fetch_assoc($query1))			
			{
				$sqlstr2="select ID from ls_products where subcateID =".$myrows['midcateID']." ";
				$query2=$opendb->query($sqlstr2);
				$mynum2=mysqli_num_rows($query2);
				
if($mynum2!=0) //中类下有小类
{
	while($myrows2=mysqli_fetch_assoc($query2))			
	{
		if($i==1)
		{
			$sqlID.="productID=".$myrows2['ID'];
		}
		else
		{
			$sqlID.=" or productID=".$myrows2['ID'];
		}
		$i++;
	}
	
}
else//中类下没小类
{
	if($i==1)
	{
		$sqlID.="productID=".$myrows['midcateID'];
	}
	else
	{
		$sqlID.=" or productID=".$myrows['midcateID'];
	}
	$i++;
}
			
			}
			
		//	echo $productID."<br>";
		$sql.="and （".$sqlID."）";
	  }
	  else
	  {//大类下没有中类
	  	$displaydownup="ok";
	  	$sql.="and productID=".$cateid;
	  }
	}
	
	//中类
	
	if($midcateid!="" && $subcateid=="" && $searchproductName=="")
	{
		//中类下有小类
		$sqlstr="select ID from ls_products where subcateID =".$midcateid." ";
		$query1=$opendb->query($sqlstr);
		$mynum=mysqli_num_rows($query1);
		if($mynum!=0)
		{
			$i=1;
			while($myrows=mysqli_fetch_assoc($query1))
			{
				if($i==1)
				{
					$sqlID.="productID=".$myrows['ID'];
				}
				else
				{
					$sqlID.=" or productID=".$myrows['ID'];
				}
				$i++;
			}
		//	echo $productID."<br>";
		$sql.="and (".$sqlID.")";
	  } else {//中类下没有小类
		  	$sql.="and productID=".$midcateid;
  	  		$displaydownup="ok";
	  }
	}
	
	//小类
	if($subcateid!="" && $searchproductName==""){
		$sql.="and productID=".$subcateid;
		$displaydownup="ok";
	}
		
	$products_sql=$sql; // for products list 查询数据库
		
	//查询数据
	if($searchproductName!=""){
		$ls_msg2="相关产品 >> 搜索：".$searchproductName;			
		switch($searchrange){
			case 'productname':
				$sql.="and productName like '%".$searchproductName."%'";
				$searchsql ="and productName like '%".$searchproductName."%'";
				break;
			case 'productno':
				$sql.="and productItemNo like '%".$searchproductName."%'";
				$searchsql ="and productItemNo like '%".$searchproductName."%'";
				break;
			case 'both':
				$sql.="and (productItemNo like '%".$searchproductName."%' or productName like '%".$searchproductName."%' or ID like '%".$searchproductName."%' or productSpecification like '%".$searchproductName."%' or productDescription  like '%".$searchproductName."%' or canshu  like '%".$searchproductName."%' or basicInfo like '%".$searchproductName."%' or additionalInfo  like '%".$searchproductName."%' or productKeywords  like '%".$searchproductName."%' or productTitle  like '%".$searchproductName."%' or parameters  like '%".$searchproductName."%' or advantages  like '%".$searchproductName."%' or characteristics  like '%".$searchproductName."%' or date  like '%".$searchproductName."%' or productVideo  like '%".$searchproductName."%' or wholepath  like '%".$searchproductName."%' or filename like '%".$searchproductName."%'  )";
				$searchsql ="and (productItemNo like '%".$searchproductName."%' or productName like '%".$searchproductName."%'  or ID like '%".$searchproductName."%' or productSpecification like '%".$searchproductName."%' or productDescription  like '%".$searchproductName."%' or canshu  like '%".$searchproductName."%' or basicInfo like '%".$searchproductName."%' or additionalInfo  like '%".$searchproductName."%' or productKeywords  like '%".$searchproductName."%' or productTitle  like '%".$searchproductName."%' or parameters  like '%".$searchproductName."%' or advantages  like '%".$searchproductName."%' or characteristics  like '%".$searchproductName."%' or date  like '%".$searchproductName."%' or productVideo  like '%".$searchproductName."%' or wholepath  like '%".$searchproductName."%' or filename like '%".$searchproductName."%')";
				break;
			default:
				$sql.="and productName like '%".$searchproductName."%'";
				$searchsql ="and productName like '%".$searchproductName."%'";
		}
	}
	
	
	$sql.=$sortstr;	
	 

		$result=$opendb->query($sql);
	
	/********************************************************************************/
	// 获取总数据量
	
	$row = mysqli_num_rows($result);
	$amount = $row;
	
	// 记算总共有多少页
	if( $amount ){
	   if( $amount < $PageSize ){ $page_count = 1; }               //如果总数据量小于$PageSize，那么只有一页
	   if( $amount % $PageSize ){                                  //取总数据量除以每页数的余数
	       $page_count = (int)($amount / $PageSize) + 1;           //如果有余数，则页数等于总数据量除以每页数的结果取整再加一
	   }else{
	       $page_count = $amount / $PageSize;                      //如果没有余数，则页数等于总数据量除以每页数的结果
	   }
	}
	else{
	   $page_count = 0;
	}
	
	// 翻页链接
	$page_string = '';
	$pagecatestr="subcateid=".$subcateid."&midcateid=".$midcateid."&cateid=".$cateid."&catename=".urlencode($catename)."&midcatename=".urlencode($midcatename)."&subcatename=".urlencode($subcatename)."&searchproductName=".urlencode($searchproductName)."&sortpro=".$sortpro; 
	if( $page == 1 ){
	   $page_string .= '首页 | 上一页 |';
	}
	else{
	   $page_string .= '<a href=?id='.$id.'id='.$id.'&page=1&'.$pagecatestr.'><u><font color=\"#0000FF\">首页</font></u></a> | <a href=?id='.$id.'&page='.($page-1)."&".$pagecatestr.'><u><font color=\"#0000FF\">上一页</font></u></a> | ';
	}
	if( ($page == $page_count) || ($page_count == 0) ){
	   $page_string .= ' 下一页 | 尾页';
	}
	else{
	   $page_string .= ' <a href=?id='.$id.'&page='.($page+1).'&'.$pagecatestr.'><u><font color=\"#0000FF\">下一页</font></u></a> | <a href=?id='.$id.'&page='.$page_count.'&'.$pagecatestr.'><u><font color=\"#0000FF\">尾页</font></u></a>';
	}
		 $page_string .= " &nbsp;&nbsp;页次：<font color='#FF0000'><b>".$page."</b></font>/<b>".$page_count."</b>页";
		 $page_string .= " &nbsp;&nbsp;<b>".$PageSize."</b>个产品每页";
		 $page_string .= "&nbsp; &nbsp;共 <b style='color:red;'>".$row."</b> 个产品";
	
	
	?>
      <br>
  <table border="0" width="98%" align="center">
    <tr>
      <td width="79%">&nbsp;</td>
      <td width="21%" align="right">共 <b style='color:red;'><?=$row?></b> 个产品 &nbsp; &nbsp;&nbsp; &nbsp; <?php if(!(strpos($_SERVER['HTTP_REFERER'],"managearticle")===false)){ ?> <a href="managearticle.php">返回</a><?php } ?></td>
    </tr>
  </table>
  <?php
		 
		/*\***************************************************************************\*/
		/*\查询数据库	\*/
		/*\***************************************************************************\*/ 
//	$sort='Num';
	//查询数据
	if($searchproductName!=""){$products_sql.= $searchsql;}
	$products_sql.=$sortstr." limit ". ($page-1)*$PageSize .",".$PageSize." ";	
	
	//echo $products_sql."<br>";
	$products_query=$opendb->query($products_sql);
		/*\***************************************************************************\*/
		/*\查询数据库	\*/
		/*\***************************************************************************\*/ 			
?>
      <table width="98%" border="0" align="center" cellpadding="3" cellspacing="1" style="background-color: ">
        <tr height="37">
          <td align="center" width="3%">序号</td>
          <td align="center" width="7%">货号/标志<br />
            <a href='relationselect.php?id=<?=$products_rows['ID']?>&page=<?=$page?>&cateid=<?=$cateid?>&midcateid=<?=$midcateid?>&subcateid=<?=$subcateid?>&pp=<?=$i?>&searchproductName=<?=urlencode($searchproductName)?>&searchrange=<?=$searchrange?>&sortpro=<?=$sortpro==""?"H":""?>' title='首页热点'>H</a>
            <a href='relationselect.php?id=<?=$products_rows['ID']?>&page=<?=$page?>&cateid=<?=$cateid?>&midcateid=<?=$midcateid?>&subcateid=<?=$subcateid?>&pp=<?=$i?>&searchproductName=<?=urlencode($searchproductName)?>&searchrange=<?=$searchrange?>&sortpro=<?=$sortpro==""?"C":""?>' title='类别代表图'>C</a>
            <a href='relationselect.php?id=<?=$products_rows['ID']?>&page=<?=$page?>&cateid=<?=$cateid?>&midcateid=<?=$midcateid?>&subcateid=<?=$subcateid?>&pp=<?=$i?>&searchproductName=<?=urlencode($searchproductName)?>&searchrange=<?=$searchrange?>&sortpro=<?=$sortpro==""?"P":""?>' title='推荐产品'>P</a>
            <a href='relationselect.php?id=<?=$products_rows['ID']?>&page=<?=$page?>&cateid=<?=$cateid?>&midcateid=<?=$midcateid?>&subcateid=<?=$subcateid?>&pp=<?=$i?>&searchproductName=<?=urlencode($searchproductName)?>&searchrange=<?=$searchrange?>&sortpro=<?=$sortpro==""?"N":""?>' title='新产品'>N</a></td>
          <td align="center"  width="14%">图片</td>
          <td align="center"  width="7%">相关</td>
          <td align="center"  width="17%">产品名称</td>
          <td align="center"  width="19%">Title</td>
          <td width="26%" align="center">Description</td>
          <td width="7%" align="center">点击</td>
          <!--<td align="center" colspan="2">排序</td>-->
        </tr>
  <?php
	
	$i=1;
	while($products_rows=mysqli_fetch_assoc($products_query)){
		if($products_rows['ID']==$id) continue; //自己跳过
		 	//查询大类
		 	$search_cate_sql="select subcateID from ls_products where ID = ".$products_rows['productID']."";
		 	$search_cate_sql="select midcateID from ls_products where ID=(".$search_cate_sql.")";
			$search_cate_sql="select * from ls_products where ID=(".$search_cate_sql.")";
		 	$search_cate_query=$opendb->query($search_cate_sql);
		 	$search_cate_rows=mysqli_fetch_assoc($search_cate_query);
			
			//查询中类
		 	$search_midcate_sql="select subcateID from ls_products where ID = ".$products_rows['productID']."";
		 	$search_midcate_sql="select * from ls_products where ID=(".$search_midcate_sql.")";
		 	$search_midcate_query=$opendb->query($search_midcate_sql);
		 	$search_midcate_rows=mysqli_fetch_assoc($search_midcate_query);
		
		 	//查询小类
		 	$search_subcate_sql="select * from ls_products where ID = ".$products_rows['productID']."";
		 	$search_subcate_query=$opendb->query($search_subcate_sql);
		 	$search_subcate_rows=mysqli_fetch_assoc($search_subcate_query);
		 	
		 	//echo $search_subcate_sql."<br>";
		if(($search_cate_rows['enable']==1 || $search_cate_rows['enable']=="")&&($search_midcate_rows['enable']==1 || $search_midcate_rows['enable']=="")){
		if($search_subcate_rows['enable']==1){ ?>
        
        <tr class="preview" bgcolor='#f9f9f9'height='37' 
			onMouseOver="
			this.style.backgroundColor='#e5e5e5';"
			 onMouseOut=
			this.style.backgroundColor='#f9f9f9' 
			>
          <td align='center'><?=$products_rows['ID']?></td>
          <td style='padding-left:10px;'>
            <?php echo $products_rows['productItemNo']; ?>
            
            <br/><span style='color:red'>
              <?=$products_rows['isbranch']!=0?"M ":"" ?>
              <?=intval($products_rows['AdditionalAttributes'])&1? "<span title='首页热点'>H</span> ":"" ?>
              <?=$ls_classproType == "selectPic" && intval($products_rows['AdditionalAttributesClassPro'])&1?"<span title='类别代表图'>C</span> ":"" ?> 
              <?=intval($products_rows['AdditionalAttributes'])&2?"<span title='推荐产品'>P</span> ":"" ?>
              <?=intval($products_rows['AdditionalAttributes'])&4?"<span title='新产品'>N</span> ":"" ?>
              
              </span>
            
          </td>
          <td align="center" style='padding-left:10px;'><span style="padding-left:4px;">
            <?php 				$picsrc= $products_rows['wholepathpic']; //  21-7-11 换后台，新传图片优先使用，新图为空，使用老图
				if($picsrc!=NULL){
			?>
            <img src='<?=$_SESSION['dda']?>../<?=$picsrc?>?i=<?=rand()?>' width=<?=($SmallPicW/3)?> height=<?=($SmallPicH/3)?> border='0' />
            <?php }else{ ?>
            <img src='<?=$_SESSION['dda']?>../<?=getproductfolder($products_rows['productID'])?>/<?=$SmallFoldersProducts?>/<?=$products_rows['productPic']?>?i=<?=rand()?>' width=<?=($SmallPicW/3)?> height=<?=($SmallPicH/3)?> border='0' />
            <?php } ?>
            </span></td>
          <td align="center" style='padding-left:10px;'><?php if(!(strpos($rela,",".$products_rows['ID'].",")===false)) {echo "已相关"; $relafont="<font color='#717679'>"; } else { 
			 
			$relafont="<font>";
			?>
            <p>
              <input name='selectid<?=$i?>' type='checkbox' onclick="unselectall()" id="select_id" value='<?=$products_rows['ID']?>' <?=strpos($rel,",".$products_rows['ID'].",")===fasle?"checked":""?> />
            </p>
            <?php } ?>
          </td>
          <td align="left"><span style="padding-left:10px;">
            <?=$relafont?> <?=$products_rows['productName']?></font>
            </span></td>
          <td><a class='proTil' href='javascript:void(0);' title='<?=$products_rows['productTitle']?>' >
            <?=$relafont?><?=$products_rows['productTitle']?></font>
            </a></td>
          <td><a class='proDesc' href='javascript:void(0);' title='<?=strip_tags($products_rows['productDescription'])?>' >
            <?=$relafont?> <?=strip_tags($products_rows['productDescription'])?></font>
            </a></td>
          <td align='center' style='padding-left:4px;'><?=$products_rows['hits']?>			  </td>
          <!--<td width="3%" align='center'> 
			<?php if(($i+($PageNum*($page-1)))!=$amount&&$displaydownup=="ok"){ ?>
				<a id='dpp<?=$products_rows['ID']?>' href='?skipdown=1&id=<?=$products_rows['ID']?>&Num=<?=$products_rows['Num']?>&ls=down&page=<?=$page?>&cateid=<?=$cateid?>&catename=<?=$catename?>&midcateid=<?=$midcateid?>&midcatename=<?=$midcatename?>&subcateid=<?=$subcateid?>&subcatename=<?=$subcatename?>&sortpro=<?=$sortpro?>' title='向下移动'><img src='images/down.gif' border='0'></a>
			  <input type="text" name="down<?=$products_rows['ID']?>" size="1" maxlength="5" value=1        onblur="document.getElementById('dpp<?=$products_rows['ID']?>').href=document.getElementById('dpp<?=$products_rows['ID']?>').href+'&skipdown1='+this.value">
			<?php } ?>
			</td>
			
			<td width="3%" align='center'> 
			<?php if(($i+($PageNum*($page-1)))!=1&&$displaydownup=="ok"){ ?>
				<a id='upp<?=$products_rows['ID']?>' href='?skipup=1&id=<?=$products_rows['ID']?>&Num=<?=$products_rows['Num']?>&ls=up&cateid=<?=$cateid?>&page=<?=$page?>&catename=<?=$catename?>&midcateid=<?=$midcateid?>&midcatename=<?=$midcatename?>&subcateid=<?=$subcateid?>&subcatename=<?=$subcatename?>&sortpro=<?=$sortpro?>' title='向上移动'><img src='images/up.gif' border='0'></a>
				<input type="text" name="up<?=$products_rows['ID']?>" size="1" maxlength="5" value=1          onblur="document.getElementById('upp<?=$products_rows['ID']?>').href=document.getElementById('upp<?=$products_rows['ID']?>').href+'&skipup1='+this.value">
			<?php } ?>
			</td>-->
        </tr>
        <?php $productID2=$products_rows['productID'];
		 }
	 }
	$i++;
	
	} ?>
        <tr>
          <td colspan='10' align='center' height='37'>
            
            <?php echo $page_string;
	if($productID2!=''){ ?>	<?php }?>
          </td>
        </tr>
        
  </table>
      <table width="98%" border="0" align="center" cellpadding="5" cellspacing="0">
        <tr bgcolor="">
          <td width="23%" style="padding-left:6px;"><input name="chkAll" type="checkbox" id="chkAll" onClick="CheckAll(this.form);" value="checkbox">
            所有</td>
          <td width="37%" style="padding-left:6px;"><input type="text" name="searchproductName" size="16" maxlength="50"  value="<?=$searchproductName?>" />
            <select name="searchrange" id="searchrange">
              <option value="both">综合查询</option>
              <option value="productname">查找产品名称</option>
              <option value="productno">查找产品编号</option>
            </select>
            <button type="button"  value="查询"   onclick="document.del.action='?id=<?=$id?>';document.del.submit();" />查询</button>
            <button name="重置" type="reset"   onclick="this.form.searchproductName.value='';document.del.action='?id=<?=$id?>';document.del.submit();"  value="重置" />重置</button></td>
          <td width="24%" style="padding-left:6px;"><input name="gopage" id="gopage" type="text" size="3" />
            页
            <button type="button" name="Submit" value="GO"   onclick="document.del.action='?id=<?=$id?>';document.del.submit();" />GO</button> &nbsp;  &nbsp; 
            每页
  <select name="perpage" id="perpage" onchange="document.del.action='?id=<?=$id?>';document.del.submit();">
    <option value="12" <?=$perpage==12||$perpage==""?"selected":""?>>12</option>
    <option value="20" <?=$perpage==20?"selected":""?>>20</option>
    <option value="50" <?=$perpage==50?"selected":""?>>50</option>
    <option value="100" <?=$perpage==100?"selected":""?>>100</option>
    <option value="200" <?=$perpage==200?"selected":""?>>200</option>
    <option value="500" <?=$perpage==500?"selected":""?>>500</option>
    <option value="1000" <?=$perpage==1000?"selected":""?> >1000</option>
  </select></td>
          <td width="16%" style="padding-left:6px;"><?php if( ($cateid!=""||$midcateid!="" ||$subcateid!="" ) && isset($productID2) ){?>
            <?php }?></td>
        </tr>
        <tr bgcolor="">
          <td colspan="4" align="right" style="padding-left:6px;"><button name="submitdel" type='submit' value='确定'>确定</button>
            <input name="Action" type="hidden" id="Action" value="prorela" /></td>
        </tr>
  </table>
</form>
      <br/>
<?php 
 }
?>
	   
 
 
</body>
</html>
