Rich2233 Rich2233 - 5 months ago 15
HTML Question

Echo table in PHP

So far, I got my code to read from a .txt file, parse the info and output via html. My question is how can I can echo my $name and $email variables into a two-column table?

Here is my code:

<?php

// Read the file into an array
$users = file("names.txt");

// Cycle through the array
foreach ($users as $user) {

// Parse the line, retriving the name and e-mail address
list($name, $email) = explode(" ", $user);

// Remove newline from $email
$email = trim($email);

// Output the data...how could I do this with a two-column table?
echo "<a href=\"mailto:$email\">$name</a> <br />";

}

?>


Thanks in advance.

Answer

just add some markup

// Read the file into an array
$users = file("names.txt");

if (count($users)) {
    // Open the table
    echo "<table>";

    // Cycle through the array
    foreach ($users as $user) {

        // Parse the line, retriving the name and e-mail address
        list($name, $email) = explode(" ", $user);

        // Remove newline from $email
        $email = trim($email);

        // Output a row
        echo "<tr>";
        echo "<td>$name</td>";
        echo "<td><a href=\"mailto:$email\">$email</a></td>";
        echo "</tr>";
    }

    // Close the table
    echo "</table>";
}
Comments