<? ob_start();

session_start();

require_once("../dbconnect.php");



 $view="Select * from products where pid='".$_GET['prid']."' "; 

 $exeview=mysql_query($view);

 $viewrow=mysql_fetch_assoc($exeview);
  $pname = $viewrow["pname"];	
require_once("sesurl.php");

//////****csv***/////

//$fp1 = fopen('products.csv', 'w');
    $fp1=fopen('Wallpaper.csv','w');
    
	 fwrite($fp1,"pid,");
	 fwrite($fp1,"catid,");
	 fwrite($fp1,"subcat_id,");
	 fwrite($fp1,"subsubcatid,");
	 fwrite($fp1,"itemno,");
	 fwrite($fp1,"pname,");
	 fwrite($fp1,"unitsprice,");
	 fwrite($fp1,"colors,");
	 fwrite($fp1,"description,");
	 fwrite($fp1,"discount,");
	 fwrite($fp1,"mini,");
	 fwrite($fp1,"maxi,");
	 fwrite($fp1,"status,");
	 fwrite($fp1,"addedon,");
	 fwrite($fp1,"weight,");
	 fwrite($fp1,"sizes,");
	 fwrite($fp1,"Print\n,");
	 fclose($fp1);

			  
// for insert related products
$msg = "";
if(isset($_POST["insert"])){
	 $products = $_POST["products"];
	 $cat_id = $_POST["cat"];
	$product_ids = 	"";
	if($products!=""){
		foreach($products as $values) {
			$product_ids .= $values.","; 
		}
		$product_ids = substr($product_ids,0,-1);
	}
	
	$chk_rs = mysql_query("select * from related_products where prid=".$_GET["prid"]." and catid = ".$cat_id."");
	if(mysql_num_rows($chk_rs)>0){
		$insert_pr="update related_products set relprid = '".$product_ids."' where prid=".$_GET["prid"]." and catid=".$cat_id."";
	}
	else {
	$insert_pr= "INSERT INTO related_products(prid,catid,relprid) VALUES('".$_GET['prid']."','".$cat_id."','".$product_ids."')";
	
	}
	
	$prfm_insert = mysql_query($insert_pr);
	if($prfm_insert){
		$msg = "Your record has been successfully inserted";
	}	
	
}
			  
$pr_chk ="";
$view_pro = false;
if(isset($_POST["cat"])) {
	if($_POST["cat"]!=0){
		$pr_sql = mysql_query("select * from products where catid= ".$_POST["cat"]."");
		if(mysql_num_rows($pr_sql)>0)	
			$view_pro = true;
	}
}		  



?>

<!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=iso-8859-1" />

<title>Welcome to Sir Box and Plastics</title>

<style type="text/css">

<!--

body {

	margin-left: 0px;

	margin-top: 0px;

	margin-right: 0px;

	margin-bottom: 0px;

}

-->

</style>

<link href="stylesheet.css" rel="stylesheet" type="text/css" />

<script language="javascript" src="ajaxvalid.js"></script>



</head>

<body>

<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">

  <tr>

    <td><table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">

      <tr>

        <td valign="top"><table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">

          <tr>

            <td><? require_once("header.php"); ?></td>

          </tr>

          <tr>

            <td>&nbsp;</td>

          </tr>

			<tr>
				<td>
					<table>
						<tr>
							<td>Add related products</td>			
						</tr>			
						<tr>
							<td>
								<form name="form1" method="post">
								<table>
									<?php  if($msg!="" ) {?>
										<tr>
											<td colspan="2" style="color:red;text-align:center;"><?php echo $msg;?></td>
										</tr>
									<?php  }?>
									<tr>
										<td>Product Name:	</td>	
										<td><?php echo $pname;?></td>	
									</tr>
									<tr>
										<td>Select Category:	</td>	
										<td>
											<select name="cat" onchange="document.form1.submit();" >	
											<option value="0">Select Category</option>	
										<?php 
											$sql = mysql_query("select * from tblcategories ");
											while ($result = mysql_fetch_assoc($sql)){ 
												$selected = "";
												if($_POST["cat"]==$result["category_id"])
													$selected = "selected";		
										?>
												<option <?php echo $selected;?> value="<?php echo $result["category_id"];?>"><?php echo $result["categoryname"];?></option>
												<?php 			
											}
										?>		
											</select>
										</td>	
									</tr>
									<?php if($view_pro) { ?>
									<tr>	
										<td>Select Products :</td>
										<td> 
											<table>			
												<tr>
													<?php 
														$cnt = 0;				
														while($pr_rs = mysql_fetch_assoc($pr_sql)){
															$chk_pro = mysql_query("select relprid from related_products where prid=".$_GET["prid"]." and catid=".$_POST["cat"]."");
															$checked = "";				
															if(mysql_num_rows($chk_pro)>0) {		
																$rel_fetch = mysql_fetch_assoc($chk_pro);				
																if(strstr($rel_fetch["relprid"],$pr_rs["pid"]))	{
																	$checked = "checked";			
																}				
															}
																if($cnt%4 == 0) {
																	echo "</tr><tr>";
																}
													?>			
														<td><input type="checkbox" <?php echo $checked;?> name="products[]" value="<?php echo $pr_rs["pid"];?>"><?php echo $pr_rs["pname"];?></td>
													<?php $cnt++; }?>
												</tr>		
											</table>	
										</td>
									</tr>
									<?php } ?>
									<tr>		
										<td colspan="2" > <input type="submit" name="insert" value="Submit"></td>		
									</tr>		
								</table>
								</form>
							</td>			
						</tr>		
					</table>
				</td>
			<tr>

          </tr>

          <tr>

            <td>&nbsp;</td>

          </tr>



          <tr>

            <td><? require_once("footer.php"); ?></td>

          </tr>

          <tr>

            <td height="200">&nbsp; </td>

          </tr>

        </table></td>

      </tr>

    </table></td>

  </tr>

</table>

</body>

</html>

