codeigniter/php error undefine property in private function - php

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;

Related

Moodle : I Am Stuck in Getting Context id when i tried to insert image in database

require_once("$CFG->libdir/formslib.php");
class block_add_edit_form extends moodleform {
public function definition()
{
global $CFG,$DB;
$mform = $this->_form;
$mform->addElement('text', 'id');
$mform->setType('id', PARAM_INT);
$mform->addElement('editor', 'attachment',get_string('file', 'block_add'),);
$mform->setType('attachment', PARAM_RAW);
$this->add_action_buttons();
}
function validation($data, $files) {
return array();
}
This is my form where i assigning values, down below is the code where i want insert but getting this error
Coding error detected, it must be fixed by a programmer: Invalid context id specified context::instance_by_id()
Debug info:
Error code: codingerror
Stack trace:
line 5346 of \lib\accesslib.php: coding_exception thrown line 1776 of \lib\blocklib.php: call to context::instance_by_id() line 1478 of \lib\blocklib.php: call to block_manager->process_url_edit() line 1645 of \lib\pagelib.php: call to block_manager->process_url_actions() line 1110 of \lib\pagelib.php: call to moodle_page->starting_output() line 1391 of \lib\outputrenderers.php: call to moodle_page->set_state() line 179 of \my\index.php: call to core_renderer->header()
require_once (__DIR__ . '/../../config.php');
require_once('edit_form.php');
$mform = new block_add_edit_form();
global $DB;
if ($mform->is_cancelled()) {
print_r("1");
}else if ($fromform = $mform->get_data()) {
var_dump($mform->get_data());
$record = new stdClass();
$record->id = $fromform->id;
$record->attachment = $fromform->attachment;
$DB->insert_record('image', $record);
}
echo $OUTPUT->header();
$mform->display();
echo $OUTPUT->footer();

Undefined property: Pages::$course_model error

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']));

Function php argument

something is wrong in one of my functions. The others work fine.
The application has a MVC structure. Beginning with objects and MVC, I must ay.
Basically, I have a page (services.php) linking to:
echo"Plus d'informations...";
Function in the model page (model.php):
function get_NextServ($id)
{
global $bdd;
$id = (int)$id;
$req = $bdd->query('SELECT * FROM entreprises WHERE id= ?');
$req->execute(array($id));
return $req;
}
And the detailservices.php page:
try
{
if (isset($_GET['id']))
{
$id = intval($_GET['id']);
$billet = get_NextServ($id);
if (get_NextServ($id) < 1)
{
echo "No......";
} else
{
foreach ($billet as $data)
{
echo "<h2>" . $data['entreprise'] . "</h2>";
}
.....
I have the following errors:
Warning: Missing argument 1 for get_NextServ(), called in projects\myapp-v3\controllers\controllers.php on line 27 and defined in projects\myapp-v3\model\model.php on line 54
Notice: Undefined variable: id in projects\myapp-v3\model\model.php on line 57
Fatal error: Call to a member function execute() on a non-object in projects\myapp-v3\model\model.php on line 59
Thanks guys!

Undefined variable error in class

I have a class that running inside a form to talk to a database, however the variables I'm using are returning errors. I have searched google but I cannot get a clear answer to my problem. Here are the errors, I've probably missed something obvious...hopefully:
Notice: Undefined variable: beer_name in /home/carlton/public_html/PHPproject/models/beer.php on line 28
Notice: Undefined variable: beer_type in /home/carlton/public_html/PHPproject/models/beer.php on line 35
Notice: Undefined variable: beer_abv in /home/carlton/public_html/PHPproject/models/beer.php on line 42
Notice: Undefined variable: beer_rating in /home/carlton/public_html/PHPproject/models/beer.php on line 49
Notice: Undefined variable: BeerEditor in /home/carlton/public_html/PHPproject/forms/beeradded.php on line 38
Fatal error: Call to a member function addBeer() on a non-object in /home/carlton/public_html/PHPproject/forms/beeradded.php on line 38
Here is my class:
protected $beer_name; //varchar(45)
protected $beer_type; //varchar(45)
protected $beer_abv; //decimal(4,2) alcohol percentage ex. 06.50
protected $beer_rating; //char(10) 1 awful beer, 10 great beer
public function __construct($beer_name = null){
if ($beer_name !== null){
$this->setBeerName($beer_name);
}
//defaults
//$this->setBeerType($beer_type);
//$this->setBeerABV($beer_abv);
//$this->setBeerRating($beer_rating);
}
public function setBeerName(){
$this->beer_name = $beer_name;
}
public function getBeerName(){
return $this->beer_name;
}
public function setBeerType(){
$this->beer_type = $beer_type;
}
public function getBeerType(){
return $this->beer_type;
}
public function setBeerABV(){
$this->beer_abv = $beer_abv;
}
public function getBeerABV(){
return $this->beer_abv;
}
public function setBeerRating(){
$this->beer_rating = $beer_rating;
}
public function getBeerRating(){
return $this->beer_rating;
}
}
Here is my form:
<?php
session_start();
ini_set('display_errors', 1);
error_reporting(E_ALL); ini_set('display_errors', 1);
require "/home/carlton/public_html/PHPproject/allincludes.php";
//var_dump($_POST);
if ($_SESSION['logged_in']!="yes"){
header ("Location: unauth.php");
}
//if (count($_POST)>0){
//need to add in validation check here
//
//
//
//
//$validationErrors= 0;
//if(count($validationErrors == 0 )){
$form=$_POST;
$beer_name = $form['beer_name'];
$beer_type = $form['beer_type'];
$beer_abv = $form['beer_abv'];
$beer_rating = $form['beer_rating'];
// }
$new_beer = new Beer($beer_name);
$new_beer->setBeerType($beer_type);
$new_beer->setBeerABV($beer_abv);
$new_beer->setBeerRating($beer_rating);
$BeerEditor->addBeer($new_beer);
echo "Beer added.";
echo ' Back to add menu ';
//}
you forget to pass the argument, here:
public function setBeerName(){
$this->beer_name = $beer_name;
}
it should be:
public function setBeerName($beer_name){
$this->beer_name = $beer_name;
}
please check in function setBeerType, setBeerABV, setBeerRating too.

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

Categories