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

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; }?> 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

don't return , print and check the result
ex:
var_dump($query->result());

Related

Call to undefined method in Codeigniter 3

So i just want to make simple form to add, update, and delete some data(Name, id, and address). Add and delete works fine for me, but the update doesn't, codeigniter give me a error message :
A PHP Error was encountered
Severity: Error
Message: Call to undefined method Mahasiswa_model::getMahasiswa()
Filename: controllers/Mahasiswa.php
Line Number: 35
Backtrace:
And these is my code which i thought its related with the error.
This one file name is Mahasiswa.php
<?php
if (!defined('BASEPATH')) exit('no direcet script access allowed');
class Mahasiswa extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->database();
$this->load->helper('url');
$this->load->helper('form');
$this->load->model('Mahasiswa_model');
}
public function index() {
$data['mhs'] = $this->Mahasiswa_model->retrieve();
$this->load->view('Mahasiswa_view', $data);
}
public function form_tambah() {
$this->load->view('Tambah_view');
}
public function submit() {
$this->Mahasiswa_model->add($this->input->post('var'));
$data['submitted'] = TRUE;
$this->load->view('Tambah_view', $data);
}
function delete() {
$this->Mahasiswa_model->delete($this->uri->rsegment(3));
$this->index();
}
function form_update() {
$data['mhs'] = $this->Mahasiswa_model->getMahasiswa($this->uri->rsegment(3));
$this->load->view('update_view', $data);
}
function update() {
$this->Mahasiswa_model->update($this->input->post('old_nim'),
$this->input->post('var'));
$this->index();
}
}
?>
And this one is Mahasiswa_model.php
<?php
class Mahasiswa_model extends CI_Model {
function retrieve() {
$query = $this->db->get('mhs');
if($query->result()) {
foreach ($query->result() as $content) {
$data[] = array(
$content->nim,
$content->nama,
$content->alamat
);
}
return $data;
} else {
return FALSE;
}
}
function add($arg) {
$data = array (
'nim' => $arg[0],
'nama'=> $arg[1],
'alamat' => $arg[2],
);
$this->db->insert('mhs', $data);
}
function delete($id) {
$this->db->where('nim', $id);
$this->db->delete('mhs');
}
function update($id, $form) {
$data = array(
'nim' => $form[0],
'nama' =>$form[1],
'alamat' => $form[2],
);
}
}
?>
Maybe someone can help me find where mistake i made? Line 35 not clear enough for me
This line in your controller is the problem:
$this->Mahasiswa_model->getMahasiswa($this->uri->rsegment(3));
You're calling the getMahasiswa method that should be available in your model, but (assuming the code you posted for your model is complete) it's not. Your model only has the retrieve, add, delete and update methods declared.
you either need to create the getMahasiswa method in your model, or you need to call a different method from your controller.
if you want to call a function from same controller then simply write.
$data = $this->getMahasiswa($parameter1);
IMP : function must be present there in the same controller.

Currently Using CodeIgniter Framework i have an Error

