0

A PHP Error was encountered

Severity: Notice
Message: Undefined variable: data
Filename: views/body_view.php
Line Number: 13
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: views/body_view.php
Line Number: 13

So I have this problem in Codeigniter

Controller:

<?php
if ( ! defined('BASEPATH')) exit ('No direct script access allowed');

class Blog extends CI_Controller {

    public function __construct()
    {
    parent::__construct();
    }    
    public function index()
    {
        echo 'Hello World!';
    }

    public function mat()
    {
        $this->load->model('materials_model');
        $data = array();
        $data['news'] = $this->materials_model->get();
        $this->load->view('body_view',$data);
    }


}
?>

Model:

<?php
if ( ! defined('BASEPATH')) exit ('No direct script access allowed');

class Materials_model extends CI_Model
{
    public function get()
    {
        $query = $this->db->get('materials');
        return $query->result_array();
    }
}

?>

View:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    <meta name="author" content="The Sabotage Rebellion hackers" />

    <title>a</title>
</head>

<body>

<?php foreach ($data as $one):?>
<?=$one['author']?>
<?php endforeach; ?>

</body>
</html>
0

3 Answers 3

1

change your view code to

<?php foreach ($news as $one):?>
<?=$one['author']?>
<?php endforeach; ?>

that is, replace $data by $news

Sign up to request clarification or add additional context in comments.

Comments

0

In view do as follows

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    <meta name="author" content="The Sabotage Rebellion hackers" />

    <title>a</title>
</head>

<body>

<?php foreach ($news as $one):?>
<?=$one['author']?>
<?php endforeach; ?>

</body>
</html>

replace $data with $news in foreach

Comments

0

Say you have $data array as:

$data['x'] = 1;

if you pass the $data to a view,

you can access 1 as $x and not $data['x']

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.