Welcome Guest, Not a member yet? Register   Sign In
foreach in view, what am I doing wrong
#1

[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.
#2

[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;
#3

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

InsiteFX
#4

[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;




Theme © iAndrew 2016 - Forum software by © MyBB