• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
foreach loop in model

#1
[eluser]ns8814[/eluser]
Ok so what. I understand how to pass the model to the controller. Then send the $data to the view. Then typically do the foreach loop in the view to output your results. The question I have is can you create the foreach loop in the model so when you get to the view it looks nice and neat and only needs to echo $dataVariable. In case I am not explaining it clearly I posted the code below.
Code:
~~~~~~~~~~MODEL ~~~~~~~~~
function get_customers()
{
     $query = $this->db->get('customers');
foreach($query->result() as $row)
{
echo '<option value="'.$row-&gt;customerID.'">'.$row->FirstName.$row->LastName.'</option>';
    
}
    }  

~~~~~~Controller~~~~~
$data["customerlist"]=$this->estimates_model->get_customers();

~~~~~~~VIEW~~~~~~~~~~`
&lt;?php echo $customerlist;?&gt;
This is how I currently have it setup and I thought it should work however nothing outputs on the view. Not sure if this is bad practice anyhow but it seems to make sense to try to do it this way if I can get it to work.

#2
[eluser]ns8814[/eluser]
Actually I stand corrected it prints out at the top of the Page. I guess it echos directly from the controller

#3
[eluser]vitoco[/eluser]
instead of echoing inside the loop, concat every iteration and return the final result.

Code:
~~~~~~~~~~MODEL ~~~~~~~~~
function get_customers()
{
     $query = $this->db->get('customers');
     $options = '' ;
     foreach($query->result() as $row)
     {
          $options .= '<option value="'.$row-&gt;customerID.'">'.$row->FirstName.$row->LastName.'</option>';
     }

     return $options ;
    }  

~~~~~~Controller~~~~~
$data["customerlist"]=$this->estimates_model->get_customers();

~~~~~~~VIEW~~~~~~~~~~`
&lt;?php echo $customerlist;?&gt;

Saludos

#4
[eluser]ns8814[/eluser]
your the man.

Thanks a bunch. I have learned a lot in the last few weeks. Sometimes I still have a hard time time understanding how to pass data from the model->Controller->view. I also was not aware that you could concat an equal like that .= so I guess I should do a quick search on php manual and check it out.

Thanks again

#5
[eluser]boltsabre[/eluser]
Quote:I also was not aware that you could concat an equal like that .

I've been involved with php on a almost daily basis for 2.5 years now, and I'm still learning new tricks and stuff on a very regular basis. Which is good, until I go back and look at some code I did a year ago (makes we want to cringe/cry when I see my old code!)

Quote: Sometimes I still have a hard time time understanding how to pass data from the model->Controller->view
If you're new to both PHP/OOP and CI then that's to be expected.
Just keep at it, keep researching, keep googling when you want to know something, keep playing around at it in your spare time and you'll get good at it! Good luck!!!


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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