Extracting a Field From 1 Record Returned - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: Extracting a Field From 1 Record Returned (/showthread.php?tid=69258) |
Extracting a Field From 1 Record Returned - cgtrman - 10-27-2017 The code below works, allowing me to retrieve all fields for a single record. One of the fields I use from this record is 'name' but to access it on the view page, I must place two lines of code to extract the 'name' field from the record returned. I want to be able to extract the 'name' field in the controller and pass to view. I'm sure there is a simply solution since the data is already successfully retrieved. MODEL - Performs a select all and returns a single record // Select a record (all columns) from 'proposal_info' table per prop ID public function get_propinfo($propid) { // Build query $this->db->select('*'); $this->db->from('proposal_info P'); $this->db->join('status', 'P.status = status.id'); $this->db->join('regions', 'P.region = regions.id'); $this->db->join('usa_states', 'P.state_id = usa_states.id'); $this->db->join('type', 'P.type = type.id'); $this->db->where('P.id', $propid); return $this->db->get(); } CONTROLLER - The line in red is is where I retrieve the record from the query in the model. The 'name' field is contained within the $data['propinfo'] but how can I retrieve it here in the controller? Seems like it would public function summary() { $this->load->model('Propinfo_model'); $data['page'] = "Project Summary"; $data['allprops'] = $this->Propinfo_model->get_allprops($_SESSION['userid_db']); // required for LH nav proj list $data['propinfo'] = $this->Propinfo_model->get_propinfo($_SESSION['session_propid']); // query for proposal info $data['announcements'] = $this->Propinfo_model->get_announcements($_SESSION['session_propid']); // query for announcements $this->load->view('templates/bp_header_view', $data); $this->load->view('templates/bp_nav_view', $data); $this->load->view('summary_view', $data); // page content $this->load->view('templates/bp_footer_view', $data); $this->load->view('templates/bp_endjs_view', $data); } VIEW - Unfortunately, this is the only way I know how to get the 'name' field. This wks, but I want to do this at the controller, not the view. $row = $propinfo->row_array(); $proposal_name = $row['name']; Any feedback is greatly appreciated. RE: Extracting a Field From 1 Record Returned - PaulD - 10-28-2017 Why not return the row array in your model like this: PHP Code: return $this->db->get()->row_array(); Then in your view get the name like this PHP Code: <?php echo $propinfo['name']; ?> OR, in your model have your function something like this and only return the name: PHP Code: public function get_propinfo_name($propid) Hope that helps, Paul. RE: Extracting a Field From 1 Record Returned - dave friend - 10-28-2017 Did you see my answer to this question on Stackoverflow? RE: Extracting a Field From 1 Record Returned - PaulD - 10-28-2017 @Dave friend I just read your answer, and I did not realise you could chain view loads! :-) Much cleaner - thank you. (You live and learn I suppose - I love chaining stuff) Paul. RE: Extracting a Field From 1 Record Returned - cgtrman - 10-28-2017 (10-28-2017, 07:38 AM)PaulD Wrote: Why not return the row array in your model like this: Paul - Thank you for the response. This is the solution! I replaced my code in the model with return $this->db->get()->row_array(); and then was able to use your code in the view <?php echo $propinfo['name']; ?> to get the 'name' field. Since I also needed other fields from that record, this was the better method. Thx again! RE: Extracting a Field From 1 Record Returned - dave friend - 10-28-2017 (10-28-2017, 07:59 AM)PaulD Wrote: I love chaining stuff Me too. Most (but not all) of the loader methods return $this which makes chaining possible. I recently learned another term for the concept of "method chaining" - Fluent Interfaces. Like you said, "Live and Learn". RE: Extracting a Field From 1 Record Returned - cgtrman - 10-28-2017 (10-28-2017, 07:52 AM)dave friend Wrote: Did you see my answer to this question on Stackoverflow? Dave - Thx for your response as well on stackoverflow! |