1

I have this code in my controller:

$array = $this->get_latest_posts(5, 0);
foreach($array as $row){
  $this->load->view('blog_page', $row);
}

In my model it goes like this:

// Some SQL query here;

$array = $query->result_array();
return $array;

But the view simply does not load. Where the problem may be?

Thank you for answers!

3 Answers 3

2

You should use loop in view and write the html code in loop.It Should be like :

$array['all_posts'] = $this->get_latest_posts(5, 0);
$this->load->view('blog_page', $array);

in your view file

<ul>

<?php     foreach($all_posts as $post) { ?>
      <li><?php echo $post->name; ?></li>//assuming the name is attribute of post.
<?php  } ?>
</ul>
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

$array = $this->get_latest_posts(5, 0);

then pass this array to view,

$this->load->view('blog_page', $array);

and in you view page, you can use that array.

Comments

0

for anyone who's still has this issue when trying to load another view as a component inside a for loop...

make sure you pass your variable wrapped inside an array before you pass it to the view.

$array = $this->get_latest_posts(5, 0);
$data['row'] = $row;
foreach($array as $row){
  $this->load->view('blog_page', $data);
}

Comments

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.