![]() |
Display single record in 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: Display single record in view (/showthread.php?tid=36509) Pages:
1
2
|
Display single record in view - El Forum - 12-04-2010 [eluser]simplepixie[/eluser] All tutorials etc that I have read all cover how to get records from a database and use the foreach to display the results (i.e. list or table etc) and are mainly based around blogs, but I can't seem to find any examples of how to grab and display just one record. What I am trying to do is view the details of an an account holder/user but I can't work out how to pass the result information back to the controller and view file to display this information (I know how to get it, I hope). Just to expand, I have a list of accounts and I want to click on a link to view the account's full details and then if necessary update them etc. Can someone please enlighten me. Display single record in view - El Forum - 12-04-2010 [eluser]Andy UK[/eluser] You'll need to pass the id of the record you want to retrieve to the model. You can do this my constructing a link that includes the id http://yoursite.com/controller/function/id and then grabbing that id in the controller function using $id = $this->uri->segment(3); You can then retrieve the necessary data for that record by calling the model function and passing it the id $result = $this->your_model->your_function($id); Now you have the fields for the record you queried in the $result variable which can be passed to the view to do your foreach loop. I recommend your work through the video tutorials on the nettuts website, especially the CRUD one which will walk you through this whole process.. http://net.tutsplus.com/category/tutorials/php/ Display single record in view - El Forum - 12-05-2010 [eluser]simplepixie[/eluser] Thank you - I didn't explain properly as I know how to pass the id and how to get the information (I already have this working). What I don't know how to do is format the coding to pass the one result back to the view page - all tutorials I have read and watched seem to be based around sending back multiple records and using foreach to display the results and I can do this fine, what I can't get me head around is how to pass back all the fields from 1 row. Display single record in view - El Forum - 12-05-2010 [eluser]Twisted1919[/eluser] Code: function blah($id) Display single record in view - El Forum - 12-05-2010 [eluser]vodich[/eluser] This is a very good question.I would also somethimes love access only one field in the array but nothing without a foreach loop.And this last post haven't lit a match, in my brain at least. Display single record in view - El Forum - 12-05-2010 [eluser]sas1ni69[/eluser] Have you solved this? What is your fix? Thanks Display single record in view - El Forum - 12-05-2010 [eluser]Andy UK[/eluser] Why not just fetch one single record from the database as Twisted does. You only fetch the record that you want by passing the record ID to the database. Then pass that to a view and pick out the fields you want to show, for example echo $result->some_field Display single record in view - El Forum - 12-05-2010 [eluser]Atharva[/eluser] In model (some_model.php) Code: function blah($id) In controller Code: $this->load->model('some_model'); In View (some_view.php) assuming query is returning a non empty record Code: echo $details->field_from_db; Display single record in view - El Forum - 12-06-2010 [eluser]sas1ni69[/eluser] Hi Atharva, I followed your exact code and it returns me two errors; Message: Undefined variable: details Message: Trying to get property of non-object Both these errors are from the view. Display single record in view - El Forum - 12-06-2010 [eluser]Atharva[/eluser] Make sure that your query is returning something. If not, then you have to show some custom message like "No records found". Also, check whether $data['details'] is what you are using in controller so that it can be accessed in view as $details. Edit: I guess you are not passing $data to view, is that a case? |