• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Extracting a Field From 1 Record Returned

#1
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.
Reply

#2
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
{
 
     // no need for all the joins as you only want the name
      $result $this->db->select('name')
 
       ->from('proposal_info'); 
        ->where('id'$propid);
        ->get()
 
       ->row_array();

 
    if ( ! empty($result)) return $result['name'];
 
    else return FALSE;



Hope that helps,

Paul.
Reply

#3
Did you see my answer to this question on Stackoverflow?
Reply

#4
@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.
Reply

#5
(10-28-2017, 08:38 AM)PaulD Wrote: 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
{
 
     // no need for all the joins as you only want the name
      $result $this->db->select('name')
 
       ->from('proposal_info'); 
        ->where('id'$propid);
        ->get()
 
       ->row_array();

 
    if ( ! empty($result)) return $result['name'];
 
    else return FALSE;



Hope that helps,

Paul.

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! 
Reply

#6
(10-28-2017, 08:59 AM)PaulD Wrote: I love chaining stuff

Me too.  Big Grin

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

#7
(10-28-2017, 08:52 AM)dave friend Wrote: Did you see my answer to this question on Stackoverflow?

Dave - Thx for your response as well on stackoverflow!
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.