Monday 20 July 2015

CREATE DOWNLOADABLE CSV FILE IN PHP

CREATE DOWNLOADABLE CSV FILE IN PHP

<?php
mysql_connect('localhost','root','')
mysql_select_db('your database name');
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=total_student.csv');
//to write the csv file
$output = fopen('php://output', 'w');
//heading of your file
fputcsv($output,array('Class','Total no. of students'));


for($i=1;$i<=12;$i++){
$std="select count(enrollment_no) from student_enrollment where student_curr_class_stand='$i'";
$data=mysql_fetch_array(mysql_query($std));
$class="std".$i;
$student=$data[0];
//insert data in your csv file
fputcsv($output,array($class,$student));
}

?>

Saturday 4 July 2015

insert file and image usin php and ajax

HTML AND AJAX CODE

index.php

<html>
<head>
    <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>  
</head>
<script>
function uploadFile(){
  var input = document.getElementById("file");
  file = input.files[0];
  if(file != undefined){
    formData= new FormData();
    if(!!file.type.match(/application.*/)){
      formData.append("application", file);
      $.ajax({
        url: "upload.php",
        type: "POST",
        data: formData,
        processData: false,
        contentType: false,
        success: function(data){
            alert('success');
        }
      });
    }else{
      alert('Not a valid image!');
    }
  }else{
    alert('Input something!');
  }
}


</script>
<body>
    <input type="file" id="file" />
    <button onClick="uploadFile();">UploadFile</button>
</body>
</html>

Upload.php

<?php
$dir = "../gurukul/assignments/";
move_uploaded_file($_FILES["application"]["tmp_name"], $dir. $_FILES["application"]["name"]);
?>
INSERT DATA IN DATABASE FROM CSV OR EXCEL SHEET
 HTML CODE

<div class="admin_head"><h2>Add Data Using CSV File</h2></div>
<div class="clr"></div>
<hr />
<?php if(isset($err)) echo "<div class='error mb_15'>$err</div>"; ?>

<div class="admin_head"><h2>Upload new csv by browsing to file and clicking on Upload</h2></div>
<div class="clr"></div>

<form method="post" enctype="multipart/form-data">
<table cellpadding="0" cellspacing="0" border="0" id="ad_tab">
 <tr><td></td><td></td><td></td></tr>
 <tr>
  <td class="req">File name to import : </td>
  <td><input type="file" name="filename" size="50"></td>
 </tr>
 <tr>
 <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 <input type="submit" value="Upload" name="submit"></td>
 <tr>
</table>
</form>


PHP CODE

<?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";
}
}
?>