I've got an array, but when I try to use it, I get the Undefined variable notice.
Here's the relevant lines:
$varEvents = array();
...
if ($selectedResult) {
while ($row = mysql_fetch_assoc($selectedResult)) {
array_push($varEvents, $row['eventID']);
}
mysql_free_result($selectedResult);
}
...
print_r($varEvents);
if (is_array($varEvents)) {
if (count($varEvents) > 0) {
if (in_array($id, $varEvents)) {
$varRegistered = 1;
}
}
unset($varEvents);
}
and the result shows as:
Array ( [0] => 4 )
Notice: Undefined variable: varEvents in /home/.../www/registration.php on line 143
Notice: Undefined variable: varEvents in /home/.../www/registration.php on line 145
line 143: print_r($varEvents);
line 145: if (is_array($varEvents)) {
All relevant lines are in the same loop and I do get most of the results I expect, except $varRegistered never changes to 1 and that messes up my result.
It is most likely because of this line:
unset($varEvents);
You are unsetting the variable within the loop and next iterations don't find it again.
Related
I have the following php code that is looking for the last entry of test entry in a mysql query result.
It checks if the last entry is valid for a device or it tries to search the latest one in the for that type of test (or failing that leave it as untested). After that, it does the same for the second device in the same manner. However I get errors pointing to lines on second foreach loop.
if ($device1_valid) {
$Results_d1 = $History[$TestNo][$iter]['Results_d1'];
$Colour_d1 = Colour($Results_d1);
$Date_d1 = $History[$TestNo][$iter]['Date_'];
} else {
foreach ($History[$TestNo]['iter'] as $item) {
$device1_valid = $History[$TestNo][$item]['d1_valid'];
if ($sf1_valid) {
$Results_d1 = $History[$TestNo][$item]['Results_d1'];
$Colour_d1 = Colour($Results_d1);
$Date_d1 = $History[$TestNo][$item]['Date_'];
break;
} else {
$Results_d1 = "----";
$DateTime_d1 ="----";
$Colour_d1 = 'white';
}
}
}
unset($item);
if ($device2_valid) {
$Results_d2 = $History[$TestNo][$iter]['Results_d2'];
$Colour_d2 = Colour($Results_d2);
$Results_d2 = $History[$TestNo][$iter]['Results_d2'];
$Date_d2 = $History[$TestNo][$iter]['Date_'];
} else {
foreach ($History[$TestNo]['EntryNo'] as $item) {
$device2_valid = $History[$TestNo][$item]['d2_valid'];
if ($device2_valid) {
$Results_d2 = $History[$TestNo][$item]['Results_d2'];
$Colour_d2 = Colour($Results_d2);
$Date_d2 = $History[$TestNo][$item]['Date_'];
break;
} else {
$Results_d2 = "----";
$DateTime_d2 ="----";
$Colour_d2 = 'white';
}
}
This results in warnings for the second loop as such:
Notice: Undefined index: EntryNo in /server/filename.php on line 129
Warning: Invalid argument supplied for foreach() in /server/filename.php on line 129
Why is this error occurring and how will I be able to remove it? The query does result in correct data (which is displayed later but I don't understand why theses notifications and warning are happening. This only happens in the second foreach loop and not the first.
Edit:
$History[$TestNo] is a multidimensional array.... so vardump gives array(49) { [0]=> array(25) {....} [1]=> array(25) [2]=> array(25){...} etc. I call this function setting $EntryNo to 0.
vardump $History[$TestNo][$EntryNo] simply gives array(25) {....}
There are no warnings in the first loop but second loop it says the index is undefined. This is key reason why the other question identified as duplicate does not address my issue. The question is why is this occuring in the second foreach loop and how can I avoid this.
For
`Notice: Undefined index: EntryNo in /server/filename.php on line 129
Warning: Invalid argument supplied for foreach() in /server/filename.php on line 129'
It must be like this at foreach($History[$TestNo]['EntryNo'] as $item) There is no element in array $History[$TestNo] with with key EntryNo.
Would you please var_dump($History[$TestNo]) and check it ?
Notice: Undefined variable: Colour_sf2 in /server/filename.php on line 184
For this, You have not included enough code here but it must be because you have not defined $Colour_sf2 before use it in any function or condition.
I've been using this script in the past and was ok. I converted txt file to csv and have got some undefinded error.
function Importcsv($filename) { $row = 0;
$col = 0;
$handle = #fopen($filename, "r");
if ($handle)
{
while (($row = fgetcsv($handle, 4096)) !== false)
{
if (empty($fields))
{
$fields = $row;
continue;
}
foreach ($row as $k=>$value)
{
$results[$col][$fields[$k]] = $value;
}
$col++;
unset($row);
}
if (!feof($handle))
{
echo "Error: unexpected fgets() failn";
}
fclose($handle);
}
return $results; }
$filename = "tm_data.csv";
$csvArray = Importcsv($filename);
foreach ($csvArray as $row)
{
echo $row['CITY'];
}
Please let me know if you have any idea or discovered similar issues before.
Please see few error lines
Notice: Undefined offset: 386 in /Applications/XAMPP/xamppfiles/htdocs/tm_marketing/index.php on line 20
Notice: Undefined offset: 387 in /Applications/XAMPP/xamppfiles/htdocs/tm_marketing/index.php on line 20
Notice: Undefined offset: 388 in /Applications/XAMPP/xamppfiles/htdocs/tm_marketing/index.php on line 20
Notice: Undefined offset: 389 in /Applications/XAMPP/xamppfiles/htdocs/tm_marketing/index.php on line 20
Notice: Undefined offset: 390 in /Applications/XAMPP/xamppfiles/htdocs/tm_marketing/index.php on line 20
Thanks,
From the (lack of) information posted in the question I think line 20 is:
$results[$col][$fields[$k]] = $value;
It seems that the first row of your CSV file contains less columns than the data rows.
The first row (the column names) is stored in $fields and it has 386 columns. Some of the data rows contain more than 386 columns (391, it seems) and for $k starting with 386, $fields[$k] is undefined and triggers the notices you posted.
The solution is up to you. You can fix the CSV file by adding the missing column names or by removing the extra values from the offending row(s).
You can also modify the code to do one of the above (either generate some column names on-the-fly or ignore the extra values).
You should also check if the CSV file is properly encoded. Maybe there is no extra column and fgetcsv() incorrectly split the row. Check the separator, quote and escape characters used in the file and make sure you use the same values in the call to fgetcsv().
line numbber 82 is the if statement
$re_arr is an array containing 25 elements
if ($pass[0]==substr($re_arr[$i],0,1))
{
$pass1[0]=$re_arr[$i];
unset($re_arr[$i]);
$re_arr=array_values($re_arr);
}
what could be done to avoid the notice
Add a check if $re_arr[$i] exists.
if (!empty($re_arr[$i]) && $pass[0]==substr($re_arr[$i],0,1))
{
$pass1[0]=$re_arr[$i];
unset($re_arr[$i]);
$re_arr=array_values($re_arr);
}
I am trying to install a arcade script and keep getting these errors, can anyone help out?
Notice: Undefined index: do in C:\xampp\htdocs\meggiemoos\arcade\install\new.php on line 34
Notice: Use of undefined constant sql - assumed 'sql' in C:\xampp\htdocs\meggiemoos\arcade\install\new.php on line 34
Notice: Undefined index: do in C:\xampp\htdocs\meggiemoos\arcade\install\new.php on line 37
Notice: Use of undefined constant sql2 - assumed 'sql2' in C:\xampp\htdocs\meggiemoos\arcade\install\new.php on line 37
This is line 34 to 38
<? if ($_GET['do'] == sql) {
include ('sql1.php');
}
else if ($_GET['do'] == sql2) {
include ('sql2.php');
I have tried if isset($_GET but I might have done it wrong as of the closed bracket { for next line, I was told to add another bracket as well but never told where.
Thanks for your help :)
Try
if (isset($_GET['do']) && $_GET['do'] == 'sql')
sql by itself is nothing to PHP. You need to have it as a string or variable, as follows:
<? if ($_GET['do'] == "sql") {
or
$var = "sql";
<? if ($_GET['do'] == $var) {
As for the line 37 error, the server cannot find the GET variable named 'do', so you need to find out why it's not being set. You might consider adding this to earlier parts of your script:
if (!isset($_GET['do'])) {
exit("do variable was not set!");
}
else {
$do = $_GET['do'];
}
That way, you don't have to keep typing $_GET['do'], which can get tedious.
While populating an array with data from a SimpleXML call, PHP throws exception to what it believes as 'Undefined' keys, however, the output is actually correct.
$doc = new SimpleXmlElement($http_result, LIBXML_NOCDATA);
$result = array();
$x = 0;
foreach($doc->users->user as $item) {
$result['user'][$x]['id'] .= $item->id;
$result['user'][$x]['name'] .= $item->name;
$result['user'][$x]['email'] .= $item->email;
$x++;
}
print json_encode($result);
This actually outputs what I expect, i.e. {"user":[{"id":"4843977","name":"Test New User","email":"test#newuser.com"}]}
However, the following errors are also present, and I'm not totally sure why - this doesn't appear in 5.2.6 but does for 5.2.10
Notice: Undefined index: user in /var/vhosts/sys-dev/docs/file.php on line 36
Notice: Undefined offset: 0 in /var/vhosts/sys-dev/docs/file.php on line 36
Notice: Undefined index: id in /var/vhosts/sys-dev/docs/file.php on line 36
Notice: Undefined index: name in /var/vhosts/sys-dev/docs/file.php on line 37
Notice: Undefined index: email in /var/vhosts/sys-dev/docs/file.php on line 38
Notice: Undefined offset: 1 in /var/vhosts/sys-dev/docs/file.php on line 36
Notice: Undefined index: id in /var/vhosts/sys-dev/docs/file.php on line 36
Notice: Undefined index: name in /var/vhosts/sys-dev/docs/file.php on line 37
Notice: Undefined index: email in /var/vhosts/sys-dev/docs/file.php on line 38
Notice: Undefined offset: 2 in /var/vhosts/sys-dev/docs/file.php on line 36
Notice: Undefined index: id in /var/vhosts/sys-dev/docs/file.php on line 36
Notice: Undefined index: name in /var/vhosts/sys-dev/docs/file.php on line 37
Notice: Undefined index: email in /var/vhosts/sys-dev/docs/file.php on line 38
I think You must change just ".=" to "=" in lines:
$result['user'][$x]['id'] = $item->id;
$result['user'][$x]['name'] = $item->name;
$result['user'][$x]['email'] = $item->email;
You don't define what are $result['user'] and $result['user'][$x].
You need to instantiate them as array so you won't get that error.
$result['user'] = array();
foreach($doc->users->user as $item) {
$result['user'][$x] = array();
$x++;
}
For the undefined indexes in the fields, the problem is similar. You use ".=" when the variable doesn't exists yet. So you should instantiate it first with an empty string.
$result['user'][$x]['name'] = '';
You need to initialize the $result array first:
$result = array('user' => array());
And since you’re using the string concatenation and assignment operator .=, that would also apply to the $result['user'][$x] arrays:
foreach($doc->users->user as $item) {
$result['user'][$x] = array(
'id' => null,
'name' => null,
'email' => null
);
$result['user'][$x]['id'] .= $item->id;
$result['user'][$x]['name'] .= $item->name;
$result['user'][$x]['email'] .= $item->email;
$x++;
}
But that’s not necessary since you can also write it like this:
$result = array('user' => array());
foreach($doc->users->user as $item) {
$result['user'][] = array(
'id' => $item->id,
'name' => $item->name,
'email' => $item->email
);
}
Edit Since we elaborated that the attributes of $item are SimpleXMLElement objects too, $item->attr[0] is required to address the string value itself. Thus:
$result = array('user' => array());
foreach($doc->users->user as $item) {
$result['user'][] = array(
'id' => $item->id[0],
'name' => $item->name[0],
'email' => $item->email[0]
);
}
It happens, because you're not just setting the array values, but you're concatenating to the current value:
$result['user'][$x]['id'] .= $item->id;
This line means "take the current value of $result['user'][$x]['id'] and add $item->id to it". The notice is then thrown, because the current value is not yet existent.
Amend the code to this
$result['user'][$x]['id'] = $item->id;
and you should be safe. No idea though, why 5.2.6 is not throwing the errors, maybe you should check with the error_reporting setting in the php.ini.