I trying to get a very simple query results set to display on a page using Codeignighter.
For my controller i have this:
class Homepage extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->helper('string');
$this->load->helper('text');
$this->load->library('form_validation');
$this->load->helper('url');
$this->load->model('Job_sectors_model');
}
public function index(){
$page_data['sectors'] = $this->Job_sectors_model->get_sectors();
$this->load->view('header');
$this->load->view('homepage', $page_data);
$this->load->view('footer');
}
}
and for my model file i have this:
class Job_sectors_model extends CI_Model {
function get_sectors() {
return $this->db->get('sectors');
}
}
On my view file i have a simple select:
<select class="form-control" name="job_sectors">
<option>Choose job sector...</option>
<?php foreach ($sectors->result() as $row) : ?>
<option value="<?php echo $row->nc_id ; ?>"><?php echo $row->tc_name ; ?></option>
<?php endforeach ; ?>
</select>
I have this setting in my autoload.php file:
$autoload['libraries'] = array('database', 'session');
The page loads and the select has the correct number of items from the db table. What I am getting is an error in each of the seelcts option when you open the select. I see thiswhen I inspect it:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: stdClass::$nc_id
Filename: views/homepage.php
Line Number: 68
So the nc_id is a table column name, its just not returning the value. How do i define the property to stop this error from happening?
Many thanks M
Ok i now have it working but it doesnt feel right:
so my model is this:
class Job_sectors_model extends CI_Model {
function get_sectors() {
$query = $this->db->get('sectors');
return $query->result_array();
}
}
my view is this:
<select class="form-control" name="job_sector">
<?php foreach ($sectors as $sector): ?>
<option value="<?php echo $sector['NC_ID']; ?>"><?php echo $sector['TC_NAME']; ?></option>
<?php endforeach; ?>
</select>
and my controller is this:
class Homepage extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->helper('string');
$this->load->helper('text');
$this->load->helper('form');
$this->load->helper('url');
$this->load->library('form_validation');
$this->load->model('Job_sectors_model');
}
public function index(){
$this->load->view('header');
$page_data['sectors'] = $this->Job_sectors_model->get_sectors();
$this->load->view('homepage', $page_data);
$this->load->view('footer');
}
When I say ity doesnt feel right is this the right way to code in codeigniter, feels like i'm hacking it together to make it work?
Thanks