Welcome Guest, Not a member yet? Register   Sign In
Display single record in view
#1

[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.
#2

[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/
#3

[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.
#4

[eluser]Twisted1919[/eluser]
Code:
function blah($id)
{
   $sql = 'SELECT * FROM TABLE WHERE ID=? LIMIT 1';
   $query = $this->db->query($sql,array($id));
   return ($query->num_rows() > 0) ? $query->row() : false;
}
#5

[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.
#6

[eluser]sas1ni69[/eluser]
Have you solved this? What is your fix? Thanks
#7

[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
#8

[eluser]Atharva[/eluser]
In model (some_model.php)
Code:
function blah($id)
{
   $sql = 'SELECT * FROM TABLE WHERE ID=? LIMIT 1';
   $query = $this->db->query($sql,array($id));
   return ($query->num_rows() > 0) ? $query->row() : false;
}

In controller
Code:
$this->load->model('some_model');
$data['details'] = $this->some_model->blah($id);
$this->load->view('some_view',$data);

In View (some_view.php) assuming query is returning a non empty record
Code:
echo $details->field_from_db;
echo $details->another_field_from_db;
#9

[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.
#10

[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?




Theme © iAndrew 2016 - Forum software by © MyBB