Simple Looping Problem - 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: Simple Looping Problem (/showthread.php?tid=523) |
Simple Looping Problem - MoFish - 12-13-2014 Hi, I'm running into a little bit of trouble and would appriciate some guidance. I currently have a list of statuses held in a database table called 'status'. These are 'Open', 'Closed', 'Pending'. I also have another table of sales which is in a table called 'sales'. I'm trying to loop around each status and get the relevant sales, but am having a bit of an issue working out how this is done. Please note the below code does not work Controller: PHP Code: // this returns all the statuses (open, closed, pending) View: PHP Code: <?php foreach ($status as $s){ ?> Thank you for your time. MoFish RE: Simple Looping Problem - wolfgang1983 - 12-13-2014 (12-13-2014, 07:59 AM)MoFish Wrote: Hi, Have you tried some thing like this on controller PHP Code: $results = $this->sales_model->get_sales(); View Example PHP Code: <?php foreach($sales as $sale) { ?> Model example PHP Code: public function get_sales() { RE: Simple Looping Problem - MoFish - 12-13-2014 I have tried the following but unfortunately am still struggling. I'm needing to loop around a table called "status" and get all status and display these to the screen in my view. eg. Code: <h1>Open</h1> I then need to loop around another table called "sales" and get all the elements where its 'sale_status_id' equals the 'status_id' from the above loop. This should then display something like. Code: <h1>Open</h1> Regards, MoFish RE: Simple Looping Problem - includebeer - 12-13-2014 PHP Code: foreach($data['status'] as $status){ Simple, you overwrite the previous result at each iteration of your loop, and you end up with only the last one. Store the results in an array like this : PHP Code: foreach($data['status'] as $status){ RE: Simple Looping Problem - MoFish - 12-13-2014 Hi Includebeer, Thank you for taking the time to respond. I have tried the following, but unfortunately i'm still not getting the results I require. PHP Code: $data["status"] = $this->sales_model->get_status(); In the array returned to the view $sales, this does not contain all the status values with the sales inside each one. I would have expected a result something like the following: Code: Open // loop around each status Regards, MoFish RE: Simple Looping Problem - includebeer - 12-13-2014 Quote:In the array returned to the view $sales, this does not contain all the status values with the sales inside each one. I would have expected a result something like the following: You can build your result array the way you want. Since I don't know how your model returns the data I can't give you the exact code. One way you can do it is like this : PHP Code: $data["status"] = $this->sales_model->get_status(); This will give something like : PHP Code: array{ RE: Simple Looping Problem - MoFish - 12-14-2014 Hi, I'm not sure if i'm doing this the correct way, but im returning my results in the model like: PHP Code: $query = $this->db->get(); I tried the above, but unfortunately still not playing ball. Regards, MoFish RE: Simple Looping Problem - includebeer - 12-14-2014 What error do you get? Note that the foreach in my example is assigning $status by reference. So you can modify the original array and not a copy of it. Also, you don't have to loop on the results, you can replace PHP Code: foreach ($query->result() as $row) { PHP Code: return $query->result(); RE: Simple Looping Problem - MoFish - 12-14-2014 I think you may have cracked it! Seems to be appearing as expected. Thanks ever so much for taking the time to respond to me. |