-1

Can u tell me why show me tons of notice ?

I am new in php and don't understand where is my mistake, notices from line:

<td>'.$columns[1].'</td> 

To line:

$sum+=$columns[2];

Here is my code:

<?php
         if(file_exists('data.txt'))
         {
             $result= file('data.txt');
             $sum='0';

             foreach($result as $value)
             {

                 $columns=explode('-', $value);

                 echo  '<tr>
                             <td>'.$columns[0].'</td>
                             <td>'.$columns[1].'</td>
                             <td>'.$columns[2].'</td>
                             <td>'.$kinds[trim($columns[3])].'</td>
                        </tr>';
                 $sum+=$columns[2];

             }
             echo '<tr>
                <td>--</td>
                <td>--</td>
                <td>' . $sum . ' </td>
                <td>--</td>
            </tr>'; 


         }             
         ?>

I am new in php and don't understand where is my mistake :( notices from line

<td>'.$columns[1].'</td> 

to line

$sum+=$columns[2];

Here are the notices:

Notice: Undefined offset: 3 in C:\xampp\htdocs\HomeworkOne\index.php on line 31 
Notice: Undefined index: in C:\xampp\htdocs\HomeworkOne\index.php on line 31
Notice: Undefined offset: 2 in C:\xampp\htdocs\HomeworkOne\index.php on line 33
Notice: Undefined offset: 1 in C:\xampp\htdocs\HomeworkOne\index.php on line 29
Notice: Undefined offset: 2 in C:\xampp\htdocs\HomeworkOne\index.php on line 30
Notice: Undefined offset: 3 in C:\xampp\htdocs\HomeworkOne\index.php on line 31
Notice: Undefined index: in C:\xampp\htdocs\HomeworkOne\index.php on line 31
Prix
  • 19,417
  • 15
  • 73
  • 132
