Monday, 21 April 2014

EXPORT FORM DATA TO EXCEL (CSV) IN PHP

In this tutorial you are going to learn how to download an excel report after filling a HTML form.
MySQL is not used in this tutorial, just a simple example showing how to generate Excel file using core PHP functions.
<?php

//give a filename
$filename = "myexcel.csv";


//set headers to download the file
header( 'Content-Type: text/csv' );
header( 'Content-Disposition: attachment;filename='.$filename);

//set an array
$cells = array("Column1", "Column2", "Column3");

$file = fopen('php://output', 'w');           

fputcsv($file,$cells);

fclose($file);


?>
When you run this code on a web browser, a CSV (myexcel.csv) file will immediately download with  the values given in the array $cells[].
I have used php://output stream which sends the output from Apache server to the browser.
fputcsv() - this function plays the main role in the above script, it helps us to create the CSV file from the array $cells[].
Now, lets move to the dynamic part,
Create a form in HTML, see the below (index.html):
<html>
<body>
    <form action="excel.php" method="post">
        <table>
            <tr><td>Name</td><td><input type="text" name="name" /></td></tr>
            <tr><td>Email</td><td><input type="text" name="email" /></td></tr>
            <tr><td>Location</td><td><input type="text" name="location" /></td></tr>
            <tr><td>&nbsp;</td><td><input type="submit" /></td></tr>
        </table>
    </form>
</body>
</html>

PHP script – slightly modified from the first script (excel.php):
<?php

//give a filename
$filename = "myexcel.csv";


//set headers to download file
header( 'Content-Type: text/csv' );
header( 'Content-Disposition: attachment;filename='.$filename);

$file = fopen('php://output', 'w');           

//set the column names
$cells[] = array('Name', 'Email', 'Location');

//pass all the form values
$cells[] = array($_POST['name'], $_POST['email'], $_POST['location']);

foreach($cells as $cell){
    fputcsv($file,$cell);
}


fclose($file);


?>
if you compare the first script and this one above, you can see there is a difference in $cells[]  variable which became multi-dimensional array and also I’m looping through each array usingforeach()
Basically first iteration will pass the column names which is hard coded, the second iteration will pass the dynamic values from $_POST.

Monday, 14 April 2014

WordPress Blog Too Slow? 7 Ways You Can Speed It Up Again In Minutes

Your WordPress site is slow, and you’re losing page views and revenue because of it. But you’re also busy – I get it. Here’s seven speed tips you could have a crack at on your lunch break – they’re so simple and easy.

Optimize Images

This is a two-parter. The first thing is to equip yourself with the knowledge of using the best format for the job. When we talk about images used on the web, it’s generally restricted to either a JPG or PNG. PNG is a lossless format, best suited to simple images and illustrations, or screenshots. JPG is a lossy format suitable for photographs – it can retain the detail at varying levels of quality. If you’re uploading photographs in PNG format, you probably have images that are up to ten times as big as they would be if you used JPG. Just bear in mind that JPG loses a little detail and clarity each time modifications are made and it’s saved – so always export from your originals.
  • Use JPG for photos, but only export once.
  • Use PNG for illustrations and screenshots
Second – even if you are using the the correct image format, chances are you could still be making savings by compressing and optimizing those images. EWWW Image Optimizer is a simple plugin that will do this for automatically as you upload them to WordPress. You can also run the optimizer over all your existing images, potentially shaving up to 95% with no noticeable loss in quality; and even convert to and from PNG and JPG if you did choose the wrong format. That alone will make a huge difference to loading time for the end user. What are you waiting for?