0

Hello guys,

I am relatively new to Php, so go easy on me. Having said that, the output of the code is exactly what I am expecting ( a table of 6 columns and n rows ).My problem is that I have 5 notifications at the end of the table of undefined offset 1, 2 ,3 ,4 ,5 (indicating the lines $result[1]...$result[5];

By printing the array I am also noticing at the end of it Array ( [0] => [1] => [2] => [3] => [4] => [5] => ) maybe that is the problem, the index being set to nothing, but I hate 'maybe' so I am quite stuck at the moment.

Thanks in advance

$csvData = file_get_contents('excel_csv.csv');
$lines = explode(PHP_EOL, $csvData);

$array = array();
foreach ($lines as $line) {
    $array[] = str_getcsv($line);
}

echo "<table class='main-table'>";
    foreach($array as $result) {
           echo "<tr class='main-row'>
             <td class='main-column'><img src='poze/$result[0]'/></td>  
             <td class='main-column'>$result[1]</td>   
             <td class='main-column'>$result[2]</td>  
             <td class='main-column'>$result[3]</td>
             <td class='main-column'>$result[4]</td>  
             <td class='main-column'>$result[5]</td>   
            </tr>";
    }
echo "</table>";
Duhanes
  • 389
  • 1
  • 3
  • 10
  • Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – Pankaj Makwana Nov 15 '17 at 11:43

1 Answers1

1

change 1st tow line

$csvData = file_get_contents('excel_csv.csv');
$lines = explode(PHP_EOL, $csvData);

with

$lines = file('excel_csv.csv', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

to avoid empty lines

splash58
  • 26,043
  • 3
  • 22
  • 34
  • It worked ! Thank you for that. Just a follow up question, Array ( [0] => [1] => [2] => [3] => [4] => [5] => ) at the end of the table there is one more row which is empty. How could I remove that? – Duhanes Nov 15 '17 at 11:54
  • It can't be with my answer – splash58 Nov 15 '17 at 12:18
  • Empty values are result of empty line at the end of a file. Because with the option FILE_SKIP_EMPTY_LINES you will not get them, empty values will not be too – splash58 Nov 15 '17 at 12:34
  • Glad to help. Good luck! – splash58 Nov 15 '17 at 12:34