Note: When you use the print_r() it will output the array with stdClass Object in CI when you are pulling out a value from the Database.
There is a solution to display the array without using the stdClass Object while iterating.
Example: Consider $final consider the array and while using print_r() it displayed the stdClass Object.
- You have to use the loop as follows so that it avoids the
stdClass Object while printing it.
Code:
This code you can use it for the values to retrieve from the DB using the Controller and Model.
If single row of the output is retrieved from the DB
<?php
foreach($final->result() as $single)
{
//You can print the variable values over here as follows (E.g) echo $single->id
}
?>
If Multiple row of the output is retrieved from the DB
<?php
$row=array();
foreach($final->result() as $single)
{
//You can store it as an array here if you are going on with multiple loops
$row[] = $single;
}
print_r($row); // here you can save it as an another array
?>
How should the model Code look like if you are using `->result()` in the foreach to get the values
Here is the sample that your model should look like if you are using the above methods for the retrieving of the output.
employee_model.php
<?php
class Employee_model extends CI_Model{
function __construct() {
parent::__construct();
}
public function getEmployees()
{
$this->db->select('*');
$this->db->from('employee');
$this->db->where('delete_status','0');
$this->db->where('status','1');
$this->db->order_by('id','DESC');
$query = $this->db->get();
return $query;
}
?>
How to call the model from the controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends Layout_Controller {
public function __construct(){
parent::__construct();
$this->load->library("pagination");
$this->load->model('employee_model');
$this->load->model('ajax_model');
}
public function employee_listing()
{
$result['all_employee'] = $this->employee_model->getEmployees(); // getEmployees is the function name in the employee_model
$this->load->view('frontend/employee_list',$result);
}
q1andq2keys?