Undefinet offset error - php

My code sometimes give code undefinet offset error.
Error:
[03-Sep-2015 13:06:44] NOTICE: "Undefined offset: 6"
File: /home/mdmeds/public_html/includes/pages/game/class.ShowBuildingsPage.php | Line: 111
and
[04-Sep-2015 17:38:57] NOTICE: "Undefined offset: 8"
File: /home/mdmeds/public_html/includes/pages/game/class.ShowBuildingsPage.php | Line: 111
This is the part of the code
$Element = $CurrentQueue[$QueueID - 2][0]; /**this give the error*/
$BuildEndTime = $CurrentQueue[$QueueID - 2][3];
unset($CurrentQueue[$QueueID - 1]);
$NewQueueArray = array();
foreach($CurrentQueue as $ID => $ListIDArray)
{
if ($ID < $QueueID - 1) {
$NewQueueArray[] = $ListIDArray;
} else {
if($Element == $ListIDArray[0] || empty($ListIDArray[0]))
continue;
$BuildEndTime += BuildFunctions::getBuildingTime($USER, $PLANET, $ListIDArray[0]);
$ListIDArray[3] = $BuildEndTime;
$NewQueueArray[] = $ListIDArray;
}
}
I read lot of articles about this kind of errors but i do not know how to fix my code. Can someone help me please ?

You are trying to play with indexes that you are not sure they even exist...
things like
$CurrentQueue[$QueueID - 2]
is a guess... Get to find another way..

In this piece of code $CurrentQueue[$QueueID - 2][0], the key is generated dynamically based on $QueueID. WHen you get the error, it means that the specified key is not available in the array $CurrentQueue.
To avoid such run time exceptions, you can do something like this
if (!empty($CurrentQueue[$QueueID - 2])) {
// the actual functionality goes here.
}

Related

Undefined Constant in PHP

