Reputation:
I am new to PHP, CodeIgniter, and I am currently studying the tutorials of both. I'm trying to build a blog tutorial, and I'm receiving some errors. Help would be greatly appreciated. Thanks in advance!
Controller:
<?php
class Blog extends Controller{
function index()
{
$data['title'] = "My Blog Title";
$data['heading'] = "My Blog Heading";
$data['todo'] = array('clean house','eat lunch','call mom');
$this->load->view('blog_view');
}
}
?>
View:
<html>
<head>
<title><?=$title?></title>
</head>
<body>
<h1><?=$heading?></h1>
<ol>
<?php foreach($todo as $item): ?>
<li><?=$item?></li>
<?php endforeach; ?>
</ol>
</body>
</html>
Errors:
Message: Undefined variable: heading
Filename: views/blog_view.php
Line Number: 6
Message: Undefined variable: todo
Filename: views/blog_view.php
Line Number: 10
Message: Invalid argument supplied for foreach()
Filename: views/blog_view.php
Line Number: 10
Upvotes: 4
Views: 2266
Reputation: 4549
and if you having that message again (happens when defined in methods other than the index method). best idea (the way i resolved it) is to put "@" before that variable in your view file. and woala...
Upvotes: 0
Reputation: 121
You need to pass your $data variable to the view:
$this->load->view('blog_view', $data);
Upvotes: 12