-2

I have some issues with explode function. I set it like this:

$p = explode(";", $data[$c]);

end I received results fine in first 18 rows and then I got error message:
Notice: Undefined offset: 6 in C:\xampp\htdocs\slovenac\zaloge\parser.php on line 18

that 18th row is like this:

ean: 4975769396717;AVTORADIO JVC KD-X50BTEY;0;0;135;Softtrade d.o.o., Gorèe 20d;JVC;0097;

and my explode react on ".," just before "Gorèe 20d".

How can I fix this?

    <?php
$row = 1;
if (($handle = fopen("csv/".$_GET['file_name'], "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        $row++;
        for ($c=0; $c < $num; $c++) {
            echo $data[$c] . "<br />\n";
            $p = explode(";", $data[$c]);
            echo "<tr>";
            echo "<td>".$p[0]."</td>";
            echo "<td>".$p[1]."</td>";
            echo "<td>".$p[2]."</td>";
            echo "<td>".$p[3]."</td>";
            echo "<td>".$p[4]."</td>";
            echo "<td>".$p[5]."</td>";
            echo "<td>".$p[6]."</td>";
            echo "<td>".$p[7]."</td>";
            echo "<td>".$p[8]."</td>";
            echo "</tr>";
        }
    }
    fclose($handle);
}

End here are my results with error:

ean: 4975769396717;AVTORADIO JVC KD-X50BTEY;0;0;117;Urbo PE Bre�ice;JVC;0097;
    ean: 4975769396717;AVTORADIO JVC KD-X50BTEY;0;0;118;Tripex PE Celje;JVC;0097;
    ean: 4975769396717;AVTORADIO JVC KD-X50BTEY;0;0;129;Setring PE Medvode;JVC;0097;
    ean: 4975769396717;AVTORADIO JVC KD-X50BTEY;0;0;131;Dar PE Ptuj;JVC;0097;
    ean: 4975769396717;AVTORADIO JVC KD-X50BTEY;0;0;133;ETS Pregl PE Slovenska Bistrica;JVC;0097;
    ean: 4975769396717;AVTORADIO JVC KD-X50BTEY;0;0;134;Digital Elektronik PESolkan;JVC;0097;
    ean: 4975769396717;AVTORADIO JVC KD-X50BTEY;0;0;135;Softtrade d.o.o.

    Notice: Undefined offset: 6 in C:\xampp\htdocs\slovenac\zaloge\parser.php on line 18

    Notice: Undefined offset: 7 in C:\xampp\htdocs\slovenac\zaloge\parser.php on line 19

    Notice: Undefined offset: 8 in C:\xampp\htdocs\slovenac\zaloge\parser.php on line 20
    Gor�e 20d;JVC;0097;

    Notice: Undefined offset: 4 in C:\xampp\htdocs\slovenac\zaloge\parser.php on line 16

2 Answers2

1

The Notice: Undefined offset occurs in Line 18 of your code, not your data file. It is because $p[6] is not defined - and this is, because some lines in your CSV file are simply having not enough columns/semicolons.

Just check, if a value is set in your $p array, before output:

 echo "<td>".(isset($p[0]) ? $p[0] : '-')."</td>";
 echo "<td>".(isset($p[1]) ? $p[1] : '-')."</td>";

This will output a '-', if a value is not set in your file. You could use a empty string '' as well.

Get it a little more compact by using a for loop:

for ($i=0; $i<9; $i++) {
    echo '<td>'.(isset($p[$i]) ? $p[$i] : '-').'</td>';
}
DerVO
  • 3,679
  • 1
  • 23
  • 27
  • Hi DerVO, I have to say thanks for helping me and I very appreciate people who actually wants to help. You and corros were both very helpful and sorry that I can not give you both thumbs up. I could but somebody from people above, who is here just to give negative rates when someone is in real trouble took me that option. I will definitely contact support for this, because I just tried to get some help. Once again thanks to you and to corros. – Aleksandar Milic Jun 01 '16 at 23:02
0

according to your code you take for granted that every line of the csv file has at least nine values, but the last line of your example has only six values.

try:

<?php
$row = 1;
if (($handle = fopen("csv/".$_GET['file_name'], "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        $row++;
        for ($c=0; $c < $num; $c++) {
            echo $data[$c] . "<br />\n";
            $p = explode(";", $data[$c]);
            echo "<tr>";
            foreach($p as $value){
                echo "<td>".$value."</td>";
            }
            //if you want to always have nine columns
            for($i=9-count($p); $i > 0; $i--){
                echo "<td></td>";
            }
            echo "</tr>";
        }
    }
    fclose($handle);
}

This is just an example, the code can be improved

corros
  • 597
  • 2
  • 7
  • 15