PHP : Generate Excel file


$header = array(‘First Name’,’Last Name’,’About you Description’);

// Create a array having xls information to print. It could be get from database.
$xls_data_to_print = array(
‘first_name’=> ‘John’,
‘last_name’=> ‘Rawal’,
‘about_info’=> ‘He is a great player.’,

‘first_name’=> ‘John1’,
‘last_name’=> ‘Rawal1’,
‘about_info’=> ‘He is a great player1.’,

$xls_content_row = ”;
foreach($xls_data_to_print as $xls_ky => $xls_val){
// Write a code here to generate XLS file with required details.
// Use $xls_val->title OR $xls_val[‘title’] depending array type.
$rows = array();
$rows[]= $xls_val[‘first_name’];
$rows[]= $xls_val[‘last_name’];
$rows[]= $xls_val[‘about_info’];

$xls_content_row .= implode(“\t”, array_values($rows)) . “\r\n”;
$xls_content_header = implode(“\t”, array_values($header));
$xls_content = $xls_content_header.”\n”.$xls_content_row;
$filename = ‘Xls_data_list_’.date(“d_m_Y”);
header(“Content-type: text/plain; charset=UTF-8”);
header(“Content-Disposition: attachment; filename=$filename”);
header(“Content-Type: application/”);
header(“Pragma: no-cache”);
header(“Expires: 0”);
print $xls_content;

ZF2 : Add flash messages

Step 1: module/Application/src/Application/Controller/IndexController.php


namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use MyLib\Controller\AppController;

class IndexController extends AppController {

public function indexAction() {

$this->flashMessenger()->addMessage(array(‘success’ => ‘Custom success message to be here…’));

$this->flashMessenger()->addMessage(array(‘info’ => ‘Custom information message to be here…’));

$this->flashMessenger()->addMessage(array(‘error’ => ‘Custom error message to be here…’));

return new ViewModel ();


Continue reading