<?php
session_start();
ob_start();
include_once("../dbconnect.php");//Database connection


	
	


$dbtab = "products";
$fieldseparator = ',';
$lineseparator = "\n";
//change the name of the file you want
$upload = "uploads/";
 $res = move_uploaded_file($_FILES['csv']['tmp_name'],$upload.basename($_FILES['csv']['name']));
$csvfile = $upload. $_FILES['csv']['name'];

/********************************************************************************************/
/* Would you like to add an empty field at the beginning of these records?
/* This is useful if you have a table with the first field being an auto_increment integer
/* and the csv file does not have such as empty field before the records.
/* Set 1 for yes and 0 for no. ATTENTION: don't set to 1 if you are not sure.
/* This can dump data in the wrong fields if this extra field does not exist in the table
/********************************************************************************************/
$addauto = 0;
/********************************************************************************************/
if(!file_exists($csvfile)) {
$msg = "File not found. Make sure you specified the correct path.
";
}
$file = fopen($csvfile,"r");

if(!$file) {
$msg = "Error opening data file.
";
}

$size = filesize($csvfile);

if(!$size) {
$msg = "File is empty.
";
}
$csvcontent = fread($file,$size);

fclose($file);
$lines = 0;
$queries = "";
$inserted = 0;
$linearray = array();


foreach(split($lineseparator,$csvcontent) as $line) {
$lines++;
  $line = trim($line,' \t');
  
 $line = str_replace("\\r",'',$line);
 
/************************************************************************************************************
This line escapes the special character. remove it if entries are already escaped in the csv file
************************************************************************************************************/
  $line = str_replace("'","\'",$line);
/***********************************************************************************************************/

$linearray = explode($fieldseparator,$line);

if($lines == 1)
{
//first line as the name of the fields
 $first = explode($fieldseparator,$line);
}
if($lines !=1)
{

if($_POST['catid']!="")
{
	 $catid=$_POST['catid'];
}
else
{
	$catid=0;
}



// $linemysql = implode(',',$linearray);
if($linearray[1]!="") {




/*  $sql= "INSERT INTO products(catid,cattype,pattern,accessorydesign,designtype,collectionname,pdesc,composition,length1,repeat1,prepasted,washable,	scrubbable,peelable,strippable,coordinating,price,color,pimage1) VALUES('".$_POST['catid']."','".$_POST['cattype']."','".$linearray[1]."','".$linearray[2]."','".$desval['desid']."','".$linearray[4]."','".$linearray[5]."','".$linearray[6]."','".$linearray[7]."','".$linearray[8]."','".$linearray[9]."','".$linearray[10]."','".$linearray[11]."','".$linearray[12]."','".$linearray[13]."','".$linearray[14]."','".$linearray[15]."','".str_replace(":",",",$linearray[16])."','".$linearray[17]."')";*/


echo $sql= "INSERT INTO products(catid,subcat_id,subsubcatid,itemno,pname,unitsprice,colors,description,discount,mini,maxi,status,addedon,weight,sizes,Print) VALUES('".$_POST['catid']."','".$_POST['subcat']."','".$_POST['subsubcat']."','".$linearray[0]."','".$linearray[1]."','".$linearray[2]."','".$linearray[3]."','".$linearray[4]."','".$linearray[5]."','".$linearray[6]."','".$linearray[7]."',1,'".$linearray[8]."','".$linearray[9]."','".$linearray[10]."','".$linearray[11]."')";


$queries .= $sql . "\n";
$res = mysql_query($sql);


$lastid=mysql_insert_id();

 
   


	
	        //$lastid=143;


 
	}		
if($res){

$inserted ++;
}
}
}
 $inserted; 
   $lines;
  $rec = $lines-2;

     $msg = "Found a total of $rec  records in this csv file.Data inserted successfully";
//header("location:addsubcategory.php?catid=".$_POST['catid']."&subcat=".$_POST['subcat']."&msg=".($msg));

header("Location:addsubcategory.php?catid=".$_POST['catid']."&msg=".($msg)); 




?>