Echo a single entry from a single DB returned array. - 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: Echo a single entry from a single DB returned array. (/showthread.php?tid=50746) |
Echo a single entry from a single DB returned array. - El Forum - 04-06-2012 [eluser]mflammia[/eluser] Sorry if this is a simple but really struggling to find an answer. Have the following array returned to the view from a single DB query: Code: <?PHP Code: Array I can get something like the first name to display by doing something like the following: Code: <?php foreach($person as $row):?> Which seems a long winded / incorrect why to go about something that only has one record. I've tried every combination of coding to just echo a single part of the array, with just a few like: Code: echo $person[0]['first_name']; Without any luck. Help much appreciated in advance Echo a single entry from a single DB returned array. - El Forum - 04-06-2012 [eluser]CroNiX[/eluser] If you know you are only retrieving one record, use db::row() instead of db::result() and use db::row_array() instead of db::result_array(); Check the docs for database/generating query results for the options. Echo a single entry from a single DB returned array. - El Forum - 04-06-2012 [eluser]mflammia[/eluser] Thank for replying and pointing me in the right direction - had looked there many times but it just did not make sense to me. Took me a while and learnt something new in the process, thanks. For anyone else having the same problem this is what I did. In my model Code: function get_person(){ In my controller Code: function get_records_edit(){ In my view i simpely do the following Code: <?php echo $person->first_name ?> Echo a single entry from a single DB returned array. - El Forum - 04-06-2012 [eluser]jmadsen[/eluser] good job - that's the idea. In your model, I would add a check first on Code: if ($query->numrows() ==1) or you can write more generic and return a result(), a row() or false ...how ever you want to expand this (so get person could be for use with a where clause) Also, I would take the $this->uri->segment(3)) out of the model and pass it in via a parameter instead, that can be validated Eventually you may want to look into using MY_Model - lots of tutorials out there about that Echo a single entry from a single DB returned array. - El Forum - 04-06-2012 [eluser]Samus[/eluser] [quote author="jmadsen" date="1333748548"]good job - that's the idea. In your model, I would add a check first on Code: if ($query->numrows() ==1) or you can write more generic and return a result(), a row() or false ...how ever you want to expand this (so get person could be for use with a where clause) Also, I would take the $this->uri->segment(3)) out of the model and pass it in via a parameter instead, that can be validated Eventually you may want to look into using MY_Model - lots of tutorials out there about that[/quote] Code: if ($query->num_rows() ==1) Little typo. |