<div class="admin_head"><h2>Upload new csv by browsing to file and clicking on Upload</h2></div>
<td>
<?php
if(isset($_POST['submit'])) {
$data=$_FILES['filename'];
if(!$data['size']) $err="please select the file";
elseif(is_uploaded_file($_FILES['filename']['tmp_name'])) {
$handle = fopen($_FILES['filename']['tmp_name'], "r");
$hasNext=0;
$KEY=0;
$couponCategory=array();
$couponCatID=array("others"=>"");
$couponMerchant=array();
$merchantID=array();
while(($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if($hasNext) {
$couponCategory[$KEY]=strtolower(trim($data[4]));
$merchantFetch=explode(".",strtolower($data[5]));
$couponMerchant[$KEY]=strtolower(trim($merchantFetch[0]));
$KEY++;
}
$hasNext++;
}
$couponCategory=array_merge(array_unique($couponCategory));
$couponMerchant=array_merge(array_unique($couponMerchant));
if(!$couponCategory || !$couponMerchant) die("Required data not found");
// check coupon category present in database
for($i=0;$i<count($couponCategory);$i++) {
if($couponCategory[$i]!=NULL) {
$check="select id from coupons_category where name='$couponCategory[$i]'";
list($CID)=mysql_fetch_row(mysql_query($check));
if($CID) $couponCatID[$CID]=$couponCategory[$i];
else {
$err="Category : ".ucwords($couponCategory[$i])." not found, please add category first";
break;
}
}
}
// check merchant present in database
for($j=0;$j<count($couponMerchant);$j++) {
$check1="select merchant_id from merchant where merchant_name='$couponMerchant[$j]'";
list($MID)=mysql_fetch_row(mysql_query($check1));
if($MID) $merchantID[$MID]=$couponMerchant[$j];
else {
$err="Merchant : ".ucwords($couponMerchant[$j])." not found, please add merchant first";
break;
}
}
if(!@$err) $proceedMe="ok";
}
if(@$proceedMe) {
$handle = fopen($_FILES['filename']['tmp_name'], "r");
$hasNext=0;
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if($hasNext) {
$time=time();
$merchant=explode(".",strtolower($data[5]));
$merchant_name=strtolower(trim($merchant[0]));
$cat_name=strtolower(trim($data[4]));
$mer_id=array_search($merchant_name,$merchantID);
$cat_id=array_search($cat_name,$couponCatID);
$tags=strtolower($data[4]." ".$$data[1]." ".$data[0]." ".$data[3]." ".$data[5]." ".$merchantWebsite);
$exp= explode(" ",$data[2]);
$e_array=explode("-",$exp[0]);
$day=$e_array[0];
$month=$e_array[1];
$year=$e_array[2];
$e_time = mktime(23, 59, 59, $month, $day, $year);
//echo "Merchant ".$merchant_name." has id : ".$mer_id;
//echo "<br />Category ".$cat_name." has id : ".$cat_id."<hr />";
$import="INSERT into coupons(merchant_id,code,coupon_url,description,expiry,category,tags,time) values('$mer_id','$data[0]','$data[3]','$data[1]','$e_time','$cat_id','$tags','$time')";
mysql_query($import);
}
$hasNext++;
}
$err="Import Done";
}
}
?>