Undefined Offset error when read a file into table - php

I've got Undefined Offset error, but I don't know what's wrong.
Here's the code
<?php
$documentRoot = $_SERVER['DOCUMENT_ROOT'];
$fileName = "$documentRoot/Tutorials/PHP/Assignments/Assignment 3/data/quote.txt";
$filePointer = fopen($fileName, 'r');
$lineCounter = 0;
$display = "";
while(true)
{
$line = fgets($filePointer);
list($firstName, $lastName, $contactMethod, $phoneMail, $resideCity, $comments) = explode("|", $line);
if (!isset($comments))
{
$comments = "";
}
$lineCounter++;
if(feof($filePointer))
{
break;
}
if($lineCounter % 2 == 0)
{
$style = "style = 'background-color:white';";
}
else
{
$style = "style = 'background-color:lightgray';";
}
//Write to table
print"<tr $style>";
print"<td>$firstName</td>";
print"<td>$lastName</td>";
print"<td>$contactMethod</td>";
print"<td>$phoneMail</td>";
print"<td>$resideCity</td>";
print"<td>$comments</td>";
print"</tr>";
}
fclose($filePointer);
?>
I added:
if (!isset($comments))
{
$comments = "";
}
Because I assume the offset error appears because I didn't type in anything in comments. However, I still get this error.
Please help.
Here's the error message:
Notice: Undefined offset: 5 in /Users/Lio/Documents/Eligio's/Tutorials/PHP/Assignments/Assignment 3/quotes.php on line 32
Notice: Undefined offset: 4 in /Users/Lio/Documents/Eligio's/Tutorials/PHP/Assignments/Assignment 3/quotes.php on line 32
Notice: Undefined offset: 3 in /Users/Lio/Documents/Eligio's/Tutorials/PHP/Assignments/Assignment 3/quotes.php on line 32
Notice: Undefined offset: 2 in /Users/Lio/Documents/Eligio's/Tutorials/PHP/Assignments/Assignment 3/quotes.php on line 32
Notice: Undefined offset: 1 in /Users/Lio/Documents/Eligio's/Tutorials/PHP/Assignments/Assignment 3/quotes.php on line 32

The problem is before your isset... so when you do explode and pass results to list... that expects 6 parametes only receive 5 or less and you get the error.
I suggest the following replacement:
$line = fgets($filePointer);
$data=explode("|", $line);
while(count($data)<6) $data[]="";
list($firstName, $lastName, $contactMethod, $phoneMail,
$resideCity, $comments) = $data;
That will add enough data for list to set vars.
and should work!
Happy coding!

Related

Getting Error Illegal string offset After upgrade of PHP

Warning [2] Illegal string offset 'title' - Line: 66
if(!isset($params['title'])) {
$params['title'] = "Hidden Content";
}
and
Warning [2] Illegal string offset 'title' - Line: 3
$return = $content;
}
eval("\$return = \"".$templates->get("lock_wrapper")."\";");
return $return;
}
Any Solution is appreciated

What is wrong with the lines? (php)

