CodeIgniter Forums

Full Version: Cannot use object of type CI_DB_mysqli_result as array In codeigniter
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello, I have got below error when I create a pagination. Any one can hep me to resolve this error...

Model:

PHP Code:
<?php
Class Pagination_model extends CI_model
{
 
   public function __construct()
 
   {
 
       parent:: __construct();
 
       $this->load->library('session');
 
   }

 
   function ambil_data($pagination,$offset)
 
   {
 
      return $this->db->get("item",$pagination,$offset)->result();
 
   }



Controller :

PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

class 
Pagination extends CI_Controller {

 
   public function __construct()
 
   {
 
       parent::__construct();
 
       $this->load->model("Pagination_model");
 
       $this->load->library('pagination');
 
   }
 
   public function index($offset=3)
 
   {
 
       $data $this->db->get("item");

 
       $config['total_row'] = $data->num_rows();
 
       $config['base_url'] = base_url().'pagination/index';
 
       $config['per_page'] =3;

 
       //bootstrap
 
       $config['full_tag_open'] = "<ul class='pagination pagination-sm' style='position:relative;top:25px;'";
 
       $config['full_tag_close'] = "</ul>";
 
       $config['num_tag_open'] = "<li>";
 
       $config['num_tag_close'] = "</li>";
 
       $config['cur_tag_open'] = "<li class='disabled'><li class='active'>< a href='#'>";
 
       $config['cur_tag_close'] = "<span class='sr-only'></span></a></li> ";
 
       $config['next_tag_open'] = "<li>";
 
       $config['num_tag_close'] = "</li>";
 
       $config['prev_tag_open'] = "<li>";
 
       $config['prev_tag_close'] = "</li>";
 
       $config['first_tag_open'] = "<li>";
 
       $config['first_tag_close'] = "</li>";
 
       $config['last_tag_open'] = "<li>";
 
       $config['last_tag_close'] = "</li>";

 
       $this->pagination->initialize($config);

 
       $data['halaman'] = $this->pagination->create_links();
 
       $data['offset'] = $offset;

 
       $data['data'] = $this->Pagination_model->ambil_data($config['per_page'],$offset);


 
       $this->load->view('pagination',$data);

 
   }




View

PHP Code:
<table id="example1" class="table table-bordered table-striped table-hover">
 
   <thead>
 
   <tr>
 
       <th style="text-align: center;">Item ID</th>
 
       <th style="text-align: center;">Item Name</th>

 
   </tr>
 
   </thead>
 
   <tbody>
 
   <?php
    foreach 
($data as $row) {

 
       ?>
        <tr>
            <td><?=$row->item_id ?></td>
            <td><?=$row->item_name ?></td>

        </tr>
    <?php }
 
   ?>
    </tbody>
</table>
<?php echo $halaman?>
Use var_dump or print_r on all your variables to see why your input/output contains wrong data.
Maybe you should be returning a result_array() and not a result() from your model.