0

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?

5 Answers 5

1

You can use fgetcsv()

Note: A blank line in a CSV file will be returned as an array comprising a single null field, and will not be treated as an error.

<table>
<?php
$handle = fopen("test.csv", "r");
while (($parts = fgetcsv($handle, 1000, ",")) !== FALSE) {
    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>
Sign up to request clarification or add additional context in comments.

Comments

0

Instead of echo'ing all the parts like you do know (the 10 lines), you could replace it by something like this:

for($i = 1; $i <= 10; $i++) {
    echo '<td>' . (!empty($parts[$i]) ? $parts[$i] : '') . '</td>';
}

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).

3 Comments

empty() will still through a warning because the index does not exist. You need isset() here.
So what if suddenly the number (10) of codes would change? How would we write the php code so that it will check for the maximum amount of values and set that as a variable which we could put here: for($i = 1; $i <= $maxvalues; $i++) { echo '<td>' . (!empty($parts[$i]) ? $parts[$i] : '') . '</td>'; }
Yes, that seems good. Just set $maxvalues to the amount of codes that are there. Or: first walk through all items to find out which one has the most codes and use that amount as $maxvalues, after that do the code as you already do..
0

The undefined offset error is an E_NOTICE, meaning it can safely be ignored. To prevent notices from showing up, use:

error_reporting(E_ALL ^ E_NOTICE);

If you don't want to do this, there are a few ways you can fix the problem. One trick would be to change your explode line to:

$parts = explode(",", $line_of_text.",,,,,,,,,,");

Such that there will always be at least 10 parts.

1 Comment

This also works but I think I will stick with Stefandoorn's solution.
0
<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>";
for($i=1; $i<=10; $i++) {
    echo "<td>" . $parts[$i] ? $parts[$i] : '' . "</td>";
}
echo "</tr>";
}

fclose($file_handle);

?>
</table>

Comments

0
while (!feof($file_handle) )
{
  $line_of_text = fgets($file_handle);
  $parts = explode(',', $line_of_text);

  // line not empty?
  if (isset($parts[0]) && !empty($parts[0]))
  {
    // first line
    echo '<td>' . $parts[0] . '</td>';
    // other lines
    for($i = 1; $i <= 10; $i++) {
      // echo with check
      echo '<td>' . (isset($parts[$i]) ? $parts[$i] : '') . '</td>';
    }
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.