Transmitting database data to view won't work |
11-15-2014, 01:13 AM
(This post was last modified: 11-15-2014, 05:54 AM by rooye. Edit Reason: I did not present my problem explicitly )
Hello everyone
I am following the CI tutorials and it works well when I use the news.php controller as stated in the tutorials. However I get errors trying to transmit model data to my static pages(pages.php controller and home.php) I had made earlier.here are the errors: ............................................................... A PHP Error was encountered Severity: Notice Message: Undefined variable: news Filename: pages/home.php Line Number: 1 ........................ A PHP Error was encountered Severity: Warning Message: Invalid argument supplied for foreach() Filename: pages/home.php Line Number: 1 ............................................................ This is my code for news_model.php <?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(); } } Then for News.php controller <?php 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(); $data['title'] = 'News archive'; $this->load->view('templates/header', $data); $this->load->view('news/index', $data); $this->load->view('templates/footer'); } 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']; } } Here comes index.php for view <?php foreach ($news as $news_item): ?> <h2><?php echo $news_item['title'] ?></h2> <div id="main"> <?php echo $news_item['text'] ?> </div> <p><a href="news/<?php echo $news_item['slug'] ?>">View article</a></p> <?php endforeach ?> AND this view.php <?php echo '<h2>'.$news_item['title'].'</h2>'; echo $news_item['text']; THE STATIC PAGES ARE: pages.php <?php class Pages extends CI_Controller { public function index() { $data['news'] = $this->news_model->get_news(); $data['title'] = 'News archive'; $this->load->view('templates/header', $data); $this->load->view('pages', $data); $this->load->view('templates/footer'); } public function view($page = 'home') { if ( ! file_exists(APPPATH.'/views/pages/'.$page.'.php')) { // Whoops, we don't have a page for that! show_404(); } $data['title'] = ucfirst($page); // Capitalize the first letter $this->load->view('templates/header', $data); $this->load->view('pages/'.$page, $data); $this->load->view('templates/footer', $data); } } AND HOME home.php <?php foreach ($news as $news_item): ?> <h2><?php echo $news_item['title'] ?></h2> <div class="main"> <?php echo $news_item['text'] ?> </div> <?php endforeach ?> Thanks for your help
Hi everyone
I discovered the problem and it had to do with an error or mismatch of variables. I corrected that and all is ok now.THANKS TO YOU ALL
Yeah, that happens, and good for you for finding the error.
Don't hesitate to return when you have another error, or just come back and see if you can help someone else. |
Welcome Guest, Not a member yet? Register Sign In |