Amit Saharan
Amit Saharan

Reputation: 73

Passing data to view Codeigniter

I want to pass row fetched from database to view from controller.

foreach ($usertable->result() as $note) {
    $note['title'];
    $this->load->view('note',$note);    
}

but it didn't work.

Upvotes: 0

Views: 63

Answers (2)

Nimish Mer
Nimish Mer

Reputation: 42

you can use following.

foreach ($usertable->result() as $note) {
    $note[]=$note;
}

$note['title'];
$this->load->view('note',$note);

Upvotes: 0

Parag Tyagi
Parag Tyagi

Reputation: 8960

Strictly, not the right way to send data to view (not inside for loop). This will load the view the numbers of time the loop runs.

Enclosed all the notes data into a variable say via an array data['notes'] and the now in view you can use notes variable for fetching data.
Read docs for more info.

In controller:

$data['notes'] = $usertable->result();
$this->load->view('note', $data);

In view:

<table>
<tr>
    <td>Note id</td>
    <td>title</td>
</tr>

<?php foreach($notes as $n) { ?>
<tr>
    <td><?php echo $n->id; ?></td>
    <td><?php echo $n->title; ?></td>
</tr>
<?php } ?>

</table>

Upvotes: 3

Related Questions