Welcome Guest, Not a member yet? Register   Sign In
issue with controller and view
#1

[eluser]cupidleomanoj[/eluser]
hi,
I am new to CI, I get a error message as "Undefined variable : data". i have pasted my codes,


/* * *
Controller ( welcome.php)
* * * /
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function my_model() {
$this->load->model('Firstmodel');
$data['query'] = $this->Firstmodel->tbl_data();
$this->load->view('firstview', $data);
}
}
?>



/* * *
Model (firstmodel.php)
* * * /
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Firstmodel extends CI_Model {
function __construct() {
parent::__construct();
}
function tbl_data() {
$query = $this->db->get('data'); // data is my table name.
return $query->result();
}
}
?>


/* * *
View (firstview.php)
* * * /
<html>
<head></head>
<body>
<?php echo $data; ?>
</body>
</html>


********************************************
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: data

Filename: views/firstview.php

Line Number: 14

********************************************
kindly please help me... thanks in advance.......
#2

[eluser]danmontgomery[/eluser]
http://ellislab.com/codeigniter/user-gui...views.html

Quote:
Code:
<?php
class Blog extends CI_Controller {

    function index()
    {
        $data['title'] = "My Real Title";
        $data['heading'] = "My Real Heading";

        $this->load->view('blogview', $data);
    }
}
?>
Now open your view file and change the text to variables that correspond to the array keys in your data:
Code:
<html>
<head>
<title><?php echo $title;?></title>
</head>
<body>
    <h1>&lt;?php echo $heading;?&gt;</h1>
&lt;/body&gt;
&lt;/html&gt;
#3

[eluser]TWP Marketing[/eluser]
cupid, sorry this a long response but I hope it helps you understand...


Code:
/* * *
      Controller ( welcome.php)
* * * /
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends CI_Controller {
  public function __construct() {
            parent::__construct();
  }
public function my_model() {
    $this->load->model('Firstmodel');
    $data['query'] = $this->Firstmodel->tbl_data();
    $this->load->view('firstview', $data);
   }
}
?&gt;



/* * *
        Model (firstmodel.php)
* * * /
&lt;?php
    if ( ! defined('BASEPATH')) exit('No direct script access allowed');
      class Firstmodel extends CI_Model {
        function __construct() {
           parent::__construct();
        }
        function tbl_data() {
           $query = $this->db->get('data'); // data is my table name.
           return $query->result();
        }
      }
?&gt;


/* * *
    View (firstview.php)
* * * /
&lt;html&gt;
  &lt;head&gt;&lt;/head>
  &lt;body&gt;
    &lt;?php echo $data; ?&gt;
  &lt;/body&gt;
&lt;/html&gt;

********************************************
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: data
Filename: views/firstview.php
Line Number: 14
********************************************


First, please use the code tag to enclose your code, it makes it much easier to read. I have done so in the quote above.

The loader class will extract elements of an array when that array is passed as the second parameter, From the User Guide:

"The second optional parameter can take an associative array or an object as input, which it runs through the PHP extract function to convert to variables that can be used in your view files. Again, read the Views page to learn how this might be useful."

This means that the array: $data will NOT be visible to the view, but the elements of that array will be visible. So your view should reference those elements:
Code:
/* * *
    View (firstview.php)
* * * /
&lt;html&gt;
  &lt;head&gt;&lt;/head>
  &lt;body&gt;
    &lt;?php echo $query; ?&gt;
  &lt;/body&gt;
&lt;/html&gt;

Because you are returning the query result:
Code:
...
        function tbl_data() {
           $query = $this->db->get('data'); // data is my table name.
           return $query->result();
        }
...
From the User Guide;
"This function returns the query result as an array of objects, or an empty array on failure. Typically you'll use this in a foreach loop, like this:
Code:
$query = $this->db->query("YOUR QUERY");

foreach ($query->result() as $row)
{
   echo $row->title;
   echo $row->name;
   echo $row->body;
}

Your view must reference the query result using the correct syntax, so your view file might be something like this:
Code:
&lt;html&gt;
  &lt;head&gt;&lt;/head>
  &lt;body&gt;
&lt;?php
foreach ($query as $row)
{
echo $row->title;
echo $row->name;
}
?&gt;
  &lt;/body&gt;
&lt;/html&gt;
#4

[eluser]cupidleomanoj[/eluser]
Hi noctrum, thanks for your reply.
#5

[eluser]cupidleomanoj[/eluser]
hi TWP, thanks for your reply. I tried with your code. but still I have a doubt. what you have given works even if i delete the php code from view file.
*************************
&lt;html&gt;
&lt;head&gt;&lt;/head>
&lt;body&gt;
&lt;?php
foreach ($query as $row)
{
echo $row->title;
echo $row->name;
}
?&gt;
&lt;/body&gt;
&lt;/html&gt;
************************
since you echo it in the controller file, the values from the DB gets executed in the query and gets printed.
can you please explain how to load the view from controller to display the values from the model.

please explain with this concept,
$this->load->view('viewname');

thanks in advance........
#6

[eluser]TWP Marketing[/eluser]
[quote author="cupidleomanoj" date="1303952841"]hi TWP, thanks for your reply. I tried with your code.
since you echo it in the controller file, the values from the DB gets executed in the query and gets printed.
can you please explain how to load the view from controller to display the values from the model.

please explain with this concept,
$this->load->view('viewname');

thanks in advance........[/quote]

cupid,
There should be no use of "echo" in the controller (did I understand your statement correctly?).

when loading a view (from the controller) you want to pass data using the second parameter:
Code:
...
$this->load->view('viewname',$some_data); //<-- see the User Guide for the full list of parameters on load->view() http://ellislab.com/codeigniter/user-guide/general/views.html
...
The var, $some_data, is usually an array. That array will be opened up by the loader and only the individual elements will be visible in the view. So in the controller:
Code:
...
$some_data = array( 'name' => 'fred', $title => 'owner' );
$this->load->view('viewname',$some_data);
...
in the view:
Code:
...
echo $name;
echo $title;
...




Theme © iAndrew 2016 - Forum software by © MyBB