Undefined variable error in class - php

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.

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

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;

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!

Error When Calling Function

I searched forever trying to find an answer, but was ultimately stumped. I've been writing code to allow multiple bots to connect to a chat box. I wrote all the main code and checked it over to make sure it was all okay. Then when I got to calling the function needed to make it work, it gave me an error saying:
Notice: Undefined variable: ip in C:\wamp\www\BotRaid.php on line 40
And also an error saying:
Fatal Error: Cannot access empty property in C:\wamp\www\BotRaid.php
on line 40
( Also a screenshot here: http://prntscr.com/ckz55 )
<?php
date_default_timezone_set("UCT");
declare(ticks=1);
set_time_limit(0);
class BotRaid
{
public $ip="174.36.242.26";
public $port=10038;
public $soc = null;
public $packet = array();
##############################
# You can edit below this #
##############################
public $roomid="155470742";
public $userid = "606657406";
public $k = "2485599605";
public $name="";
public $avatar=;
public $homepage="";
##############################
# Stop editing #
##############################
public function retry()
{
$this->connect($this->$ip,$this->$port); //Line 40, where I'm getting the error now.
$this->join($this->$roomid);
while($this->read()!="DIED");
}
public function connect($ip, $port)
{
if($this->$soc!=null) socket_close($this->$soc);
$soc = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
if(!$this->$soc)$this->port();
if(!socket_connect($this->$soc,$this->$ip,$this->$port))$this->port();
}
public function port()
{
$this->$port++;
if($this->$port>10038) $this->$port=10038;
$this->retry();
}
public function join($roomid)
{
$this->send('<y m="1" />');
$this->read();
$this->send('<j2 q="1" y="'.$this->$packet['y']['i'].'" k="'.$this->$k.'" k3="0" z="12" p="0" c"'.$roomid.'" f="0" u="'.$this->$userid.'" d0="0" n="'.$this->$name.'" a="'.$this->$avatar.'" h="'.$this->$homepage.'" v="0" />');
$this->port();
$this->$roomid;
}
public function send($msg)
{
echo "\n Successfully connected.";
socket_write($this->$soc, $this->$msg."\0", strlen($this->$msg)+1);
}
public function read($parse=true)
{
$res = rtrim(socket_read($this->$soc, 4096));
echo "\nSuccessfully connected.";
if(strpos(strtolower($res), "Failed"))$this->port();
if(!$res) return "DIED";
$this->lastPacket = $res;
if($res{strlen($res)-1}!='>') {$res.=$this->read(false);}
if($parse)$this->parse($res);
return $res;
}
public function parse($packer)
{
$packet=str_replace('+','#più#',str_replace(' ="',' #=#"',$packet));
if(substr_count($packet,'>')>1) $packet = explode('/>',$packet);
foreach((Array)$packet as $p) {
$p = trim($p);
if(strlen($p)<5) return;
$type = trim(strtolower(substr($p,1,strpos($p.' ',' '))));
$p = trim(str_replace("<$type",'',str_replace('/>','',$p)));
parse_str(str_replace('"','',str_replace('" ','&',str_replace('="','=',str_replace('&','__38',$p)))),$this->packet[$type]);
foreach($this->packet[$type] as $k=>$v) {
$this->packet[$type][$k] = str_replace('#più#','+',str_replace('#=#','=',str_replace('__38','&',$v)));
}
}
}
}
$bot = new BotRaid; //This is where I had the error originally
$bot->retry();
?>
Line 40 is below the "Stop Editing" line. Anyone have any suggestions? Or perhaps need me to clear some things up?
You are accessing the properties of the class incorrectly.
The line:
$this->connect($this->$ip,$this->$port);
Should be:
$this->connect($this->ip, $this->port);
Since there was no local variable called $ip, your expression was evaluating to $this-> when trying to access the property since PHP lets you access properties and functions using variables.
For example, this would work:
$ip = 'ip';
$theIp = $this->$ip; // evaluates to $this->ip
// or a function call
$method = 'someFunction';
$value = $this->$method(); // evaluates to $this->someFunction();
You will have to change all the occurrences of $this->$foo with $this->foo since you used that notation throughout the class.
As noted in the comment by #Aatch, see the docs on variable variables for further explanation. But that is what you were running into accidentally.

Categories