Trying to get a non-object property? PHP / Codeigniter

I literally pull my hair out with it, and its beginning delays the rest of my project, and it really turns me on.

I am trying to fill a pull using values ​​taken from a database table, so if in the future the user wants to add additional parameters to the drop-down menu, they can add them to the table in the database.

I am using the Codeigniter platform (PHP) using the MVC design pattern.

Here is the error message I get:

A PHP error has occurred. Severity: Notification: attempt to get a property of a non-object. File Name: views / submit.php Line Number: 139

The My Model function is here, which extracts rows from a table called "Personnel". It works great!

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 a controller function that takes a parameter and passes it to my view. It works great!

 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 that causes the problem: I use foreach and then repeat the array instances in options.

 <select> <?php foreach ($formdata as $row) { ?> <option value="<?php echo $row->staff_id; ?>"><?php echo $row->name; ?></option> <?php } ?> </select> 

printr() $formdata variable indicates that it contains the following values:

 Array ( [0] => Array ( [staff_id] => 1 [name] => 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 ) ) 
+6
source share
2 answers

formdata is an array of arrays, not objects, so just change 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> 
+15
source

You used result_array , which means that you are going to get an array of arrays, not an array of objects. You can change your view to have this:

 <option value="<?php echo $row['staff_id']; ?>"> <?php echo $row['name']; ?> </option> 

instead

 <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.

+3
source

Source: https://habr.com/ru/post/893697/


All Articles