![]() |
Transmitting database data to view won't work - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: Transmitting database data to view won't work (/showthread.php?tid=261) |
Transmitting database data to view won't work - rooye - 11-15-2014 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 RE: Transmitting database data to view won't work - rooye - 11-15-2014 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 RE: Transmitting database data to view won't work - RobertSF - 11-18-2014 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. |