Tupan
  • 11
  • 1
  • 1
  • 1
  • 1
    Can you show us the output (with notices)? – Halcyon Sep 25 '13 at 12:13
  • Please share the notices you are getting. – Pupil Sep 25 '13 at 12:13
  • Notice: Undefined offset: 3 in C:\xampp\htdocs\HomeworkOne\index.php on line 31 Notice: Undefined index: in C:\xampp\htdocs\HomeworkOne\index.php on line 31 Notice: Undefined offset: 2 in C:\xampp\htdocs\HomeworkOne\index.php on line 33 Notice: Undefined offset: 1 in C:\xampp\htdocs\HomeworkOne\index.php on line 29 Notice: Undefined offset: 2 in C:\xampp\htdocs\HomeworkOne\index.php on line 30 Notice: Undefined offset: 3 in C:\xampp\htdocs\HomeworkOne\index.php on line 31 Notice: Undefined index: in C:\xampp\htdocs\HomeworkOne\index.php on line 31 – Tupan Sep 25 '13 at 12:14
  • 1
    I agree with @FritsvanCampen but my bet is that you're not exploding data correctly. – STT LCU Sep 25 '13 at 12:14
  • Notice: Undefined offset: 2 in C:\xampp\htdocs\HomeworkOne\index.php on line 33 Notice: Undefined offset: 1 in C:\xampp\htdocs\HomeworkOne\index.php on line 29 Notice: Undefined offset: 2 in C:\xampp\htdocs\HomeworkOne\index.php on line 30 Notice: Undefined offset: 3 in C:\xampp\htdocs\HomeworkOne\index.php on line 31 Notice: Undefined index: in C:\xampp\htdocs\HomeworkOne\index.php on line 31 Notice: Undefined offset: 2 in C:\xampp\htdocs\HomeworkOne\index.php on line 33 – Tupan Sep 25 '13 at 12:15
  • Or the txt file is not what you think it is. – Mihai Sep 25 '13 at 12:15
  • Also, i've retitled your question to match the new informations. Please try to find more meaningful titles in the future – STT LCU Sep 25 '13 at 12:16
  • Try to use the condition if(isset($columns[2])) { where ever applicable. – Vineesh Sep 25 '13 at 12:17
  • @VineeshPoduval this will throw an undefined offset error too. – Daniel W. Sep 25 '13 at 12:17
  • @DanFromGermany it won't raise notice, even something like this won't: `isset($undefined_variable[0][5][10][11])`. isset is not a function. – lupatus Sep 25 '13 at 12:22
  • Add contents of the txt file. – Protomen Sep 25 '13 at 12:28

7 Answers7

2

You are using uninitialized variables. $columns[1], $columns[2] and $columns[3] are not getting values Please try this corrected code:

<?php
if(file_exists('data.txt'))
{
    $result= file('data.txt');
    $sum='0';

    foreach($result as $value)
    {

        $columns=explode('-', $value);
        $kindsDisplay = (isset($columns[3]) && ! empty($kinds[trim($columns[3])])) ? $kinds[trim($columns[3])] : '';
        $one = isset($columns[1]) ? $columns[1] : '';
        $two = isset($columns[2]) ? $columns[2] : '';
        $sum+= isset($columns[2]) ? $columns[2] : 0;

        echo  '<tr>
                   <td>'.$columns[0].'</td>
                   <td>'.$columns[1].'</td>
                   <td>'.$columns[2].'</td>
                   <td>'.$kindsDisplay.'</td>
               </tr>';

    }

    echo '<tr>
              <td>--</td>
              <td>--</td>
              <td>' . $sum . ' </td>
              <td>--</td>
          </tr>'; 

} ?>
Allan Pereira
  • 2,572
  • 4
  • 21
  • 28
Pupil
  • 23,834
  • 6
  • 44
  • 66
1

This is an undefined offset:

$array[0] = "test1";
$array[1] = "test2";

echo $array[3];

You are better off using objects or arrays where you know whats inside.

To get rid use if's:

$array[0] = "test1";
$array[1] = "test2";

if (array_key_exists(3, $array)) {
    echo $array[3];
}

The problem in your case:

$columns = explode('-', $value);

The data you grep from the textfile is not 100% the format you expect, for example:

aaaaa-bbbbbb-cccccc-ddddddd
aaaaa--cccccc-dddddd
aaaaa-ddddd

This means, you need to verify tha data you read is VALID and in the correct format.

Daniel W.
  • 31,164
  • 13
  • 93
  • 151
1

Some things to consider: a blank line at the end of the file could cause this if it only happens once (not each line).

If you want to have default values in case your data is missing seperators you can add

$columns = $columns + array('default_for_key0', 'default1', 'default2', 'default3');

after explode().

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Christoph Diegelmann
  • 2,004
  • 15
  • 26
0

You read every line of the data file. When you have a blank line you have no data in your $result.

Then you try to explode on an empty string and columns[0] is not set.

Try to check if the array is not empty perhaps with

if(count($columns) > 2) {

}

or check wheather the array is set.

if(isset($columns[0])) {

}

otherwise you can fill the array with default data.

René Höhle
  • 26,716
  • 22
  • 73
  • 82
0

Your data is probably not as sane as you think.

$columns=explode('-', "abc");
$columns[0]; // "abc";
$columns[1]; // gives: Undefined offset 1

Either ensure your data is what you expect, or explicitly try to find error cases, like a line with no -.

You can do count($columns) to find out how many columns there are.


Be very wary of constructions like:

$kinds[trim($columns[3])]

You have 2 possible undefined offsets here. $columns[3] and $kinds[#index#] can both be undefined.

Halcyon
  • 57,230
  • 10
  • 89
  • 128
0

Try this way and you will see if you have 4 columns every time:

<?php
if (file_exists('data.txt')) {
    $result= file('data.txt');
    $sum=0;

    foreach($result as $value) {
        $columns=explode('-', $value);
        echo  '<tr>';
        foreach ($columns as $key => $column) {
            echo '<td>'.$column.'</td>';
            if ($key == 2) $sum += $column;
        }
    echo '</tr>';
    echo '<tr><td>--</td><td>--</td><td>'.$sum.'</td><td>--</td></tr>'; 
}             
?>
dareKevil
  • 11
  • 3
0

First you will need to set $kinds as array. Secondly, check array keys before you use in the program else it will throw errors like below: http://d.pr/i/GzVk

Check the revised code, I Hope this will helpful for you.

<?php
if(file_exists('data.txt'))
{
$result= file('data.txt');
$sum='0';
$kinds = array();

foreach($result as $value)
{

$columns=explode('-', $value);

if(array_key_exists($columns[0], $columns)) {
echo '<td>'.$columns[0].'</td>';
}
if(isset($columns[1]) && array_key_exists($columns[1], $columns)) {
echo '<td>'.$columns[1].'</td>';
}

if(isset($columns[2]) && array_key_exists($columns[2], $columns)) {
echo '<td>'.$columns[2].'</td>';
$sum+=$columns[2];
}

if(isset($columns[3]) && array_key_exists($columns[3], $columns)) {
echo '<td>'.$kinds[trim($columns[3])].'</td>';
}

echo      '</tr>';


}
echo '<tr>
<td>--</td>
<td>--</td>
<td>' . $sum . ' </td>
<td>--</td>
</tr>';


}
?>
Satyam Saxena
  • 581
  • 2
  • 10