returning an array from a db result set - 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: returning an array from a db result set (/showthread.php?tid=17655) |
returning an array from a db result set - El Forum - 04-11-2009 [eluser]stef25[/eluser] having some basic php array problems. below is a function from my model that gets some results from a db. id like it to return an array called $data. this works, but the print_r only prints out 1 row of the 2 rows in that table. when it iterates through the foreach i think it just overwrites the previous values. how do i create the $data array that holds all the row's data instead of just one row? Code: function getMainFeature(){ returning an array from a db result set - El Forum - 04-11-2009 [eluser]Mike Ryan[/eluser] You are overwriting the $data array each time you go through the loop, so it will only contain the last entry. This would work: Code: foreach ($query->result_array() as $row) I'm not sure why you are looping through the array to copy it. Why not just return that value directly? Code: $sql = 'SELECT id, prod_name, prod_descr_short FROM products'; returning an array from a db result set - El Forum - 04-11-2009 [eluser]stef25[/eluser] hey, thanks for your reply. your first snippet outputs some errors: Message: Use of undefined constant id - assumed 'id' any idea how to fix this? getting closer, thanks! EDIT: think it should actually be Code: foreach ($query->result_array() as $row) { returning an array from a db result set - El Forum - 04-11-2009 [eluser]TheFuzzy0ne[/eluser] What's wrong with doing this(?): Code: function getMainFeature() I must be missing something... returning an array from a db result set - El Forum - 04-11-2009 [eluser]stef25[/eluser] the only thing you missed was that you're better than us. your solution works fine and ill be using this :-) thanks TheFuzzy0ne returning an array from a db result set - El Forum - 04-11-2009 [eluser]TheFuzzy0ne[/eluser] I wouldn't say I'm better than anyone, but rather I've been here before and made the same mistakes. returning an array from a db result set - El Forum - 04-11-2009 [eluser]Mike Ryan[/eluser] [quote author="stef25" date="1239468311"] Message: Use of undefined constant id - assumed 'id' [/quote] Sorry, I was in perl mode :-) |