ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Tutorial Foreach not working

November 04, 2013 2:05pm

Subscribe [1]
  • #1 / Nov 04, 2013 2:05pm

    HarleyRowland

    1 posts

    I am trying to work through the CodeIgniter tutorial and my news pages won’t output data from the foreach loop. I get the following messages:

    A PHP Error was encountered

    Severity: Notice

    Message: Undefined variable: news

    Filename: pages/index.php

    Line Number: 1
    and

    A PHP Error was encountered

    Severity: Warning

    Message: Invalid argument supplied for foreach()

    Filename: pages/index.php

    Line Number: 1
    This is my model class:

    <?php
    class News_model extends CI_Model {
    
    public function __construct()
    {
        $this->load->database();
    }
    
    
    public function get_news($slug = FALSE)
    {
    if ($slug === FALSE)
    {
        $query = $this->db->get('news');
        return $query->result_array();
    }
    
    $query = $this->db->get_where('news', array('slug' => $slug));
    return $query->row_array();
    }
    
    
    public function index()
    {
    $data['news'] = $this->news_model->get_news();
    $data['title'] = 'News archive';
    
    $this->load->view('templates/header', $data);
    $this->load->view('news/index', $data);
    $this->load->view('templates/footer');
    }
    }

    And my controller:

    class News extends CI_Controller {
    
    public function __construct()
    {
        parent::__construct();
        $this->load->model('news_model');
    }
    
    public function index()
    {
        $data['news'] = $this->news_model->get_news();
    }
    
    
    public function view($slug)
    {
        $data['news_item'] = $this->news_model->get_news($slug);
    
        if (empty($data['news_item']))
        {
            show_404();
        }
    
        $data['title'] = $data['news_item']['title'];
    
        $this->load->view('templates/header', $data);
        $this->load->view('news/view', $data);
        $this->load->view('templates/footer');
    }
    
    }

    And the first view:

    <h2><?php echo $news_item['title'] ?></h2>
    <p>    <div id="main"><br />
            <?php echo $news_item['text'] ?><br />
        </div><br />
        <a href="http://news/&lt?php"> ?>"]View article</a></p>
    
    <p><?php endforeach ?>

    and the second:

    <?php
    echo '<h2>'.$news_item['title'].'</h2><p>';<br />
    echo $news_item['text'];

    I hope it isn’t something too stupid!

    Thanks.

  • #2 / Apr 17, 2014 4:25am

    www.sblog.in

    31 posts

    where does you foreach start I see only

    <?php endforeach ?>

    in first view

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases