I have this notice:
Notice: Undefined offset: 1 in D:\serveurs\data\localweb\alexa
traffic.php on line 14
the line 14 is this:
$usrank = ($rankus[1]) ? $rankus[1] : 0;
How do I fix this?
Here is my code:
<?php
$source = file_get_contents('http://data.alexa.com/data?cli=10&dat=snbamz&url=linuxplained.com');
//Alexa Rank
preg_match('/\<popularity url\="(.*?)" text\="([0-9]+)" source\="panel"\/\>/si', $source, $matches);
$aresult = ($matches[2]) ? $matches[2] : 0;
//Alexa Sites Linking in
preg_match('/\<linksin num\="([0-9]+)"\/\>/si', $source, $asli);
$alinksin = ($asli[1]) ? $asli[1] : 0;
//Alexa US Rank
preg_match('/\<country code\="US" name\="United States" rank\="([0-9]+)"\/\>/si', $source, $rankus);
$usrank = ($rankus[1]) ? $rankus[1] : 0;
//Alexa Reach Rank
preg_match('/\<reach rank\="([0-9]+)"\/\>/si', $source, $reachr);
$areach = ($reachr[1]) ? $reachr[1] : 0;
?>
Use isset() to check to see if that value exists:
$usrank = (isset($rankus[1])) ? $rankus[1] : 0;
This error occurs because the array $rankus doesn't have a value at index 1. The easiest fix is by using isset to check whether the index exists before attempting to use it. So then your could would be:
$usrank = (isset($rankus[1])) ? $rankus[1] : 0;
This uses the ternary operator, which is equivalent to the following (easier to understand) code:
$usrank;
if (isset($rankus[1]) {
$usrank = $rankus[1];
} else {
$usrank = 0;
}
Hopefully you now understand why the problem occurs and how to fix this.
There are more problems in your code though. When you create the variables $aresult, $alinksin and $areach, you don't check whether the needed indexes exist either. You should probably do this in order to avoid more errors like the one you get at the moment.
Finally, I noticed you are trying to parse an XML document using regular expressions. That can go wrong in a lot of ways, and it's better to use a 'real' XML parser. Take a look at SimpleXML or one of the other XML libraries for PHP!
Related
Problem
I am getting a "Undefined index: node in include()" notice in Drupal for the below line of code. I've tried the below solution but I am still receiving the error. Any ideas?
Code
$url = drupal_lookup_path('alias', 'node/' . $related['node']->nid);
The solution I tried
isset($related['node']->nid) ? $related['node']->nid : "";
Question
Does anyone know why this error continues to occur?
The error suggests the $related array variable doesn't have a node index.
First, make sure you're retrieving the node object correctly.
Then, perhaps try it like this to avoid errors:
<?php
$url = '';
if (isset($related['node']) && is_object($related['node'])) {
$nid = $related['node']->nid;
$url = drupal_lookup_path('alias', "node/$nid");
}
How can I check that the $courseAreas array includes at least "ayrshire"
and "fife"?
I've tried the following code, but it does not work:
$courseAreas = array('ayrshire', 'fife', 'cheshire', 'lanarkshire');
$includesAyrshireAndFife = (count(array_intersect(array('ayrshire', 'fife'), $courseAreas)) >= 2 ? true : false);
Try putting the ? true : false outside the braces
$courseAreas = array('ayrshire', 'fife', 'cheshire', 'lanarkshire');
$includesAyrshireAndFife = (count(array_intersect(array('ayrshire', 'fife'), $courseAreas)) >= 2) ? true : false;
var_dump($includesAyrshireAndFife);
$courseAreas = array('ayrshire', 'stirlingshire', 'cheshire', 'lanarkshire');
$includesAyrshireAndFife = (count(array_intersect(array('ayrshire', 'fife'), $courseAreas)) >= 2) ? true : false;
var_dump($includesAyrshireAndFife);
Seems to work
But your original also seems to work perfectly well.... in what circumstances do you find that it fails?
$courseAreas = array('ayrshire', 'fife', 'cheshire', 'lanarkshire');
$includesAyrshireAndFife = count(array_intersect(array('ayrshire', 'fife'), $courseAreas)) > 1;
You don't even need tenary operator because with > it's already boolean expression.
Edit:
I've noticed that your code works too. I've just shorten it.
You can use in_array()
See :- http://www.w3schools.com/php/func_array_in_array.asp
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.
}
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];
}
i m sorry for my english, i m french, i need your help for one thing please :
It s about filtering in php sphinx
this my code :
$filtres= array();
if(isset($_POST['Pharmacie']) and $_POST['Pharmacie'] ="1" ){ $filtres[]= 1;}
if(isset($_POST['Autres']) and $_POST['Autres'] ="8" ){ $filtres[] = 8;}
$varfiltres = 'array('.implode(" , ",$filtres).')';
if($filtres != array()){
$sphinx->SetFilter('Type', array(implode(",",$filtres)));
}
i have error :
Warning: assert(): Assertion failed in \sphinxapi.php on line 850
in case only one variable isset (pharmacie or Autres) that work!
also if i do $sphinx->SetFilter('Type', array(1,8)) it work!
thanks for help.
SetFilter, takes an array directly. With:
$sphinx->SetFilter('Type', array(implode(",",$filtres)));
you are first converting the Array to a String, and then putting it in an new array. Don't do that :)
This is all that is needed:
$sphinx->SetFilter('Type', $filtres);