-1

The following php code:


<?php
$fopen = fopen("tasklistout.csv","r");
while(!feof($fopen))
{
    $line = fgets($fopen);
    echo "\r\n\t<tr>";
    $piece_array = preg_split("/[\s,]+/",$line);

    for ($forvar = 1; $forvar <= 5; $forvar++)
    {
        $array_index = $forvar - 1;
        echo "\r\n\t\t<td>" . $piece_array[$array_index] . "</td>";
    }

    echo "\r\n\t</tr>\r\n";
}
fclose($fopen);
?>

Produces the following error: (on 4 separate occasions)


Notice: Undefined offset: 1 in file.php on line 33


In the following HTML Document:

<!doctype html>
<html lang="en">

   <head>
     <meta charset="utf-8">
     <title>Lab_11-Objective_01--Tables</title>
     <meta name="description" content="HTML 'table' element usage for Lab 11 Objective 01">
     <meta name="author" content="Charles E Lentz">
     <link rel="stylesheet" href="stylesheet.css">
   </head>

   <body>

   <table>
    <tr>
        <th>Image Name</th>
        <th>PID</th>
        <th>Session Name</th>
        <th>Session#</th>
        <th>Mem Usage</th>
    </tr>
    <?php
    $fopen = fopen("tasklistout.csv","r");
    while(!feof($fopen))
    {
        $line = fgets($fopen);
        echo "\r\n\t<tr>";
        $piece_array = preg_split("/[\s,]+/",$line);

        for ($forvar = 1; $forvar <= 5; $forvar++)
        {
            $array_index = $forvar - 1;
            echo "\r\n\t\t<td>" . $piece_array[$array_index] . "</td>";
        }

        echo "\r\n\t</tr>\r\n";
    }
    fclose($fopen);
    ?>

   </table>

   </body>

</html>

How do I fix this error?

Antony
  • 14,900
  • 10
  • 46
  • 74
Charles Lentz
  • 61
  • 1
  • 7
  • 5
    Well, what does tasklistout.csv look like? I'm betting some lines don't have five values separated by "," but a single one. – Sergiu Paraschiv Aug 24 '13 at 16:24
  • Use `print_r( $piece_array )` before `for` loop – bystwn22 Aug 24 '13 at 16:24
  • I don't believe this is a duplicate. I know what the error 'Undefined Offset' is... it means something is wrong with returning the array value of that index... but what I'm asking is what is CAUSING that error. – Charles Lentz Aug 24 '13 at 16:25
  • Sergiu, everything you could need to answer this question can be found at: http://charleselentz.com/stackoverflow/lab11obj01/ , but last time I posted a link in the question itself, I had to delete it, because it was marked as off-topic, because I asked the question in a way stack overflow doesn't like... apparently – Charles Lentz Aug 24 '13 at 16:27
  • initialize $array_index to some value at the starting of code. Eg : $array_index =1; – Rahul Aug 24 '13 at 16:27
  • Sergiu's comment is spot on... The problem is that you blindly assume "all lines will have five elements separated by `\s` or `,`" and never validate this assumption. If a line has less than five elements, the resulting array will not have five elements and accesing by index will fail eventually. Do note that splitting on `[\s,]+` means that `foo,,,,` is parsed as *two* values, `"foo"` and `""` (the end of the string). – DCoder Aug 24 '13 at 16:32
  • The [last post](http://stackoverflow.com/questions/18420213/explode-by-t-error-pgrep-split-error-php) was marked off-topic, because OP didn't explain that he was actually inquiring about notices and warnings. The `file()` function, `str_getcsv` and a simpler `foreach` would have solved this. – mario Aug 24 '13 at 17:10

3 Answers3

1

The Undefined offset error means that an array item in that varible does not exist. This suggests that the issue is futher up in your code where the array should be created (but is not). For example, the preg_split("/[\s,]+/",$line); line is probably the issue here.

To find out add this line:

var_dump($piece_array);

after this line

$piece_array = preg_split("/[\s,]+/",$line);

If you need futher help post the outcome as an edit and I will try to help you.

sousdev
  • 132
  • 2
  • 9
  • It was a really stupid error. The error wasn't even in the php, it was in the csv file... there was 1 line at the bottom, one extra blank line throwing this error..... Should I delete this question? – Charles Lentz Aug 24 '13 at 16:35
1

There is an empty line in your csv file at the end.
Use isset and empty to check the index exists in your array.
Instead of while loop you can do it with a foreach loop along with file function. See my example code.

<?php
  // read entire file into an array
  $lines  = file( "tasklistout.csv" );
  // loop through each line
  foreach( $lines as $line ) {
    // remove whitespace from line
    $line = trim( $line );
    // make sure that line is not empty
    if ( $line ) {
      // split line with comma or space
      $piece_array  = preg_split( "/[\s,]+/", $line );
      // make sure that array contains at least 1 value
      if ( !empty( $piece_array ) ) {
        echo "\r\n\t<tr>";
        for( $i = 0; $i < 5; $i++ ) {
          if ( isset( $piece_array[$i] ) ) {
            echo "\r\n\t\t<td>".$piece_array[$i]."</td>";
          }
          else {
            echo "\r\n\t\t<td>&nbsp;</td>";
          }
        }
        echo "\r\n\t</tr>\r\n";
      }
    }
  }
?>
bystwn22
  • 1,776
  • 1
  • 10
  • 9
0

As sergiu suggested, the .csv file is probably not formated to contain five commas(And probably just one)

You should try using a foreach loop to achieve this instead, thus avoiding array indexing(Well, sort of). But first var_dump the $piece_array to see if its not empty, else you may want to check the .csv file