We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

CI News tutorial undefined variable error

Development and Programming

Ankush Thakur's avatar
Ankush Thakur
1 posts
13 years ago
Ankush Thakur's avatar Ankush Thakur

I’m stuck with a particular error in the News tutorial of CodeIgniter. I’m not sure why it’s not working. I’ve copied everything to the letter, and yet I keep getting this error when I try to visit a slug page (http://localhost/codeig/index.php/news/slug-2 for example):

A PHP Error was encountered Severity: Notice Message: Undefined variable: news Filename: news/index.php Line Number: 1

A PHP Error was encountered Severity: Warning Message: Invalid argument supplied for foreach() Filename: news/index.php Line Number: 1

Following are the various files.

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();
 }
}
?>

news.php

<?php 
class News extends CI_Controller {
 public function __construct() {
  parent::__construct();
  $this->load->model('news_model');
 }

 protected function render($data){
  $this->load->view('templates/header', $data);
  $this->load->view('news/index', $data);
  $this->load->view('templates/footer');
 }
 
 public function index() {
  $data['news'] = $this->news_model->get_news();
  $data['title'] = 'News archive';
  //print_r($data['news']); -- Working
  $this->render($data);
  $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'];
  $this->render($data);
 }
 
}
?>

index.php

<?php foreach ($news as $news_item) {?>
<h2><?php echo $news_item['title'];?></h2>
<div id="main">
 <?php echo $news_item['text'];?>
</div>
<a href="http://news/<?php">;?>"]View article</a>
<?php } ?>

view.php

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

routes.php

$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';

I’ve been pulling my hair for the past two hours. Please help!!

       
tauruss's avatar
tauruss
3 posts
13 years ago
tauruss's avatar tauruss

It’s not exactly “copied everything to the letter”. You’ve added function render() which load view “news/index”, and you’re using it in function view() in controller, which should load view “news/view”, instead of “news/index”.

       
Ankush Thakur's avatar
Ankush Thakur
1 posts
13 years ago
Ankush Thakur's avatar Ankush Thakur

Oh, dear! I feel so embarrassed! 😛 Thank you so much for solving this! 😊

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.