Undefined property: Pages::$course_model error - php

I am trying to create dynamic pages in codeigniter. I m new to codeigniter.Following is my controller (Pages.php) code:
if($this->form_validation->run() == false)
{
$this->new_course();
}
else
{
$data['course_title'] = $this->input->post('pagetitle', true);
$data['course_intro'] = $this->input->post('courseintro', true);
$data['created_at'] = date('Y-m-d h:i:sa');
$data['course_slug'] = $this->seoURL($data['course_title']);
$checkPage = $this->course_model->checkPage($data);
// is page exists
if($checkPage->num_rows() > 0 )
{
customeFlash('You have already created this page', 'alert-warning', 'pages/create_course');
}
This is my Model:
public function checkPage($data)
{
return $this->db->get_where('courses',array('course_title' => $data['$data']));
}
public function add_course($data)
{
return $this->db->insert('courses', $data);
}
I am getting an error:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Pages::$course_model
Filename: controllers/Pages.php
Line Number: 53
An uncaught Exception was encountered
Type: Error
Message: Call to a member function checkPage() on null
Filename: C:\xampp\htdocs\vedacg\application\controllers\Pages.php
Line Number: 53
I dont have solid programming background, can anyone help me with this?

Edit your controller 'Pages.php' using this
$this->load->model('course_model');
$checkPage = $this->course_model->checkPage($data);

First load the course_model.php in your controller
$this->load->model('course_model');
after that you can load
$checkPage = $this->course_model->checkPage($data);

Thank you all for the help... I was doing this worng in my model:
$this->db->get_where('courses', array('course_title'=>$data['$data']));
I corrected in this:
$this->db->get_where('courses', array('course_title'=>$data['course_title']));

Related

Why value of $id my projecct in codeigniter is NULL

I tried to find why my $id is still NULL, but in the other project it works
please help me to find the error
An uncaught Exception was encountered
Type: Error
> Message: Call to a member function get_pengajuan() on null
Filename: .\magang\application\controllers\Surat.php
Line Number: 40
Backtrace:
File: .\magang\index.php
Line: 315
Function: require_once
Line 40 is $data['pengajuan'] = $this->MPengajuan->get_pengajuan($id);
Controller:
public function edit($id)
{
$this->load->view("header");
$this->load->view("home/user");
$this->load->view("home/admin");
$this->load->view("topbar");
$this->load->view("admin/edit_surat");
$this->load->view("footer");
$data['pengajuan'] = $this->MPengajuan->get_pengajuan($id);
}
Model:
public function get_pengajuan($id) {
$query = $this->db->query("SELECT pengajuan.* , instansi_magang.nama_instansi FROM pengajuan,instansi_magang WHERE pengajuan.id_instansi=instansi_magang.id_instansi AND id_pengajuan='$id'");
return $query->result_array();
}
View:
<?php
foreach ($pengajuan as $pengajuan_item){
echo form_open_multipart('Surat/edit/'.$pengajuan_item['id_pengajuan']);
}
?>
I suggest your error is :
You don't have to use form_open_multipart . Because there is no file to upload.
Your controller must use lowercase in your view ('surat/edit/'.$pengajuan_item['id_pengajuan'])
I got solution
that i forgot to load Model in construct with
$this->load->model('MPengajuan');

codeigniter/php error undefine property in private function

I test new self(), $this both work fine but I want to know:
What difference between them will it cause a problem to me later when
using in difference situation?
Sencond how to get variable in private function load_user_record($rows)
I got error undefine property please help
private function load_user_record($rows)
{
$this->user_id = $rows->id;
$this->hashedPassword = $rows->user_pwd;
}
private function find_user_record($user_name)
{
$this->db->where('user_name',$user_name);
$query = $this->db->get('tbl_user');
$result = $query->result();
if( $query->num_rows() > 0 )
{
foreach($result as $rows):
/************************* what difference ******************************/
/*
* $item = new self();
* $item->load_user_record($rows);
* $user_result[] = $item;
*/
$this->load_user_record($rows);
$user_result[] = $this;
/************************* End HERE ******************************/
endforeach;
return $user_result;
}
return FALSE;
}
public function check_user_exist($user_name, $password)
{
$find_user = $this->find_user_record($user_name);
if($find_user !== FALSE)
{
/********************* HERE error undefine property*********************/
foreach($find_user as $user):
/* $user_id = $user->user_id;
* $hashed_password = $user->hashed_password;
* using this 2 variable above work
*/
$this->load_user_record($user);
// using this load_user_record not work
endforeach;
/************************* End HERE ******************************/
if( password_verify( $password, $hashedPassword )) {
if (password_needs_rehash($hashedPassword, PASSWORD_DEFAULT)) {
$time = date("Y-m-d H:i:s");
$newHashedPassword = password_hash($password, PASSWORD_DEFAULT);
$this->db->where('id',$user_id)
->set(array(
'user_pwd' => $newHashedPassword,
'last_login' => $time
));
if(!$this->db->update('tbl_user'))
return FALSE;
}
return TRUE;
}
return FALSE;
}
return FALSE;
}
Error happen
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Login::$id
Filename: core/Model.php
Line Number: 77
Backtrace:
File:
C:\wamp\www\CodeIgniter_Project\application\models\backend\Login_model.php
Line: 10 Function: __get
File:
C:\wamp\www\CodeIgniter_Project\application\models\backend\Login_model.php
Line: 49 Function: load_user_record
File:
C:\wamp\www\CodeIgniter_Project\application\controllers\backend\Login.php
Line: 27 Function: check_user_exist
File: C:\wamp\www\CodeIgniter_Project\index.php Line: 316 Function:
require_once
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Login::$user_pwd
Filename: core/Model.php
Line Number: 77
Backtrace:
File:
C:\wamp\www\CodeIgniter_Project\application\models\backend\Login_model.php
Line: 11 Function: __get
File:
C:\wamp\www\CodeIgniter_Project\application\models\backend\Login_model.php
Line: 49 Function: load_user_record
File:
C:\wamp\www\CodeIgniter_Project\application\controllers\backend\Login.php
Line: 27 Function: check_user_exist
File: C:\wamp\www\CodeIgniter_Project\index.php Line: 316 Function:
require_once
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: hashedPassword
Filename: backend/Login_model.php
Line Number: 53
Backtrace:
File:
C:\wamp\www\CodeIgniter_Project\application\models\backend\Login_model.php
Line: 53 Function: _error_handler
File:
C:\wamp\www\CodeIgniter_Project\application\controllers\backend\Login.php
Line: 27 Function: check_user_exist
File: C:\wamp\www\CodeIgniter_Project\index.php Line: 316 Function:
require_once
and is my code right in OOP? Don't mind me if I'm wrong in OOP it my 1st code in OOP.
In the loop you used the same variable $find_user. Change the code to:
foreach($find_user as $user):
$this->load_user_record($user);
endforeach;

Undefined property: stdClass in my codeigniter controller

This is my controller function
public function check_orgn_mail()
{
$this->load->model('registrationmodel', '', TRUE);
$omail = $this->input->post('mailid');
$mailresult = $this->registrationmodel->check_org_mail();
$smil = explode("#", $omail);
$registerCompanyID = null;
if ($mailresult)
{
foreach ($mailresult as $row)
{
$registerCompanyID = $row->id;
$cmail = explode("#", $row->email);
$acmail[] = $cmail[1];
}
if (in_array($smil[1], $acmail))
{
return TRUE;
}
}
$this->form_validation->set_message('check_orgn_mail', 'Invalid Organization Mail id.');
return false;
}
This is the function in my model
function check_org_mail()
{
$this->db->select('email','id');
$this->db->from('customers');
$this->db->where('status', 1);
$query_emai = $this->db->get();
return $query_emai->result();
}
I am getting this error
Severity: Notice
Message: Undefined property: stdClass::$id Filename:
controllers/registration.php
I tried some trial and error but not working. Please help me with this. Previously it was working fine but when I checked with the different mail id, it started giving this error.
change your select to this:
$this->db->select('email, id');
You need to use
$this->db->select('email, id');
All columns are supposed be a single string argument separated by commas.
You are providing multiple arguments.

PHP CodeIgniter: Trying to get property of non-object

Everything seems to work just fine but I keep getting the following error:
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: models/ordermodel.php
Line Number: 24
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: models/ordermodel.php
Line Number: 31
================================================================
In the view i just echo $company_name
Controller:
$city = $this->ordermodel->get_city($order_reference);
$customerCompanyName = $this->ordermodel->get_company_name($order_reference);
$data['company_name'] = $customerCompanyName;
Model:
function get_city($ordernumber) {
$this->db->where('order_number', $ordernumber);
$city = $this->db->get('order');
return $city->row()->city;
}
function get_company_name($ordernumber) {
$this->db->where('order_number', $ordernumber);
$companyname = $this->db->get('order');
return $companyname->row()->company_name;
}
Use therresult() method, it should elimiate all of your errors
$row = $city->result(); return $row->city;
Seem that your query has no results, try:
function get_company_name($ordernumber) {
$this->db->where('order_number', $ordernumber);
$companyname = $this->db->get('order');
if ($companyname->num_rows() > 0) {
return $companyname->row()->company_name;
}
else {
return '';
}
}

Illegal string offset error in codeigniter view

im doing a PHP project using codeigniter 2.1.2 , using mySql database on WAMP, i have stumbled upon the following problem:
Here is my model
function loadUserData() {
$sql = "SELECT username FROM user WHERE username = ?";
$query = $this -> db -> query($sql, $this->session->userdata('username'));
if ($query -> num_rows() > 0) {
foreach ($query->result() as $row) {
$data = $row;
}
return $data;
}
}
The controller
function profile_access() {
if ($this -> session -> userdata('is_logged_in')) {
$this->load-> model('profile_model');
$uData['rows']=$this->profile_model->loadUserData();
$this->load->view('profile_view',$uData);
} else {
echo "<script type=\"text/javascript\"> alert(\"You need to provide your credentials first.\");</script>";
redirect('login_controller/index');
}
}
and the view
<?php
foreach($rows as $r){
echo print_r($r);
echo $r[username]->username; //line no. 81 in my code
}
?>
the print_r($r) from view gives me the following errors:
alphaomega1
A PHP Error was encountered
Severity: Notice
Message: Use of undefined constant username - assumed 'username'
Filename: views/profile_view.php
Line Number: 81
A PHP Error was encountered
Severity: Warning
Message: Illegal string offset 'username'
Filename: views/profile_view.php
Line Number: 81
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: views/profile_view.php
Line Number: 81
I expect the above code to return only "alphaomega" which is the username in the database. it is however displayed, but i dont know why i'm getting those errors. would appreciate some help with this. thanks.
In your view:
foreach($rows as $r){
echo $r->username;
}
In your model:
function loadUserData() {
$sql = "SELECT username FROM user WHERE username = ?";
$query = $this->db->query($sql, $this->session->userdata('username'));
return $query->result();
}

Categories