Welcome Guest, Not a member yet? Register   Sign In
passing query results from model to view
#1

[eluser]ns8814[/eluser]
I dont know. I posted a similar problem awhile back and thought I had a grasp of the concept. I am not a programmer by trade and am trying to figure this thing out. Anyway basically I am trying to setup an edit form using the same create new form. I simply want to get the results from the Get-Project_details_model and and use them in the view.

Code:
MODEL
function get_project_details($projectID)
{
  $this->db->where('projectID',$projectID);
  $query=$this->db->get('deck_'."projects");
  
  if ($query->num_rows() > 0)
  {
   foreach($query->result() as $row)
   {
   $row = $query->row_array();
   $projectname=$row['projectname'];
  
     echo $projectname;
     echo $row['customerID'];
    
   }
  return $query->result();
  }
}
HERE is the Controller:
Code:
CONTROLLER
function editproject ()
{
  $projectID=$this->uri->segment(4);
  $this->output->enable_profiler(TRUE);
  $this->load->model('estimates_model');
  $data["results"]=$this->estimates_model->get_project_details($projectID);
  
  
  $data['main_menu'] ='mainmenu_view';
  $data['main_content']="estimates/".'CreateEstimate_view';
  
  $this->load->view("templates/Default/"."admin_view2",$data);
  
}

Here is in the view
Code:
<input name="ProjectID" type="hidden" value="" />
  Enter a Project Name: <input name="projectname" type="text" value="<?php echo $projectname; ?>"/><br />

Anyway I messed around with for awhile. I took a long break a couple months and tried to jump back into it which didnt help much. I just cant seem to get a solid grasp of how to get the information from the model to the view. I am sure I am forgetting something fairly simple though.
Any help would be greatly appreciated.
#2

[eluser]ns8814[/eluser]
ok so I put the model in the typical fashion. as follows.To explain my question better. I don't want to have to write a foreach in the view inorder to get some data from the array.

MODEL VIEW
Code:
function get_project_details($projectID)
{
  $this->db->where('projectID',$projectID);
  $query=$this->db->get('deck_'."projects");

  return $query->result();
  
}

CONTROLLER VIEW
Code:
$data['pdetails']=$this->estimates_model->get_project_details($projectID);
$this->load->view("templates/Default/"."admin_view2",$data);

So lets say a table column is 'projectname'
How can get that data out in the view without having to use a foreach statement. This way I can use the isset() function to populate the form.
#3

[eluser]Marcus Marden[/eluser]
it seems to be you have same question as like me.

http://ellislab.com/forums/viewthread/227752/
#4

[eluser]TWP Marketing[/eluser]
Personally, I have no problem using foreach() to create view code. It works well when you cannot predict the actual number of records to be displayed, or you have a select list of options.

Why don't you want to use foreach()?
#5

[eluser]cartalot[/eluser]
first problem in yr controller:
Code:
$data["results"]=$this->estimates_model->get_project_details($projectID);

you are assuming that you will get results back, which is not safe

second related problem in yr model, again you need a fallback
easiest is IF there are no results -- return FALSE;

so then in yr controller do an IF check like
the ! means, if this did NOT happen
Code:
if( ! $data['pdetails'] = $this->estimates_model->get_project_details($projectID) )
{
// whatever your fallback is for no results back
}
else
{
// make the magic happen
}

foreach -- it can be very confusing with the index etc, but once you get used to it, its very cool.

think of it this way - once you get your results into $results in the model - the controller
is just making sure something came back from the database, and then passing it to the view.

if there are no results, then you are dealing with it BEFORE going to the view. in other words, sure
you can check with an isset in the view -- but really you dont need to because your controller should catch it before calling the view.
#6

[eluser]ns8814[/eluser]
Yes I understand the risk of not getting a result. however it is something should always yield a result. I can definetly add that in when I get it working.

All the data is getting passed from the model to controller. and the result array gets passed to the view. I just cant figure out how to get the results out of the array.
doing a var_dump($results) in the view gives me

object(stdClass)[23]
public 'ProjectID' => string '60' (length=2)
public 'customerID' => null
public 'Status' => string 'Archived' (length=8)
public 'projectname' => string 'Harry Sack' (length=10)


I just cant figure out how to get the variables out.
#7

[eluser]ns8814[/eluser]
Ok So I changed the model as follows.

Code:
function get_project_details($projectID)
{
  $this->db->where('projectID',$projectID);
  $query=$this->db->get('deck_'."projects");
  $pdetails=$query->row();
  return $pdetails;
  //return $query->result();
  
  
}
I can now get the results I want by typing
echo $pdetails->projectname

However is there a way to get the data out with as an object using the $query->result()

#8

[eluser]CroNiX[/eluser]
result() and row() both produce an object.

result() is an array of rows (multiple results). Each row is an object.
row() is a single row, which is an object (single result).

So if you are only going to get one row, like getting a user by username, then use row() (or row_array()).
If you are grabbing more than one, use result() (or result_array()).

result_array() does the same thing as result(), except each row is an array.
result_row() does the same thing as row(), except the result is a single array.

if you use result() or result_array(), you will need to loop over your result to get the individual rows in the view.
Code:
foreach($result as $row)
{
  echo $row->fieldname;  //if object
  echo $row['fieldname']; //if array
}

If you use row() or row_array(), you can just directly use the values.
Code:
echo $result->fieldname;  //object
echo $result['fieldname'] //array
#9

[eluser]ns8814[/eluser]
Thank you very much that was the answer I was looking for. Sorry still very new to php and codeigniter.




Theme © iAndrew 2016 - Forum software by © MyBB