Codeigniter: Pass Multidimensional Array to the View

In this post, we will see how to pass multidimensional array to the view in codeigniter.Multidimensional array is an array which contains more than one array. Suppose we have two table one is employee basic details and the other is salary table and we want data from both table in an array. So the best way to produce all these data in a clean manner by using multidimensional array.

Lets have an example of multidimensional array.

Controller:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class MultidimensionalArrayController extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function index() {
$data = array();
$data['employeedetails']['basicinfo']['name'] = "Ajay Kumar";
$data['employeedetails']['basicinfo']['contact']['phone'] = "0657-2246748";
$data['employeedetails']['basicinfo']['contact']['mobile'] = "8645697412";
$data['employeedetails']['basicinfo']['email'] = "ajay@gmail.com";
$data['employeedetails']['basicinfo']['address'] = "Bagbera,Jamshedpur";
$data['employeedetails']['salaryinfo']['bank'] = "ICICI";
$data['employeedetails']['salaryinfo']['acno'] = "18556464846484";
$data['employeedetails']['salaryinfo']['branch'] = "Jamshedpur";
$data['employeedetails']['salaryinfo']['ifsc'] = "ICIC864";
$this->load->view('multidimensional', $data);
}
}

View:

<html>
<head>
<title>Multidimensional Array Example</title>
</head>
<body>
<style>
h3{
font-family: Verdana;
font-size: 18pt;
font-style: normal;
font-weight: bold;
color:red;
text-align: center;
}
table{
font-family: Verdana;
color:black;
font-size: 12pt;
font-style: normal;
font-weight: bold;
text-align:left;

}
</style>
<h3>Multidimensional Array Example</h3>
<div class="row">
<div class="col-md-2"></div>
<div class="col-md-8">
<table id="table" align="center" cellpadding = "5" border="2" class="table table-striped table-bordered" cellspacing="0" width="80%" style="position:relative; top:7px;">
<thead>
<tr><th colspan="4">Basic Information</th></tr>
<tr>
<th>Name</th>
<th>Contact</th>
<th>Email</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo $employeedetails['basicinfo']['name']; ?></td>
<td>Phone: <?php echo $employeedetails['basicinfo']['contact']['phone']; ?> | Mobile: <?php echo $employeedetails['basicinfo']['contact']['mobile']; ?></td>
<td><?php echo $employeedetails['basicinfo']['email']; ?></td>
<td><?php echo $employeedetails['basicinfo']['address']; ?></td>
</tr>

</tbody>
</table>
<table id="table" align="center" cellpadding = "5" border="2" class="table table-striped table-bordered" cellspacing="0" width="80%" style="position:relative; top:7px;">
<thead>
<tr><th colspan="4">Salary Information</th></tr>
<tr>
<th>Bank</th>
<th>A/c No.</th>
<th>Branch</th>
<th>IFSC</th>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo $employeedetails['salaryinfo']['bank']; ?></td>
<td>Phone: <?php echo $employeedetails['salaryinfo']['acno']; ?></td>
<td><?php echo $employeedetails['salaryinfo']['branch']; ?></td>
<td><?php echo $employeedetails['salaryinfo']['ifsc']; ?></td>
</tr>

</tbody>
</table>
</div>
<div class="col-md-2"></div>
</div>
</body>
</html>
Multidimensional Array
Thank you for reading this post. we hope you like this Post, Please feel free to comment below, your suggestion. if you face any issue with this code let us know. We’d love to help!

Base Model: Create and Use Base Model in Codeigniter

Codeigniter Base Model

In this post, We will discuss about base model in Codeigniter. Purpose of uses of Base model is to put all common CRUD functions in one model and can be extended to each child model. In every CI project i uses this base model to increase more productivity.

1.What is Base model?

Base model is a model where we can put all our common function which may be used within entire system.
By extending this base model to all other model we can use functions easily and with less code.

2. Why Use Base Model?

Purpose of uses of base model is to removing repetition of code,increasing productivity and coding in less amount of time.

3.Key Features of Codeigniter Base Model:

a.) CRUD and all common functionality in one place.
b.) Easy to use.

4. Need to know:

Before we creating CI base model we need to know few things.
a.) The class declaration of model must extend the parent class.
b.) As defined subclass_prefix in config file, name of new class for model must be prefixed with ‘MY_’.You can change this. This is configurable.

5. Implementation of Base Model:

Lets start with the example of base model. Follow these steps as i am showing below.

I.Create class named MY_Model.php inside application/core and put all common functions inside here. Here is how code looks.

