CodeIgniter Forums
foreach in view, what am I doing wrong - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: foreach in view, what am I doing wrong (/showthread.php?tid=38101)



foreach in view, what am I doing wrong - El Forum - 01-30-2011

[eluser]Unknown[/eluser]
Hi !

I'm new to CI and PHP framework globaly.

I'm having an issue making a loop to display news in my view.

Code:
<?php foreach($news as $row => $val): ?>
    <li>This is the title : &lt;? echo $val->title; ?&gt;</li>
    <li>This is the title : &lt;? echo $row->title; ?&gt;</li>
&lt;?php endforeach; ?&gt;

These to echo displays the SAME thing (Oo ?) : "this is the title : title; ?&gt;" instead of the real title.

Moreover, when I do the same directly in my controller, it works flawlessly... The error only is in th view, and I don't get why.

For info :
Controller :
Code:
$this->load->model('News_model', '', TRUE);
$viewData['news'] = $this->News_model->get_last_x_entries(10);
$this->load->view('includes/header');
$this->load->view('news/news_default', $viewData);
$this->load->view('includes/footer');

Model :
Code:
function get_last_x_entries($x)
    {
        $this->db->select('id, author_id, title, content, created_at');
        $this->db->from('news');
        $this->db->limit($x);
        $query = $this->db->get();
        return $query->result();
    }

Thanks in advance.


foreach in view, what am I doing wrong - El Forum - 01-30-2011

[eluser]mi6crazyheart[/eluser]
Hmmm, every things seems all right. Ok try this in u'r VIEW & tell me working or not...
Code:
&lt;?php foreach($news as $val):  ?&gt;
    <li>This is the title : &lt;? echo $val->title; ?&gt;</li>
&lt;?php endforeach; ?&gt;



foreach in view, what am I doing wrong - El Forum - 01-30-2011

[eluser]InsiteFX[/eluser]
Maybe use &lt;?php

InsiteFX


foreach in view, what am I doing wrong - El Forum - 01-30-2011

[eluser]artallan[/eluser]
Code:
&lt;?php foreach($news as $row => $val): ?&gt;
    <li>This is the title : &lt;? echo $val->title; ?&gt;</li>
    <li>This is the title : &lt;? echo $row->title; ?&gt;</li>
&lt;?php endforeach; ?&gt;

use proper php tag,
Code:
&lt;?php echo $val->title; ?&gt;
or if you want the shortcode use
Code:
&lt;?=$val->title?&gt;