Php noob.
I have this text file (names.txt) with comma separated names and codes. Looks similar to this:
John doe, 123456, 876543
Mary Ann, 456878
Ben Anderson, 987554, 097532, 873445
As you can see each name can have a different amount of codes ranging from one up to 10.
What I want to do is to output this information as a table
I tried this:
<table>
<?php
$file_handle = fopen("names.txt", "rb");
while (!feof($file_handle) ) {
$line_of_text = fgets($file_handle);
$parts = explode(',', $line_of_text);
echo "<tr>";
echo "<td><strong>" . $parts[0] ."</strong></td>";
echo "<td>" . $parts[1] . "</td>";
echo "<td>" . $parts[2] . "</td>";
echo "<td>" . $parts[3] . "</td>";
echo "<td>" . $parts[4] . "</td>";
echo "<td>" . $parts[5] . "</td>";
echo "<td>" . $parts[6] . "</td>";
echo "<td>" . $parts[7] . "</td>";
echo "<td>" . $parts[8] . "</td>";
echo "<td>" . $parts[9] . "</td>";
echo "<td>" . $parts[10] . "</td>";
echo "</tr>";
}
fclose($file_handle);
?>
</table>
This does not work because sometimes $parts[] will be empty and there is nothing to output and I get an error. Undefined offset.
How would I go about doing this and only “echo” the $parts[] when it has a value?
Instead of echo’ing all the parts like you do know (the 10 lines), you could replace it by something like this:
What does it do: loop 10 times and echo always an . Inside the it checks if $parts[] for that iteration is not empty, and if not echos it. Else it echos just nothing.
You still want all ‘s to be there, else your table would get ruined (would look strange to the end user).