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
2 possibilities. 1 the column name actually doesn't exist, 2 you don't have any rows. This prevents the later from causing issues.
class Job_sectors_model extends CI_Model {
function get_sectors() {
$q = $this->db->get('sectors');
if ($q->num_rows > 0) {
return $q->result();
}
return false;
}
}
View:
<?php if ($sectors): ?>
<select class="form-control" name="job_sectors">
<option>Choose job sector...</option>
<?php foreach ($sectors as $row) : ?>
<option value="<?php echo $row->nc_id ; ?>"><?php echo $row->tc_name ; ?></option>
<?php endforeach ; ?>
</select>
<?php else: ?>
No sectors
<?php endif; ?>
you can simply pass the data from the model in $pagedata and pass it while loading the view it will generate the array like this
$this->load->view('homepage',['pagedata' => $pagedata] ) ;
on view you can use directly if single row $pagedata->nc_id else you can simply use foreach loop like this
<?php foreach ($pagedata as $row) {?>
//your code inside and access db rows by $row->nc_id
<?php } ?>
//don't use colon after foreach loop its php
why are you sending db->get object to view instead result object/array ? is there any specific reason ???
instead the normal method should be like
class Job_sectors_model extends CI_Model {
function get_sectors() {
$result =array();
foreach($this->db->get('sectors')->result() as $row)
{
$result[$row->nc_id]=$row->tc_name ;
}
return $result;
}
}
and instead of :
<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>
view file should be like :
<?php echo form_dropdown('job_sectors', $sectors, 'default_value',' class="form-control" ' ); ?>
after putting $this->load->helper('form');
in your controllers constructor