Welcome Guest, Not a member yet? Register   Sign In
HELP: Can't Display Data
#1

Hi. I am very new to CodeIgniter and I am using v2.2.3.
I managed to create the following code but I am unable to display the data.

Here is my Model
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Item_Model extends CI_Model {
      function __construct() {
        parent::__construct();
        
    }


    // Fetch data according to per_page limit.
    public function fetch_data($id) {
        $this->db->where('id', $id);
        $query = $this->db->get('items');
        if($query->num_rows()!==0)
        {
            return $query->result();
        }
        else
            return FALSE;
   }
}
?>


Here is my Controller
Code:
function item()
    {
        if($this->session->userdata('logged_in'))
           {          
               
          
            $data["results"] = $this->item_model->fetch_data($id);          


                   $session_data = $this->session->userdata('logged_in');
                   $data['username'] = $session_data['username'];  
                $data['title'] = '';
                $this->load->view('inventory/item_view', $data);
         }
         else
       {
               $this->session->set_flashdata('message', 'Oops! You have to Login');
         //If no session, redirect to login page
             redirect('login', 'refresh');
       }

    }


I am unable to display data by getting the ID.
I hope anyone can help
Reply
#2

(This post was last modified: 07-27-2015, 10:13 AM by mwhitney. Edit Reason: fix PHP tags )

You should probably be using CodeIgniter 3 if you're just getting started. 2.2.3 will reach end of life in October.

As for your problem, you haven't included your view code or really given an accurate description of your problem. By looking at your code, $results should be either FALSE or an array of row objects, so you would check in your view (since you aren't checking in your controller):
PHP Code:
<?php if ($results === FALSE) : ?>
<p>No results found</p>
<?php 
else : 
    foreach (
$results as $row) : 
?>
<p><?php echo '<pre>' print_r($row) . '</pre>'?></p>
<?php
    
endforeach;
endif; 
Reply




Theme © iAndrew 2016 - Forum software by © MyBB