Foreach undefined variable error on codeigniter view [closed] Foreach undefined variable error on codeigniter view [closed] codeigniter codeigniter

Foreach undefined variable error on codeigniter view [closed]


Does your server allow <? tags. Some configurations don't and they'll turn them into regular html comments. Try <?php foreach($comments as $c): ?>, see if that removes the problem.

Edit:

Now that we fixed that error, it doesn't know what $comments is. That's because in your controller you defined it as $commentsList:

So you need <?php foreach($commentsList as $c): ?>

Note: You still can't use <? tags, because those were the problem in the first place.

Second Edit:

I looked up CodeIgnitor's documentation, and it says you have to include in $this->load->view('view_name', $data); where data is an array of values, where the key is the variable name used in the view, and the value is the value of that key.

So try this: $this->load->view('view-name', array('comments' => $comments));

Then in the view go back to <?php foreach($comments as $c): ?>

See this for details


Try this :

<?php foreach($comments as $c){    echo $c['comment'];}?>


Strange error you got there. Why use the shorthand PHP tag when there is no HTML in the foreach loop? Or is the production code different?

You can make your code work by using the following code (work-around):

<?php    foreach($comments as $key=>$c) {        if(isset($c) && isset($c['comment'])) {            echo $c['comment'];        } else {            echo 'Error at index ' . $key;        }    }?>

Please report how manu errors you get returned.