Reputation: 516
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
<?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
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($id). It says that $id is undefined.
I hope anyone can help
Upvotes: 1
Views: 99
Reputation: 12039
Since you said your $id
is from $_GET
then try using $this->input->get()
function item()
{
if($this->session->userdata('logged_in'))
{
$id = $this->input->get('id', TRUE); //If $_GET param name is id
$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');
}
}
Upvotes: 1
Reputation: 1249
Here In your controller's action add parameter you will get your data.
function item($id) {
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');
}
}
Upvotes: 0
Reputation: 101
You'll need to define $id
before the model can use it. Since you said you don't know how, I'd suggest a tutorial.
http://learn-codeigniter.com/episode/codeigniter_basics
Upvotes: 1