![]() |
extract() in the 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: extract() in the view? (/showthread.php?tid=7869) |
extract() in the view? - El Forum - 04-25-2008 [eluser]deslacker[/eluser] I'm having a problem understanding the disply of variables that to my understanding are extracted by CI when I include $data in the call to my view: Code: <?php I understand the array name is vehicles but isn't this array extracted into variables based on the keys in the array. In this case they are year, make, model, etc. Why can't I echo these as $year, $make, $model, etc. from my view? It doesn't make a difference whether I $query->result or query->result_array in my model. Thanks everyone! extract() in the view? - El Forum - 04-25-2008 [eluser]gtech[/eluser] I am not sure how your data is returned from the model so assuming you are just doing somthing like model: Code: .. controller: Code: function index() Code: <?php print_r($vehicles)?> this should show you how the array is built up in the view, then you can work out how to itterate through it. you will probably find the result_array can return more than 1 record so it will be somthing like: echo $vehicles[0]['year']; extract() in the view? - El Forum - 04-25-2008 [eluser]deslacker[/eluser] Thanks! Here's my model code. I can foreach through the result whether I return an object or an array but why can't I address the extracted variables directly based on the key values? Code: <?php extract() in the view? - El Forum - 04-25-2008 [eluser]gtech[/eluser] in your example your passing an array called data to your view, with the key vehicles. you can pass other keys as well for example buildings; Code: $data['vehicles'] = $this->mdl_vehicle->get_all_records(); in the view, the keys of the $data array become the extracted variables. so $vehicles will be the the result_array from get_all_vehicles(); $buildings will be the result_array from get_all_buildings(); so to access your keys you will have to do somthing like this in your veiw; Code: <?php foreach($vehicles as $row):?> this is because result_array() can return more than one row eg. Code: <?php if you only want to return one row from the database use row_array() instead, then you access Make as follows in your view Code: echo $vehicles['Make']; |