We are getting a couple of notices in prestashop when customers add products to cart, namely the following:
Impossible to add the product to the cart.
textStatus: 'parsererror'
errorThrown: 'SyntaxError: Unexpected token < in JSON at position 0'
responseText:
Notice: Use of undefined constant id_customization_field_width - assumed
'id_customization_field_width' in
/home/public_html/override/controllers/front/CartController.php
on line 83
Notice: Use of undefined constant id_customization_field_height - assumed
'id_customization_field_height' in
/home/public_html/override/controllers/front/CartController.php
on line 83
The code (line 83) which this refers to is as follows:
/*add new customization cart*/
$id_customization = 0;
if ($this->_newCustomization && id_customization_field_width && id_customization_field_height) {
if ($this->context->customer->isLogged()) {
$id_adv = $this->context->cart->id_address_delivery;
} else {
$id_adv = 0;
}
Can anyone help with this? We can't seem to find where the issue lies.
what is:
id_customization_field_width
and
id_customization_field_height
is it possible that it is:
$id_customization_field_width && $id_customization_field_height
or
$this->id_customization_field_width && $this->id_customization_field_height
?

PHP Array breaking

This works on my test environment, but on my live server there is a later version of PHP which is throwing up an error and breaking my program
The code is
$oldFile = fopen("D:/ftpfolderreport/report/" . $last_file, "r");
while(!feof($oldFile))
{
$buffler = fgets($oldFile);
$bufflerArray = explode(",", $buffler);
$key = $bufflerArray[0];
$oldFileArray[$key] = $bufflerArray[1];
}
fclose($oldFile);
This line:
$oldFileArray[$key] = $bufflerArray[1];
Is throwing out this error
Notice: Undefined offset: 1 in D:\apps\wamp\www\Compliance2\compareFtpReports.php on line 57
I think this is to do with how I'm adding the $key variable inside the argument. I've tried it as ["$key"] and ['$key'] but it doesn't like it.
I have tried defining the key variable earlier in the program but still doesn't like it. I've been searching around online but can't find anything of help. Anyone any ideas?
Thanks,
Stephen.
add checks for empty
if (!empty($bufflerArray[1])) {
$key = $bufflerArray[0];
$oldFileArray[$key] = $bufflerArray[1];
}

CakePHP Notice (8) raised : Use of undefined constant inList - assumed 'inList'

Notice (8): Use of undefined constant inList - assumed 'inList' [CORE\Cake\Utility\ClassRegistry.php, line 168]
This notice has been bugging me for a while know, and I do not know how to fix it.. It was not really affecting my project earlier since its just a notice msg, but now, it is not letting me show an error message which I am trying to display to the user.
Iv got this function
public function validate_form(){
if($this->RequestHandler->isAjax()){
$this->request->data['Donor'][$this->params['form']['field']] = $this->params['form']['value'];
$this->Donor->set($this->data);
if($this->Donor->validates()){
$this->autoRender = FALSE;
}else{
$error = $this->Donor->validationErrors;
$this->set('error',$error[$this->params['form']['field']]);
}
}
}
The above is the action to which my post request submits to. Then it executes the following to display the error
if (error.length > 0) {
if ($('#name-notEmpty').length == 0) {
$('#DonorName').after('<div id="name-notEmpty" class="error-message">' + error + '</div>');
}
}else{
$('#name-notEmpty').remove();
}
The problem is that instead of the relevant error in my newly created div... I get that notice 8 from cake! Please if anyone knows why this is happening, I appreciate your aid on this one..
TLDR:
Do a project-wide find for 'inList' and find the spot where it either doesn't have quotes around it, or, if it's supposed to be a variable, is missing it's $.
Explanation:
You get that error when you try to use a PHP Constant that doesn't exist. Usually you're not actually TRYING to use a constant, but instead just forgot to wrap quotes around something or forgot to add the $ before a variable.
Examples:
$name = "Dave";
echo name; // <-- WOAH THERE, there is no Constant called name (missing $)
$people = array('Dave' => 'loves pizza');
echo $people[Dave]; // <-- WOAH THERE, no Constant called Dave (missing quotes)
Most likely somewhere else in your code you are using 'inList' as an array key but you don't have it quoted.
Example: $value = $myArray[inList];
It still works without quoting inList but it causes the notice message you're seeing.

FACEBOOK API CALL - PHP SDK FATAL ERROR

trying to get every post and comment from a facebook page, I made this function that should go through the pagination:
$req = $facebook->api("/" . $pagename . "/?fields=posts.fields(message,link,created_time,shares,likes,comments)");
function parcours_arbre($ab)
{
GLOBAL $facebook;
GLOBAL $pagename;
$next = create_request($ab['posts']['paging']['next']);
$next_req = $facebook->api($pagename.$next);
$ab_next = $next_req['data'];
$prev = create_request($ab['posts']['paging']['previous']);
$prev_req = $facebook->api($prev);
$ab_prev = $prev_req['data'];
if (empty($ab)) {
display_results($ab['posts']['data']);
} else {
parcours_arbre($ab_next);
parcours_arbre($ab_prev);
}
}
I unfortunately get the following error:
Notice: Undefined index: posts in /form.php on line 36
Notice: Undefined offset: 3 in /utils.php on line 20
Notice: Undefined offset: 4 in /utils.php on line 20
Fatal error: Uncaught GraphMethodException: Unsupported get request. thrown in /sdk/src/base_facebook.php on line 1271
Any idea how i could avoid it or what is going on? Would this go away if i use the "until" statement in my api request?
Thanks a lot,
To explain each error
the variable $ab which is an argument to the function, does not have a "posts" index. You should try to var_dump this variable so you can see what it actually looks like.
same as above
same as above
the api function takes 3. The path which should just be #pagename. The method ("GET" or "POST") most likely POST because GET is causing an error. The parameters, which should be array("fields" => "posts.fields(message,link,created_time,shares,likes,comments)")
I noticed that for next you have the code
$next_req = $facebook->api($pagename.$next);
but for previous you have
$prev_req = $facebook->api($prev);
Might want to look into this.

Select data from database and give error

I want get from database table row name but in it i get error. ($query->code_airline => is other query as select from database table row)
code:
<?=$this->db->get_where('ticket_code', array( 'code' => $query->code_airline ))->row()->name?>
error:
A PHP Error was encountered Severity: Notice Message: Trying
to get property of non-object Filename: core/Loader.php(679) :
eval()'d code Line Number: 48
If want use it as:
<?php //This is line 49
$ca = echo $query->code_airline;
$query_tc = $this->db->get_where('ticket_code', array( 'code' => $ca ))->row();
echo $query_tc->name;
?>
have this error:
Parse error: syntax error, unexpected T_ECHO in
D:\xampp\htdocs\system\core\Loader.php(679) : eval()'d code on line 49
How can fix it?
Update:
i use as:
<?php
$ca = $query->code_airline;
$query_tc = $this->db->get_where('ticket_code',array('code'=>$ca));
$row = $query_tc->row(); //This is line 52
echo $row->name;
?>
i have this error with above php code:
A PHP Error was encountered Severity: Notice Message: Trying
to get property of non-object Filename: core/Loader.php(679) :
eval()'d code Line Number: 52
You don't ECHO in a variable assignment
$ca = $query->code_airline; //no echo here!
Also, I believe you're doing something wrong in calling $query->code_airline, but you didn't provide your code so I'm just guessing.
Suggestion: chaining is a nice feature but can be confusing, especially if written as one-liner. First of all, write it in full, you can "compact" your code later if you feel so; keep in mind readability and maintenability also:
$query_tc = $this->db->get_where('ticket_code',array('code'=>$ca));
// $ca is the value assigned above. Make sure you're calling $query->code_airline correctly, as per my suggestion.
$row = $query_tc->row();
echo $row->name;
UPDATE:
you might want to be sure that there are actually any result.
$query_tc = $this->db->get_where('ticket_code',array('code'=>$ca));
// $ca is the value assigned above. Make sure you're calling $query->code_airline correctly, as per my suggestion.
if ($query_tc->num_rows() > 0)
{
$row = $query_tc->row();
echo $row->name;
}
else
{
//do something else
}

Categories