I get these error from nowhere... What is wrong?
Warning: Division by zero in
/home//public_html/wp-content/plugins//includes/classes/controller/SearchPageController.php
on line 60
Warning: Invalid argument supplied for foreach() in
/home/vineanimals/public_html/wp-content/plugins/*/includes/classes/controller/ImportPageController.php
on line 70
Warning: array_merge(): Argument #1 is not an array in
/home//public_html/wp-content/plugins//includes/classes/controller/AImportPageController.php
on line 75
line codes:
line 60: $last = ceil($load_products_result['total'] / $load_products_result['per_page']);
line 70-75:
foreach ($product['sku_products']['variations'] as $var) {
if (isset($var['image'])) {
$product['all_images'][] = $var['image'];
}
}
$product['all_images'] = array_merge($product['all_images'], $this->woocommerce_model->get_images_from_description($product['description']));
}
A) Check your SQL query or Array feed
B) Then change
$last = ceil($load_products_result['total'] / $load_products_result['per_page']);
to:
$last = ( isset($load_products_result['per_page']) ? ceil($load_products_result['total'] / $load_products_result['per_page']) : 0)
C) and change the foreach as below:
if (is_array($product['sku_products']['variations'])){
foreach ($product['sku_products']['variations'] as $var) {
if (isset($var['image'])) {
$product['all_images'][] = $var['image'];
}
}
if (isset($product['description']) && is_array($product['all_images']) && is_array($this->woocommerce_model->get_images_from_description($product['description'])) )
$product['all_images'] = array_merge($product['all_images'], $this->woocommerce_model->get_images_from_description($product['description']));
}

Undefined offset error in for loop

This code helps me find the minimum of certain sums, so I set a function as the one below:
function GetMin($s)
{
for($j=1;$j<=count($s)-1;$j++)
{
$min=$s[1];
if($s[$j] < $s[$j+1] && $s[$j] < $min)
{
$min=$s[$j];
}
else {continue;}
}
echo $min;
}
$enc=".129.25.24.154.546.214.142.254.256";
$ar=explode(".",$enc);
for($i=0;$i<count($ar)-1;$i+=3)
{
$s[$i+1]=$ar[$i+1]+$ar[$i+2]+$ar[$i+3];
echo $s[$i+1]."<br>";
}
GetMin($s)
The code should work this way:
Calculates the sum of every 3 terms of the array and store it in a term of another array called "s", the code is working well, until I try to find the minimum, it prints the minimum but with the error below:
Notice: Undefined offset: 2 in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\decrypt.php on line 9 Notice: Undefined offset: 2 in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\decrypt.php on line 9 Notice: Undefined offset: 3 in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\decrypt.php on line 9 178
It looks like you're trying to get the lowest value in an array of values, so I've changed your GetMin() function to work with the non-consecutively indexed array that you create prior to calling GetMin(). This should work for you:
<?php
function GetMin($s){
$min = NULL;
foreach($s as $v){
if(is_null($min)){
$min = $v;
} elseif($v < $min){
$min = $v;
}
}
return $min;
}
$enc = ".129.25.24.154.546.214.142.254.256";
$ar = explode(".",$enc);
$ar_count = count($ar);
for($i=0;$i<$ar_count-1;$i+=3)
{
$s[$i+1]=$ar[$i+1]+$ar[$i+2]+$ar[$i+3];
echo $s[$i+1]."<br>";
}
echo GetMin($s);

POST method array

I have a problem with creating array on POST method. Code:
<input class='form-control' name='ern[".$row['ID']."][value]' type='text' placeholder='€'>
$comp = isset($_GET['comp']) ? $_GET['comp'] : false;
$comp = ($comp !== false) ? preg_replace('/[^A-Za-z0-9]/', '', $comp) : false;
$ern = isset($_POST['ern']) ? $_POST['ern'] : false;
$ern = ($ern !== false) ? preg_replace('/[^0-9]/', '', $ern) : false;
echo $ern[$comp]['value'];
Geting errors:
Notice: Array to string conversion in
D:\Tinklapis\Graffiz-CMS\php\update_order.php on line 7
Warning: Illegal string offset 'value' in D:\Tinklapis\Graffiz-CMS\php\update_order.php on line 9
Notice: Uninitialized string offset: 0 in D:\Tinklapis\Graffiz-CMS\php\update_order.php on line 9
And nothing value printed.

Cake PHP: undefined offset error

i am newbie with Cake PHP. I am trying to run application that I have downloaded recently but it doesn't work.
My debug.log file says:
2012-07-31 12:31:47 Debug: Notice (8): Undefined offset: 0 in [C:\wamp\www\app\models\vwbrowse.php, line 78]
And my error.log file:
2012-07-31 12:31:47 Warning: Warning (2): array_keys() expects parameter 1 to be array, null given in [C:\wamp\www\app\models\vwbrowse.php, line 78]
And finally this is function that causes problem:
function afterFind($results, $primary)
{
if(!$primary) return $results;
$ret = array();
//we are primary and not part of an associated find
if(!is_array($results)) //find first
{
$tables = array_keys($results);
$record = array();
foreach($tables as $table){
$record = array_merge($record,$result[$table]);
}
$ret['Vwbrowse'] = $record;
}else{ //merge all arrays if separated
$tables = array_keys($results[0]);
foreach($results as $result){
$record['Vwbrowse'] = array();
foreach($tables as $table){
$record['Vwbrowse'] = array_merge($record['Vwbrowse'],$result[$table]);
}
$ret[] = $record;
}
}
return $ret;
}
**This is line 78: $tables = array_keys($results[0]);
**
What is wrong? Thanks in advance for solution.
You're checking to see if $results IS NOT an array, then telling your script to perform an array_keys() on it. Does not compute.
Also, you're trying to access $results[0] without confirmation that the 0th index exists.
Try this first:
if (array_key_exists(0, $results)) { ... }
The undefined index error is stating that the index of the array, in your case [0] is undefined.
You are checking if $results is an array with if (!is_array($results)), but not if it has any data. Try changing it to if (!is_array($results) || (count($results) == 0)).

Categories