How to export to excel in codeigniter 3

Leave a Comment
Today we gonna learn how to export to excel in codeigniter 3 without using any library. Download codeigniter and i already created test database. lets check our database

We are ready with our database lets start with controller i.e. ExportExcel.php
defined('BASEPATH') OR exit('No direct script access allowed');

class ExportExcel extends CI_Controller {

 function __construct()

 public function exportExcelData($records)
  $heading = false;
        if (!empty($records))
            foreach ($records as $row) {
                if (!$heading) {
                    // display field/column names as a first row
                    echo implode("\t", array_keys($row)) . "\n";
                    $heading = true;
                echo implode("\t", ($row)) . "\n";

 public function fetchDataFromTable()
  $query =$this->db->get('one_piece_characters'); // fetch Data from table
  $allData = $query->result_array();  // this will return all data into array
  $dataToExports = [];
  foreach ($allData as $data) {
   $arrangeData['Charater Name'] = $data['name'];
   $arrangeData['Charater Profile'] = $data['profile'];
   $arrangeData['Charater Desc'] = $data['description'];
   $dataToExports[] = $arrangeData;
  // set header
  $filename = "dataToExport.xls";
                header("Content-Type: application/");
                header("Content-Disposition: attachment; filename=\"$filename\"");

Now hit this url http://localhost/ci_tuts/index.php/ExportExcel/fetchDataFromTable. After hitting this url you will get your excel sheets.

Helper in codeigniter

Leave a Comment
Helpers, What is Helpers as the name suggests, help you with your work. Codeigniter helpers are do same thing. CodeIgniter has more than 20 helpers. Each helper file is simply a collection of functions in a particular category. Most of codeigniter helpers you use in your project. There are URL Helpers, that assist in creating links, there are Form Helpers that help you create form elements, Text Helpers perform various text formatting routines, Cookie Helpers set and read cookies, File Helpers help you deal with files, etc.

Unlike most of the other framework codeigniter helpers are not in OO(Object Oriented) format. Codeigniter helpers are simple, procedural functions. Each helper function performs one specific task, with no dependence on other functions.

Loading Helper in codeigniter at global level

Codeigniter not load helper file by by default. We have to load helper file name in config/autoload.php. After loading helper in atuoload.php file helper is globally available in your controller and views.
| -------------------------------------------------------------------
|  Auto-load Helper Files
| -------------------------------------------------------------------
| Prototype:
| $autoload['helper'] = array('url', 'file');

$autoload['helper'] = array('url','form');

Loading Helper in codeigniter at Controller level

Loading a helper file is quite simple using the following function:
$this->load->helper('name of your helper'); 
Where name is the file name of the helper, without the .php file extension or the "helper" part. For example, to load the URL Helper file, which is named url_helper.php, you would do this:
You can load helper anywhere within your controller functions (or even within your View files, although that's not a good practice), as long as you load it before you use it. You can load your helpers in your controller constructor so that they become available automatically in any function, or you can load a helper in a specific function that needs it.
$this->load->helper( array('url', 'file', 'form') ); 
Using above method you can load multiple helper at one time, you can specify them in an array.

Send Email With Attachment Codeigniter

Leave a Comment
After Send email with codeigniter and Send email with codeigniter and smtp today we gonna look how to send email with attachment. Before we start i hope you know how to upload image in codeigniter. Because this tutorial is combination of the send email with codeigniter and upload image or attachment in folder. So let's start with controller i.e. Send_email.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Send_email extends CI_Controller {

 function __construct(){

 public function sendEmail()
    if($this->input->post('send_email') == 'send_email'){
              $config['upload_path'] = './uploads/';
              $config['allowed_types'] = 'gif|jpg|png';
              $config['max_size'] = '100000';
              $config['max_width']  = '1024';
              $config['max_height']  = '768';

             $this->load->library('upload', $config);
             $upload_data = $this->upload->data();
             $this->email->from(''); // change it to yours
             $this->email->to($this->input->post('email_id')); // change it to yours
             if ($this->email->send()) {
                 echo "Mail Send";
                 return true;
             } else {


Now lets start with view i.e. email_view.php
<!DOCTYPE html>
<html lang="en">
 <meta charset="utf-8">
 <title>Mass Email Example</title>
 <link rel="stylesheet" href="">
 <script src="//"></script>
 <script src=""></script>
 <div class="container">
  <h1>Email Example With attchment</h1>
  <form action="<?php echo base_url()?>senddatabase_email/sendMassEmail" method="POST" enctype='multipart/form-data'>
    <div class="form-group">
      <label for="exampleInputEmail1">Enter Email</label>
      <input type="email" class="form-control" placeholder="Enter Email id" name="email_id"/>
    <div class="form-group">
      <label for="exampleInputEmail1">Enter Subject</label>
      <input type="text" class="form-control" placeholder="Enter Email Subject" name="subject"/>
    <div class="form-group">
      <label for="exampleInputPassword1">Body Content</label>
      <textarea class="form-control"  rows="3" placeholder="Enter Email Body Content" name="body"></textarea>
    <div class="form-group">
      <label for="exampleInputFile">File input</label>
      <input type="file" id="exampleInputFile" name="attachment">
    <button type="submit" name="send_email" value="send_email" class="btn btn-default">Submit</button>

Send Email With Attachment Codeigniter

After filling all form when user click on submit function goes to sendEmail function which is written inside Send_email controller. To send attachment with email we use attach() function of email library.

This above function enables you to send an attachment. Put the file path/name in the first parameter. Note: Use a file path, not a URL. For multiple attachments use the function multiple times. To pass file path we have to upload attachment in folder and make sure folder must be writable (666, or 777). After uploading attachment we get all uploaded data in $upload_data. $upload_data is array we need only full_path key value to do we have do something like this
$upload_data = $this->upload->data();

After that we are ready to send email to user with attachment.
Powered by Blogger.