<?php
include("../session.php");
include("../connect.php");
$user=$_SESSION['company'];

function findexts ($filename) 
{ 
$filename = strtolower($filename) ; 
$exts = split("[/\\.]", $filename) ; 
$n = count($exts)-1; 
$exts = $exts[$n]; 
return $exts; 
}


date_default_timezone_set ("Singapore"); 
$date=date("d-m-Y", time());
$time=date("h:i:sa", time());


	
$array_num = count($_POST['id']);

if(isset($_POST['submit'])){
	for ($i=0; $i <$array_num; $i++){
		$id = $_POST['id'][$i];
		$name=$_POST['name'][$i];
		//$type=$_POST['type'][$i];
		
		$type2=$_POST['type2'][$i];
		
		$type=$_POST['type'][$i];
		
		$price=$_POST['price'][$i];
		$detail=$_POST['detail'][$i];
		$search=$_POST['search'][$i];
		$dollar = $_POST['dollorsign'][$i];
		$url = $_POST['url'][$i];
		//
		$filename = $_FILES['file']['name'][$i];
		if($filename){
		$uploadedfile = $_FILES['file']['tmp_name'][$i];
		$ext = findexts ($_FILES["file"]["name"][$i]) ; 
		//echo $_FILES["file"]["name"][$i];

		if($ext=="jpg" || $ext=="jpeg" || $ext=="JPG" || $ext=="JPEG")
		{
		$uploadedfile = $_FILES['file']['tmp_name'][$i];
		$src = imagecreatefromjpeg($uploadedfile);

		}
		else if($ext=="png" || $ext=="PNG")
		{
		$uploadedfile = $_FILES['file']['tmp_name'][$i];
		$src = imagecreatefrompng($uploadedfile);

		}
		else if($ext=="gif" || $ext=="GIF")
		{
		$uploadedfile = $_FILES['file']['tmp_name'][$i];
		$src = imagecreatefromgif($uploadedfile);
		}
		list($width,$height)=getimagesize($uploadedfile);
		$newwidth=150;
		$newheight=150;		
		//$newheight=($height/$width)*$newwidth;
		$tmp=imagecreatetruecolor($newwidth,$newheight);

		imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
		
		
		$ran2 = $id."."; 
		$target_path = "album/"; 
		$target_path = $target_path . $ran2.$ext;
		$filename2 = $ran2.$ext;
		
		imagejpeg($tmp,"album/". $filename,100);

		imagedestroy($src);
		imagedestroy($tmp);

		rename ("album/$filename", "album/$filename2");
			//move_uploaded_file($_FILES['file']['tmp_name'][$i], $target_path) ;						
			mysql_query("UPDATE product SET pName='$name',productPic = '$filename2',type='$type',type2='$type2',price='$price',detail='$detail',searchtag='$search',dollar='$dollar',producturl='$url' WHERE id = '$id'")or die(mysql_error());
			mysql_query("UPDATE promotion SET pname='$name',normalprice='$price',dollar='$dollar' WHERE pID = '$id'")or die(mysql_error());
		}
		else{
			mysql_query("UPDATE product SET pName='$name',type='$type',type2='$type2',price='$price',detail='$detail',searchtag='$search',dollar='$dollar',producturl='$url' WHERE id = '$id'")or die(mysql_error());
			mysql_query("UPDATE promotion SET pname='$name',normalprice='$price',dollar='$dollar' WHERE pID = '$id'")or die(mysql_error());
		}
				

	}
}
echo "<script language=javascript>alert('Update Complete');window.location='editproduct.php';</script>";
die();
?>