A PHP Error was encountered
Severity: Notice
Message: Undefined property: Login::$U_Model
Filename: controllers/Login.php
Line Number: 24
Backtrace:
File: C:\xampp\htdocs\Cmsproject\application\controllers\Login.php
Line: 24
Function: _error_handler
File: C:\xampp\htdocs\Cmsproject\index.php
Line: 315
Function: require_once
$data=$this->U_Model->loginf($loginf['username'], $loginf['password']);
An uncaught Exception was encountered
Type: Error
Message: Call to a member function loginf() on null
Filename: C:\xampp\htdocs\Cmsproject\application\controllers\Login.php
Line Number: 24
Backtrace:
File: C:\xampp\htdocs\Cmsproject\index.php
Line: 315
Function: require_once
Model File Is Here:
<?php
class U_Model extends CI_Model
{
public function loginf($username,$pass){
$this->db->select('*');
$this->db->from('users');
$this->db->where('username',$username);
$this->db->where('password',$pass);
if($query=$this->db->get()){
return $query->row_array();
}else{
return false;
}
}
}
?>
Hope this will help you :
Your controller should be like this :
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Login extends CI_Controller {
public function __construct(){
parent::__construct();
//$this->load->model('User');
$this->load->helper('form');
$this->load->library('form_validation');
$this->load->helper('url');
$this->load->database();
$this->load->model('U_Model');
}
public function index(){
$this->load->view('header');
$this->load->view('login');
$this->load->view('footer');
}
/*added public before loginp*/
public function loginp(){
$username = $this->input->post('username');
$password = md5($this->input->post('password'));
/*md5 not recommended*/
$data = $this->U_Model->loginf($username, $password);
if( ! empty($data))
{
$this->session->set_userdata('sid',$data['id']);
$this->session->set_userdata('sname',$data['name']);
$this->session->set_userdata('username',$data['username']);
redirect('login/home');
}else{
$this->session->set_flashdata('emsg','Please Enter Your Correct Name And Password');
redirect('login/home');
}
}
public function home(){
$this->load->view('home');
}
}?>
Change your model like this
<?php
class U_Model extends CI_Model
{
public function loginf($username,$pass)
{
$this->db->where('username',$username);
$this->db->where('password',$pass);
$query = $this->db->get('users')
if($query->num_rows() > 0){
return $query->row_array();
}else{
return false;
}
}
}
?>
<?php
class U_Model extends CI_Model
{
public function loginf($username,$pass)
{
$this->db->where('username',$username);
$this->db->where('password',$pass);
$query = $this->db->get('users')
if($query->num_rows() > 0){
return $query->row_array();
}else{
return false;
}
}
}
?>

Fix Error Codeigniter : Call to a member function pilihkat() on a non-object

Error :
A PHP Error was encountered
Severity: Notice
Message: Undefined property: CI_DB_mysqli_result::$Input_model
Filename: controllers/Input_App.php
Line Number: 18
Backtrace:
File: C:\xampp\htdocs\admin_app\application\controllers\Input_App.php
Line: 18
Function: _error_handler
File: C:\xampp\htdocs\admin_app\index.php
Line: 315
Function: require_once
and
Fatal error: Call to a member function pilihkat() on a non-object in C:\xampp\htdocs\admin_app\application\controllers\Input_App.php on line 18
My controller
class Input_App extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->model('Input_model','input_app');
}
public function index()
{
$this->db->from('referensi_kategori');
$query = $this->db->get();
$load['pilihkategori'] = $query->Input_model->pilihkat()->result();
$this->load->view('input_view',$load);
}
Model (pilihkat())
public function pilihkat(){
$this->db->select('*');
$this->db->join('referensi_kategori','referensi_kategori.id_kategori = data_aplikasi.id_kategori','left');
$query = $this->db->get();
return $query->result();
}
View (Select dropdown from data_kategori)
<tr>
<td>Kategori</td>
<td><select name="data_kategori" class="form-control"required id="data_kategori" style="width:85%;">
<option value='0'>-- pilih kategori --</option>
<?php foreach ($pilihkategori as $kat) {
echo "<option value=".$kat->id_kategori.">".$kat->data_kategori."</option>";
}?>
</select></td>
</tr>
How to fix this error ?
Load the model like this
$this->load->model('Input_model','input_model'); //here input_model is alias name
$this->load->model('Input_app','input_app'); //here input_app is alias name
(or)
$this->load->model(array('Input_model','input_app')); // pass as array mutiple modal .in below i'm using this one
remove result() in controller while calling model because in model your returning result() so in controller no need to call it again.
$load['pilihkategori'] = Input_model->pilihkat();
Controller :
public function index()
{
$load['pilihkategori'] = Input_model->pilihkat();
$this->load->view('input_view',$load);
}
Model :
public function pilihkat()
{
$this->db->select('*');
$this->db->from('referensi_kategori');
$this->db->join('referensi_kategori','referensi_kategori.id_kategori = data_aplikasi.id_kategori','left');
$query = $this->db->get();
return $query->result();
}
In your controller need to change two things.
1.Load more than one models like this.
$this->load->model(array('Input_model','input_app'));
2.Remove result() Because you already done it in model.
public function index()
{
$load['pilihkategori'] = $query->Input_model->pilihkat();
$this->load->view('input_view',$load);
}

