Trouble pass array from DB, model to view - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Trouble pass array from DB, model to view (/showthread.php?tid=52364) |
Trouble pass array from DB, model to view - El Forum - 06-07-2012 [eluser]Dandy_andy[/eluser] This should be relatively simple, but I'm stuck. I have a DB with a table that has some country geo data organised in two rows:- country id (con_id) and country name (name). I have created a Model to extract the data from the DB and I want to pass it to the controller and then on to my view so that I can populate a dropdown list. But I'm having trouble passing an array. Here is my data... MODEL:- Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); PART OF THE CONTROLLER:- Code: <?php MY VIEW (written just to test the output at the moment but what I want to do is populate an HTML dropdown list with the relevant options values and names:- Code: <?php echo $country; ?> I know I have done something very wrong with passing the array and I would appreciate some help on this... I'm learning slowly but surely! Thanks in advance. Trouble pass array from DB, model to view - El Forum - 06-07-2012 [eluser]PhilTem[/eluser] Your code - in general - isn't wrong. Though I am wondering what this part should actually do/produce Code: foreach ($query_con->result_array() as $row) You are just looping over each row and basically do nothing but waste time You probably want something like Code: function call_country() This way you will get an array which you can easily pass to form_dropdown. Trouble pass array from DB, model to view - El Forum - 06-07-2012 [eluser]Dandy_andy[/eluser] All i want to do is pass all the DB row entries (con_id and name) to my view so I can echo them out initially. I should be able to figure out the rest from there. At the moment, all I am getting 'echoed' is the word 'Array' so I know I'm doing something wrong on the Array side! Trouble pass array from DB, model to view - El Forum - 06-07-2012 [eluser]Dandy_andy[/eluser] Hours later and I am still stuck with this. What I need to have on the view side is the value of 'con_id' and 'name' for each of the DB rows so that I can create a list. How do I combine an array into an array? Trouble pass array from DB, model to view - El Forum - 06-07-2012 [eluser]chubbypama[/eluser] i'm no ci expert, but can't you just do something like this in your model? Code: $query = $this->db->get('geo_countries'); your controller can stay as is... $data['country'] should contain an array of all the rows returned... in the view, you can loop through the records by doing something like: Code: <?php foreach ($country as $country_item): ?> Trouble pass array from DB, model to view - El Forum - 06-08-2012 [eluser]Dandy_andy[/eluser] That is not the way I would have thought of doing it but it works and it's very simple so thank you very much for the suggestion. I have always struggled with understanding PHP arrays and this is helping. Thanks again. |