0
votes

Today i am trying to understand coding principles with Code Igniter framework. Nevertheless, i faced with stupid problem.

Here is my controller grades:

<?php
class Grades extends CI_Controller
{
    public function view()
    {
        $data['todo_list'] = array('Clean House', 'Call Mom', 'Run Errands');

        $data['title'] = "My Real Title";
        $data['heading'] = "My Real Heading";

        $this->load->view('pages/home', $data);
    }
}       
?>

And here is mine home:

<html>
<head>
    <title><?php echo $title;?></title>
</head>
<body>
<h1><?php echo $heading;?></h1>

<h3>My Todo List</h3>

<ul>
    <?php foreach ($todo_list as $item):?>

    <li><?php echo $item;?></li>

    <?php endforeach;?>
</ul>

</body>
</html>

The output is:

A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: pages/home.php

Line Number: 11


Severity: Notice

Message: Undefined variable: heading

Filename: pages/home.php

Line Number: 6

May I have missed something? I would like to find out how to solve this problem.

Routes.php

$route['grades'] = 'grades';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
$route['404_override'] = '';
3
I think you want just $route['default_controller'] = 'Grades'; $route['(:any)'] = 'Grades/$1'; - Prix

3 Answers

0
votes

There is not error in your controller or view file, but there is a problem in routes

$route['grades'] = 'grades'; should be $route['grades'] = 'grades/view';

$route['default_controller'] = 'pages/view'; should be $route['default_controller'] = 'grades/view';

you have to notice that in routing works like $route['{is_the_string_will_appear_in_URL}']='{is_a_controller_name}/{is_a_method_name}/${will_be_first_parameter}' Please let me know if this works for you

0
votes

You are not accessing the elements properly. Try like this

<?php foreach ($data['todo_list'] as $item):?>

OUTPUT :

enter image description here

-1
votes

Err1: Change : to ; on line 11!

Err2: You must define the variable $heading before use it. It doesn't have any value now.