<?php session_start();  //Developed by ITechScripts?>
<html>
<head>
<title>Admin</title>
>
<style type="text/css">
<!--
.style1 {
	color: #666666;
	font-weight: bold;
}
.style3 {
color: #666666; font-size: 11px; font-family:Arial, Helvetica,sans-serif
}
-->
</style></head>
<body topmargin="0">
<center><?php
 require 'include/configurations.php'; // ITECHBIDS Configuration File 
 if($_POST[flag])
 {
 $chkfile=$_POST['chkfield'];
 $Table_name=$_POST['txtTabname'];
 $file_type=$_FILES['file_name']['type'];
   if(!empty($Table_name))
   {
    $csv_file=$_FILES['file_name']['name'];
    if(!empty($csv_file))
   {
     if($file_type=='text/comma-separated-values')
     {
     $row = 1;
 
  // upload the csv file 
                     $uploaddir= getcwd(); 
 					 $updir=explode('/',$uploaddir);
					 $count=count($updir)-1;
					 for($i=0;$i<$count;$i++)
					 {
         			 $up_dir.=$updir[$i]."/";
					 }
                     $uploaddir=rtrim($up_dir,"/");
					 $uploadfile="$uploaddir"."/images/"."$csv_file";
					 if(move_uploaded_file($_FILES['file_name']['tmp_name'],$uploadfile))
                     {  
					 $handle = fopen("$uploadfile", "r");
                     while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
 {
   $num = count($data);
  $chkfile="yes";
      if($chkfile=='yes')
   {
   	 if($row==1)
	 {
	 $sql="insert into $Table_name(";    
	 for ($c=0; $c < $num-7; $c++)
     {
       $sql= "$sql"."$data[$c] , ";
     }
     $sql=rtrim($sql," ,");
	 $sql=$sql. ") values (";
     }
	 else if($row >= 2)
	 {
	 $sql_value="";  
	 for ($c=0; $c < $num-7; $c++)
     {
	   $sql_value=$sql_value." '$data[$c]' ,";
     }
	  $sql_value=rtrim("$sql_value"," ,");
	  $ins_sql="$sql"."$sql_value".") ";
	  $res=mysql_query($ins_sql);
	  if($res)
	  {
	  $id=mysql_insert_id();
	  if($data[$c]=="yes")
	  {
$data1 = $data[$c+1];
$data2 = $data[$c+2];
$data3 = $data[$c+3];
$data4 = $data[$c+4];
$data5 = $data[$c+5];
$data6 = $data[$c+6];
	  $fea_sql="insert into featuredproducts(productid,gallery_feature,home_feature,bold,border,highlight,privateauc) values('$id','$data1','$data2','$data3','$data4','$data5','$data6')";
	  $fea_res=mysql_query($fea_sql);
	  if($fea_res)
	  $msg="Data Loaded Successfuly";
	  else
	  $msg="Sorry! mismatch in fieldnames or Tablename in database";
	  }
$msg="Data Loaded Successfuly";	  
	  }
	  else 
	   $msg="Sorry! mismatch in fieldnames or Tablename in database.";

	 }
	  
   }
   $row++;
  }
       fclose($handle);
     }
     else
     { 
      $msg="This is not an CSV file. Script terminated";
     } 
  }
  
 }  
 else
  {
    $msg="You Must Specify the Filename First!.";
  }
  }
 else
 {
 $msg="You Must Specify the Tablename First";
 }
 }
?>
<? require 'include/header.php'; ?>
<form action="bulk_load.php" method="post" enctype="multipart/form-data">
<table width="80%"  border="0" cellspacing="2" cellpadding="5" class="ft">
<tr class="ct" height="25">
  <td align="left" colspan="2">
Bulk Loader
</td>
</tr>
<? if(!empty($msg))
{
?>
<tr><td colspan="2" align="center"><font size="2" color="red"><?= $msg; ?></font></td></tr>
<?
}
?>

<tr><td align="left" colspan="2">Select CSV file from your local computer </td>
</tr>
<input type="hidden" name="txtTabname" value="auction_details">
<tr><td align="right">CSV File: </td>
<td align="left"><input type="file" name="file_name"></td></tr>
<tr><td>&nbsp;</td><td align="left">
<a href="#" id="dislink" onClick="window.open('list_cats.php','pop_up','toolbar=no, top=0,location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width=400, height=400')"><font color=red>View List of Category IDs</font></a>
</td></tr>
<tr><td>&nbsp;</td><td align="left">
<a href="#" id="dislink" onClick="window.open('list_users.php','pop_up','toolbar=no, top=0,location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width=400, height=400')"><font color=red>View List of User IDs</font></a>
</td></tr>
<tr><td>&nbsp;</td><td align="left">
<a href="#" id="dislink" onClick="window.open('../bulk_uploads/sample_csv.csv','pop_up','toolbar=no, top=0,location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width=400, height=400')"><font color=red>View CSV File Format </font></a>
</td></tr>
<tr><td colspan="2"><font size="2" color="#FF0000">Note that if you have mismatch in fieldnames or Tablename in database the Loading data will be errors!.</font></td></tr>
<tr><td colspan="2" align="center">
<input type="hidden" value="1" name=flag>
<input type="submit" class="button"   value="Load Data">
</td></tr>
</table>
</form>
<? require 'include/footer.php'; ?>
</center>
</body>
</html>