<?php
class MY_Model extends CI_Model{
protected $_table_name = '';
protected $_primary_key = 'id';
protected $_primary_filter = 'intval';
protected $_order_by = '';
public $rules = array();
protected $_timestamps = FALSE;
function __construct()
{
parent::__construct();
}
public function array_post($fields){
$data = array();
foreach ($fields as $field) {
$data[$field] = $this->input->post($field);
}
return $data;
}
public function get($id = NULL, $single = FALSE){
if ($id != NULL) {
$filter = $this->_primary_filter;
$id = $filter($id);
$this->db->where($this->_primary_key, $id);
$method = 'row';
} elseif ($single == TRUE) {
$method = 'row';
} else {
$method = 'result';
}
if (!count($this->db->order_by($this->_order_by))) {
$this->db->order_by($this->_order_by);
}
return $this->db->get($this->_table_name)->$method();
}
public function get_by($where, $single = FALSE)
{
$this->db->where($where);
return $this->get(NULL, $single);
}
public function save($data, $id = NULL)
{
// Set timestamps
if ($this->_timestamps == TRUE) {
$now = date('Y-m-d H:i:s');
$id || $data['created'] = $now;
$data['modified'] = $now;
}
// Insert
if ($id === NULL) {
!isset($data[$this->_primary_key]) || $data[$this->_primary_key] = NULL;
$this->db->set($data);
$this->db->insert($this->_table_name);
$id = $this->db->insert_id();
} // Update
else {
$filter = $this->_primary_filter;
$id = $filter($id);
$this->db->set($data);
$this->db->where($this->_primary_key, $id);
$this->db->update($this->_table_name);
}
return $id;
}
/* To delete single row */
public function delete($id)
{
$filter = $this->_primary_filter;
$id = $filter($id);
if (!$id) {
return FALSE;
}
$this->db->where($this->_primary_key, $id);
$this->db->limit(1);
$this->db->delete($this->_table_name);
}
/* To delete multiple row data */
public function delete_multiple($where)
{
$this->db->where($where);
$this->db->delete($this->_table_name);
}
}
?>

II. Now in other model we will extend base model to use all functions of base model as i am showing below.

<?php
class PayrollModel extends MY_Model{
public $_table_name;
public $_order_by;
public $_primary_key;
}
?>

III. Next create a controller where we can call model function and each model automatically loads base model by extending it.

<?php
class Payroll extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('PayrollModel');
}
public function payroll_function()
{
$id = 'Unique Id';

/* to get all input post data */
$data = $this->PayrollModel->array_post(array('bank', 'branch'));
$this->PayrollModel->_table_name = "table_name"; /* table name */
$this->PayrollModel->_primary_key = "id"; /* primary key */

/* For insert */
$this->PayrollModel->save($data); /* this will insert data */

/* For update */
$this->PayrollModel->save($data, $id); /* this will update data */

/* To get Data with filter */
$data['salary_allowance_info'] = $this->PayrollModel->get_by(array('id' => $id), FALSE); /* First parameter can contain multiple array, second is for result/row */

/* To delete row with unique id */
$this->PayrollModel->_table_name = "tbl_salary_template"; // table name
$this->PayrollModel->_primary_key = "id"; // $id
$this->PayrollModel->delete($id);

/* Delete multiple data */
$this->PayrollModel->_table_name = "table_name"; // table name
$this->PayrollModel->delete_multiple(array('ids' => $id));
}
}

Thank you for reading this post. we hope you like this Post, Please feel free to comment below, your suggestion. if you face any issue with this code let us know. We’d love to help!

Fetch records between date range using Codeigniter active record

Please find below the code to fetch all records between date range using codeigniter active record.

public function getAllDataBetweenDates($firstDate,$secondDate){
$this->db->where('date >=', $firstDate);
$this->db->where('date <=', $secondDate); return $this->db->get('your_table_name');
}

How to insert multiple array data using codeigniter active record

Below is the example to insert multiple array data using query builder class of codeigniter. By using $this->db->insert_batch() method we can achieve this.

[php]
<?php
defined(‘BASEPATH’) OR exit(‘No direct script access allowed’);
class InsertController extends CI_Controller {
public function __construct()
{
parent::__construct();
}
    public function index() {
        $data[] = array(
‘name’=>’Avinash’,
‘mobile’=>’72345613’,
‘address’=>’Delhi’
);
$data[] = array(
‘name’=>’Dinesh’,
‘mobile’=>’78945676’,
‘address’=>’Mumbai’
);
$this->db->insert_batch( ‘tbl_user’, $data );
    }
}
[/php]

Thank you for reading this post. we hope you like this Post, Please feel free to comment below, your suggestion and problems if you face – let us know. We’d love to help!