Extract multiple rows from DB and pass values 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: Extract multiple rows from DB and pass values to view (/showthread.php?tid=52875) |
Extract multiple rows from DB and pass values to view - El Forum - 06-30-2012 [eluser]Dandy_andy[/eluser] I have been searching for a simple explanation to my question but can't find one. I have a DB table from which I want to return only a specific field that could apply to various rows. For example:- ID mem_id photo_path -- ------ ---------- 1 1001 somepath1 2 1002 somepath2 3 1003 somepath3 4 1003 somepath4 5 1003 somepath5 6 1003 somepath6 7 1004 somepath7 8 1005 somepath8 I am just interest in the 'photo_path's that apply for each 'mem_id'. I have a model set up to extract rows or each instance of a particular MEM_ID:- Code: function return_photo_paths($id) { The problem I have is passing the array through to my controller in the most simplest way so that I can call on each of the paths by using $photo_path[1], $photo_path[2]... but I can't seem to achieve this as the result returned is a multidimensional array. My controller looks like this at the moment:- Code: $photopath = $this->Image_file->return_photo_paths($id); And in my view, I want to be able to call on the various paths by using a loop as the number will vary depending on which photos are assigned to which user... Code: <?php Any help appreciated. Thanks. Extract multiple rows from DB and pass values to view - El Forum - 06-30-2012 [eluser]vitoco[/eluser] First, always try to return something even when there's no matches to the query, in this case an empty array Code: function return_photo_paths($id) { In the controller you don't need to store it before either. Code: $per_id['photopath_inview'] = $this->Image_file->return_photo_paths($id) ; IN THE VIEW Code: <?php Saludos Extract multiple rows from DB and pass values to view - El Forum - 07-01-2012 [eluser]Dandy_andy[/eluser] Thanks, it seems obvious when someone else tells you! Extract multiple rows from DB and pass values to view - El Forum - 07-03-2012 [eluser]cartalot[/eluser] in the model you might want to try returning an object, it can be much simpler to deal with Code: return $query->row(); would also suggest doing as much logic as you can in the model and controller so like if no results back from model -- do your logic in the controller as to what gets passed and shown in view then keep view as simple as possible |