Codeigniter undefined variable: title , PHP error is encountered

I'm working on simple application consisting of simple form. My code really works fine in local environment. But when i uploaded it on a live server gives an error, unable to fetch database fields and i think there is an error in my model.
Here is my model class
class Model_get extends CI_Model
{
function getData($page) {
$query = $this->db->get_where('ci_tbl', array('page' => $page));
print_r($query->result());
return $query->result();
}
}
Here goes my view
<div id="content">
<?php
foreach ($results as $row) {
$title = $row->title;
$para1 = $row->para1;
$para2 = $row->para2;
}
echo heading($title, 1);
?>
<p><?php echo $title;?></p>
<p><?php echo $para1;?></p>
<p><?php echo $para2;?></p>
</div>
My controller goes as
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Site extends CI_Controller {
public function index()
{
$this->home();
}
public function home() {
$this->load->model('model_get');
$data['results'] = $this->model_get->getData('home');
$this->load->view('header');
$this->load->view('nav');
$this->load->view('main_content',$data);
$this->load->view('footer');
}
public function about() {
$this->load->model('model_get');
$data['results'] = $this->model_get->getData('about');
$this->load->view('header');
$this->load->view('nav');
$this->load->view('about_page',$data);
$this->load->view('footer');
}
Make sure you actually return the results of the query:
//print_r($query->result());
return $query->result();
At the moment you've commented out return $query->result(); and you're just printing the result. The controller calling the model isn't going to get that information and therefore isn't passing through to the view.
Also check that the database connection is correct if moving from local to a public environment.
Actually you had an error
foreach ($results as $row) {
$title=$ row->title;
^^^^
It should be
$title = $row->title;
^^^
You need to update your query within model as
class Model_get extends CI_Model {
function getData($page) {
$query = $this - > db - > get_where('ci_tbl', array('page' => $page));
return $query->result_array();
}
}

CodeIgniter - Undefined Property Error

I am trying to access all the bands in my table and print them out in a list, but when I run it I get this error:
Severity: Notice
Message: Undefined property: CI_Loader::$model_bands
Filename: views/band_view.php
Line Number: 16
band_view.php:
<h3>List of Bands:</h3>
<?php
$bands = $this->model_bands->getAllBands();
echo $bands;
?>
model_bands.php:
function getAllBands() {
$query = $this->db->query('SELECT band_name FROM bands');
return $query->result();
}
Could someone pleease tell me why it is doing this?
Why do you need to do this, the correct way is to use the model methods inside a controller, then passing it to the view:
public function controller_name()
{
$data = array();
$this->load->model('Model_bands'); // load the model
$bands = $this->model_bands->getAllBands(); // use the method
$data['bands'] = $bands; // put it inside a parent array
$this->load->view('view_name', $data); // load the gathered data into the view
}
And then use $bands (loop) in the view.
<h3>List of Bands:</h3>
<?php foreach($bands as $band): ?>
<p><?php echo $band->band_name; ?></p><br/>
<?php endforeach; ?>
Did you load your model in the Controller?
$this->load->model("model_bands");
You need to change your code like
Controller
public function AllBrands()
{
$data = array();
$this->load->model('model_bands'); // load the model
$bands = $this->model_bands->getAllBands(); // use the method
$data['bands'] = $bands; // put it inside a parent array
$this->load->view('band_view', $data); // load the gathered data into the view
}
Then View
<h3>List of Bands:</h3>
<?php foreach($bands as $band){ ?>
<p><?php echo $band->band_name; ?></p><br/>
<?php } ?>
Your Model is ok
function getAllBands() {
$query = $this->db->query('SELECT band_name FROM bands');
return $query->result();
}
You forgot to load the model on your controller:
//controller
function __construct()
{
$this->load->model('model_bands'); // load the model
}
BTW, why are you calling the model directly from your view? Should it be:
//model
$bands = $this->model_bands->getAllBands();
$this->load->view('band_view', array('bands' => $bands));
//view
<h3>List of Bands:</h3>
<?php echo $bands;?>

Categories