1
Dear, I have the following code, where I read a file . CSV and show it in a table. It even does the proposed, however returns me the error Notice: Undefined offset: 1
at the end of the file. This for all matrices, except 0
How to resolve this error?
<table id="data-table" class="table table-striped table-bordered">
<thead>
<tr>
<th>ID</th>
<th>Nome</th>
<th>Usuário</th>
<th>Email</th>
<th>Perfil</th>
</tr>
</thead>
<tbody>
<?php
$linhas = fopen ("teste.csv", "r");
while (!feof ($linhas))
{
$ponteiro = fgets($linhas, 4096);
$valores = preg_split("[;]",$ponteiro);
echo "<tr>";
echo "<td>".$valores[0]."</td>\n
<td>".$valores[1]."</td>\n
<td>".$valores[2]."</td>\n
<td>".$valores[3]."</td>\n
<td>".$valores[4]."</td>\n
<td>".$valores[5]."</td>\n";
echo "</tr>";
}
fclose ($linhas);
echo "</table>";
?>
</tbody>
</table>
I understand that it is necessary to validate if it is null, it was validating through isset(), however unsuccessful too. With this solution proposed by you I did not have effectiveness too...
– Willian Lima
@Willianlima edited the answer, I forgot that if the delimiter is not found in the string, it is returned anyway and not as
false
.– rray
In this last way, it worked, the last line was marked as 0. It is not yet ideal, but it is already a haha beginning. Thanks Edit: I made a change to the code, it’s perfect now!
– Willian Lima