简体   繁体   中英

A PHP Error was encountered Severity: Notice Message: Undefined variable: result

A PHP Error was encountered Severity: Notice

Message: Undefined variable: result

Filename: user/messages.php

Line Number: 87

Backtrace:

File: E:\\xampp\\htdocs\\ci\\application\\views\\user\\messages.php Line: 87 Function: _error_handler

File: E:\\xampp\\htdocs\\ci\\application\\controllers\\users\\Dashboard.php Line: 12 Function: view

File: E:\\xampp\\htdocs\\ci\\index.php Line: 315 Function: require_once

here is my code Controller

      <?php defined('BASEPATH') OR exit('No direct script access allowed');
        class Bills extends CI_Controller{
       function __construct(){
      parent::__construct();
     }

     public function bill(){
     $id = $this->session->userdata('email');
     $this->load->database();
     $this->load->helper(array('form', 'url', 'html'));
     $this->load->model('fuser/Bill');
     $data['result'] = $this->Bill->bills();
     $this->load->view('user/messages',$data);
      }
      }
      ?>

**Model**

      <?php  
     class Bill extends CI_Model  
     {  
      function __construct()  
      {  
         // Call the Model constructor  
         parent::__construct();  
      }  
      //we will use the select function  
      public function bills($id)  
      {  
         //data is retrive from this query  

     $query = $this->db->get('invoices'); 
       return $query->result();   
      }  





      }  
     ?> 

**View**
      <div class="billing_tabs_text_part">
                <?php foreach($result as $row){?>
                  <p><?php echo $row['book_id']; }?></p>
                </div>

i am not sure which section is your problem so i suggest something how to find error

public function bill()
{
    echo 'ok'; // check controller work
    $id = $this->session->userdata('email');
    $this->load->database();
    echo 'ok1';
    $this->load->helper(array('form', 'url', 'html'));
    echo 'ok2';
    /*$this->load->model('fuser/Bill');
    $data['result'] = $this->Bill->bills();
    $this->load->view('user/messages', $data);*/
}

if this is print ok, ok1 , ok2 then this is query problem , if no print ok,ok1 then this is not query problem

in this case try to find error, if other ok then must print ok

public function bill()
{
    echo 'ok';
    /*$id = $this->session->userdata('email');
    $this->load->database();
    $this->load->helper(array('form', 'url', 'html'));
    $this->load->model('fuser/Bill');
    $data['result'] = $this->Bill->bills();
    $this->load->view('user/messages', $data);*/
}

use <?php echo $row->book_id; }?> <?php echo $row->book_id; }?> in view

because your data is object array.

You need to find where the problem is exactly

try to var_dump the results and see if you're actually getting any results and that result is not a null or false;

so before this line: $data['result'] = $this->Bill->bills();

try to

var_dump($this->Bill->bills());     
die();

if you're getting results try the other way using dummy content instead of $this->Bill->bills(); use :

$data['result'] = [0 => 1, 1 =>2]; and var_dump from the view

see if you're getting results, and in line where you have

  return $query->result();   

make it like this:

  return $query->result() ? : [];

What gives you $query->result(); try to print in model itself

不要返回,打印并检查结果,例如: var_dump($query->result());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM