Trying to get property of non-object? PHP/Codeigniter
I have been literally pulling my hair out with this one and its beginning to delay the rest of my project and it really is getting me down.
I am trying to populate a pull down using values taken from a database table so that if in the future the user wants to add more options to the pull down they can add them to the table in the database.
I am using the Codeigniter platform (PHP) using MVC design pattern.
Here is the error message I am getting:
A PHP Error was encountered Severity: Notice Message: Trying to get property of non-object Filename: views/submit.php Line Number: 139
My Model function is this here which retrieves the rows from the table called "Staff". This works fine!
function retrieve_values()
{
$query = $this->db->get('staff');
if ($query->num_rows() > 0)
{
//true if there are rows in the table
return $query->result_array(); //returns an object of data
}
return false;
}
This is the controller function which receives the parameter and passes it to my view. This works fine!
public function displayform()
{
//Checks if a user is logged in, if they are not they get redirected -
if ( $this->session->userdata('name') == FALSE || $this->session->userdata('access_level') == FALSE)
{
redirect ('site/index');// to home page
}
//Stores the returned array in instance called "formdata" which will be passed to the view to be used in pulldown menu
$page['formdata']=$this->submit_model->retrieve_values();
//This loads the form
//Instance of "page" in array "page" specifies the file name of the page to load
$page['page'] = 'submit';
$this->load->view('template', $page );
return;
}
This is the part of the view which is causing the problem: I am using a foreach and then echoing the instances of the array into the option.
<select>
<?php foreach ($formdata as $row) { ?>
<option value="<?php echo $row->staff_id; ?>"><?php echo $row->name; ?></option>
<?php } ?>
</select>
printr()
of the variable $formdata
shows that it contains these values:
Array (
[0] => Array (
[staff_id] => 1
[n开发者_JS百科ame] => Cardiology Nurse
)
[1] => Array (
[staff_id] => 2
[name] => Radiology Nurse
)
[2] => Array (
[staff_id] => 3
[name] => Scrub Nurse
)
[3] => Array (
[staff_id] => 4
[name] => Circulating Nurse
)
[4] => Array (
[staff_id] => 5
[name] => Nurse
)
[5] => Array (
[staff_id] => 6
[name] => Training Nurse
)
[6] => Array (
[staff_id] => 7
[name] => Physiologist
)
[7] => Array (
[staff_id] => 8
[name] => Radiographer
)
[8] => Array (
[staff_id] => 9
[name] => Consultant
)
[9] => Array (
[staff_id] => 10
[name] => Radiologist
)
[10] => Array (
[staff_id] => 11
[name] => Cardiologist
)
[11] => Array (
[staff_id] => 12
[name] => Anaethestist
)
[12] => Array (
[staff_id] => 13
[name] => Non-medical Staff
)
)
formdata is an array of arrays, not objects, so simply change in your view:
<option value="<?php echo $row->staff_id; ?>"><?php echo $row->name; ?></option>
// to
<option value="<?php echo $row['staff_id']; ?>"><?php echo $row['name']; ?></option>
You've used result_array
, which means you're going to get an array of arrays, instead of an array of objects. You can either modify your view to have this:
<option value="<?php echo $row['staff_id']; ?>">
<?php echo $row['name']; ?>
</option>
instead of
<option value="<?php echo $row->staff_id; ?>">
<?php echo $row->name; ?>
</option>
or you can change $query->result_array()
to $query->result()
in the model.
精彩评论