This is default featured slide 1 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 2 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 3 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 4 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 5 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

Monday 17 September 2012

csv export and import script using php

Code bellow will be helpfull for you for importing and exporting csv files. Copy the code bellow and save it as "import_export_csv.php" then open it in your host e.g. 'localhost' and start importing, exporting csv files.

<?php
//don't forget to include your database connectivity.
 include('db_connection.php');
 if ($REQUEST_METHOD=="POST" && $_POST['btnSubmit']== 'Export') //this code is for exporting data when you click on "Export" this part of code will be executed.
 {
   // if you want to export data from a database table. You have to give your_table name in bellow query.
   $select = "SELECT * FROM Your_table";
   $export = mysql_query ( $select ) or die ( "Sql error : " . mysql_error( ) );
// code bellow will get names of the fields and set as header of the csv.
   $fields = mysql_num_fields ( $export );
   for ( $i = 0; $i < $fields; $i++ )
   {
    $header .= '"'.mysql_field_name( $export , $i ).'"';
    if($i != $fields-1)
    $header .= ",";
   }
//if data found in database arrange it in csv format and pass to a variable $data.
   while( $row = mysql_fetch_row( $export ) )
   {
    $line = '';
    foreach( $row as $value )
    {                                           
     if ( ( !isset( $value ) ) || ( $value == "" ) )
     {
      $value = '"'."\t".'",';
     }
     else
     {
      $value = str_replace( '"' , '""' , $value );
      $value = '"' . $value . '"' . ",";
     }
     $line .= $value;
    }
 
    $line = substr($line, 0, -1);
    $data .= trim( $line ) . "\n";
   }
   $data = str_replace( "\r" , "" , $data );
//if data=='' not found in database
   if ( $data == "" )
   {
    $data = "\n(0) Records Found!\n";                       
   }
   header("Content-type: application/octet-stream");
   header("Content-Disposition: attachment; filename=products_csv.csv");
   header("Pragma: no-cache");
   header("Expires: 0");
// display fields name and data here
   print "$header\n$data";
   exit;
 }
 elseif ($REQUEST_METHOD=="POST" && $_POST['btnSubmit']== 'Import') //this code is for importing data when you click on "Import" this part of code will be executed.
 {
 
   if ($_FILES[csv][size] > 0)
   {
    //get the csv file
    $file = $_FILES[csv][tmp_name];
    $handle = fopen($file, "r");
    $row=1;
 //fgetcsv — Gets line from file pointer and parse for CSV fields
    while (($line_of_data = fgetcsv($handle, 1000, ",")) !== FALSE)
    {
  //If your first line from csv contains fields names ignore it.
     if($row!=1)
     {
      // $line_of_data is numaric array has your impoted data $line_of_data[0] has your first record from csv which is fields name normaly.
      $query = "insert into yourtable (username, email) values ('$line_of_data[0]', '$line_of_data[1]')";
      mysql_query ($query) or die ( "Sql error : " . mysql_error( ) );
   
     }
      $row++;
        }
    echo "File imported successfuly.";
 
   }
 }
 ?>
 <div style="margin:0 auto; width:100%">
 <form method="post" name="form1" action="import_export_csv.php">
 click here for export a csv file: <input type="submit" name="btnSubmit" value="Export" />
 </form>
 </div>
 <div style="margin:10px auto; width:100%">
 <form method="post" name="form2" enctype="multipart/form-data" action="import_export_csv.php">
 select your .csv file: <input type="file" name="csv" />
 <input type="submit" name="btnSubmit" value="Import" />
 </form>
 </div>    

    

using google map lookup address by postal code

       

   <?php
    // First, we need to take their postcode and get the lat/lng pair:
    $postcode = 'SL1 2PH';
    // Sanitize their postcode:
    $search_code = urlencode($postcode);
    $url = 'http://maps.googleapis.com/maps/api/geocode/json?address=' . $search_code . '&sensor=false';
 //json_decode() returns an object pass 2nd parameter true to json_decode() if you want to get results as array
    $json = json_decode(file_get_contents($url), true);
 //print_r($json);  uncomment for seeing whole arry
     $lat = $json['results'][0]['geometry']['location']['lat'];
     $lng = $json['results'][0]['geometry']['location']['lng'];
    // Now build the lookup:
    $address_url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng=' . $lat . ',' . $lng . '&sensor=false';
 //json_decode() returns an object if you want to get results as array, pass 2nd parameter true e.g. json_decode(file_get_contents($address_url, true))
    $address_json = json_decode(file_get_contents($address_url), true);
    print_r($address_json['results'][0]['formatted_address']);
 ?>