Web7
Web7

Reputation: 126

Everything is correct but codeigniter showing Message: Undefined variable: data

Please read the code below

Insertmodel.php

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Insertmodel extends CI_Model {

    public function insertdata($data)
    {
        $this->db->insert('page',$data);
    }
    public function getpagedata()
    {
        //$this->db->select('pname,purl,pub');
        //$this->db->from('page');
        //$this->load->database();

        $getquery = $this->db->get('page');
        return $getquery->result();
    }
}

Pagedatainsert.php

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

class Pagedatainsert extends CI_Controller {

    public function insertdata()
    {
        $this->load->helper('date');
        $this->load->model('insertmodel','tblselect');
        $data = array(
            'pname'=>$this->input->post('page-name'),
            'purl'=>$this->input->post('page-url'),
            'pcon'=>$this->input->post('page-content'),
            'pub'=>date('Y-m-d H:i:s')
        );
        $this->tblselect->insertdata($data);
    }
    public function gpdatacon()
    {
        $this->load->model('insertmodel');
        $data['query'] = $this->insertmodel->getpagedata();
        $this->load->view('backend/pages/pages',$data);
    }
}

pages.php (which is under directory backend/pages)

<?php foreach($data as $row){?>
<tr class="odd gradeX">
        <td><?= $row->pname; ?></td>
        <td><?= $row->purl; ?></td>
        <td><?= $row->pub; ?></td>
</tr>    
<?php } ?>

Here an error is occurred which says

A PHP Error was encountered
Severity: Notice
Message: Undefined variable: data
Filename: pages/pages.php
Line Number: 73
Backtrace:
File: D:\Ampps\www\customcms\application\views\backend\pages\pages.php
Line: 73
Function: _error_handler
File: D:\Ampps\www\customcms\application\controllers\Backmanage.php
Line: 26
Function: view
File: D:\Ampps\www\customcms\index.php
Line: 316
Function: require_once

My database name is customcms, table name is page.

Made lots of searching on google already but not found so much. Besides, I found a question like this on stackoverflow but that was not so much helpful in my case. I am working for entire day on this problem but it is not getting resolved.

Upvotes: 0

Views: 1178

Answers (3)

Web7
Web7

Reputation: 126

I tried it myself that was the issue with the URL I was calling. I made two pages similar in looks. and accidentally calling the wrong URL that was obvious not working.

Upvotes: 0

Shafiqul Islam
Shafiqul Islam

Reputation: 5690

change $data to $query cause you store all data in $data['query'] so you get all data in $qyery variable

<?php foreach($data as $row){?>
.............  
<?php } ?>

to

<?php foreach($query as $row){?>
............   
<?php } ?>

if not work then print_r use and check that value exit

 public function gpdatacon()
    {
        $this->load->model('insertmodel');
        $data['query'] = $this->insertmodel->getpagedata();
        print_r( $data['query']);
        die()
        $this->load->view('backend/pages/pages',$data);
    }

for more information

https://www.codeigniter.com/user_guide/general/views.html

Upvotes: 1

Mayank Pandeyz
Mayank Pandeyz

Reputation: 26258

Change it:

$this->load->view('backend/pages/pages',$data);

to

$this->load->view('backend/pages/pages',array('data' => $data));

or change it:

<?php foreach($data as $row){?>

to

<?php foreach($query as $row){?>

Explanation: The second parameter of view() function is an array like

view('viewname', array('data' => $data))

and on view you can access it by using $data. If you pass $data then you have to use by its index i.e. $query

Upvotes: 3

Related Questions