0

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.

5
  • I also tried foreach($query as $row) instead of foreach($data as $row) in view but not working still. Commented May 11, 2017 at 11:24
  • Error is about Backmanage.php controller file. You didn't show that one nor show us URL that you are trying to access along with routes.php file code. Commented May 11, 2017 at 11:45
  • $route['default_controller'] = 'backmanage'; $route['404_override'] = ''; $route['translate_uri_dashes'] = FALSE; Commented May 12, 2017 at 16:46
  • I have also tried to pass simple values directly from the controller. I changed $data['query'] to $data = "hello" and tried to echo it on the view but it shows nothing. After tried echo var_dump($data); shown NULL. Its not sending data from controller to the view. Commented May 12, 2017 at 16:49
  • Show us Backmanage.php controller. Commented May 12, 2017 at 17:31

3 Answers 3

3

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

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

Comments

1

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

1 Comment

thanks for the answer but I already tried putting $query in foreach as per my earlier comment above
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.