0

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.

2
  • 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. Put on more code. What you really want to do here ?? Commented Mar 21, 2015 at 10:30
  • @ParagTyagi i want all the of$note data to passed as array into view(note) Commented Mar 21, 2015 at 10:32

2 Answers 2

3

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>
Sign up to request clarification or add additional context in comments.

Comments

0

you can use following.

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

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

3 Comments

I'm not down voting your answer but this will provide only last array value
foreach ($usertable->result() as $note) { $temp[]=$note; } $temp['title']; $this->load->view('note',$temp); i think its cos i have paste same array you can try above.
you can define array before foreach like $temp=array();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.