1

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.

1
  • Thanks everyone! I'm very new to php, so I apologize for my novice question. Commented Oct 26, 2011 at 19:42

8 Answers 8

8

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>";
}
Sign up to request clarification or add additional context in comments.

Comments

2

A table looks like this:

<table>
   <tr>
     <td>...</td>
     <td>...</td>
     <td>...</td>
   </tr>
   ...
</table>

Use the powers of php to put tr and td's where they should be, inside and outside the for loop.

Comments

2

do you mean more like this?

<?php

// Read the file into an array
$users = file("names.txt");
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 the data...how could I do this with a two-column table?
    echo "<tr><td>".$name."</td><td>".$email."</td></tr>";

}
echo "</table>";
?>

Comments

2

Use something like this:


<?php

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

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 the data...how could I do this with a two-column table?
    echo "<tr><td>$email</td><td>$name</td></tr>";

}

echo "</table>";

?>

Basically, what's happening here is that you're setting up an HTML table. Each iteration through your user's loop adds a new row, complete with columns.

Comments

2

Nothing special about it:

echo '<table>';
foreach($users as #user) {
    # list(…)
    echo '<tr><td>';
    echo htmlspecialchars($name);
    echo '</td><td>';
    echo htmlspecialchars($email);
    echo '</td></tr>';
}
echo '</table>';

Comments

2
<html>
    <body>

        <table>
            <tr>
                <th>name</th>
                <th>email</th>
            </tr>

            <?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 "<tr>
                    <td>$name</td>
                    <td>$email</td>
                </tr>";
            }
            ?>
        </table>
    </body>
</html>

also check this link http://www.w3schools.com/html/html_tables.asp

Comments

1
echo "<table><thead><tr><th>Name</th><th>Email</th></tr></thead><tbody>";

foreach ($users as $user) {
   //get $email and $name
   echo "<tr><td>$name</td><td><a href="mailto:$email">$email</a></td></tr>";
}
echo "</tbody></table>"

Comments

0
    <html>
<head>
<title> TINDAHAN </title>
</head>
<body>

<center> ONLINE GROCERY STORE </center>

<form action = "grocery.php" method ="post">

<table width = "1500" height= "50" border= "0">
<tr>
<td> 
<b> Coffee: </b>;<select name = "coff"> 
<option value = "Nescafe" > Nescafe </option>
<option value = "Blend45" > Blend45 </option>
<option value = "GreatTaste" > Great Taste </option>
<option value = "San Mig" > San Mig </option>
</td>

<td> 
<b> Sugar: </b>;<select name = "sugar">
<option value = "White" > White </option>
<option value = "Brown" > Brown </option>
<option value = "Mascobado" > Mascobado </option>
</td>

<td> 
<b> Milk: </b>;<select name = "milk">
<option value = "BearBrand" > Bear Brand </option>
<option value = "Nido" > Nido </option>
<option value = "Alaska" > Alaska </option>
<option value = "Carnation" > Carnation </option>
</td>
</tr>

<tr>
<td> 
<b> Quantity: </b>;
<input type = "text" name = "acoff" size = "20" maxlength = "40">
</td>

<td> 
<b> Quantity: </b>;
<input type = "text" name = "asugar" size = "20" maxlength = "40">
</td>

<td> 
<b> Quantity: </b>;
<input type = "text" name = "amilk" size = "20" maxlength = "40">
</td>
</tr>

<tr>
<td> 
<b> PRODUCTS: </b>;
NESCAFE - 120 <br>;
BLEND 45 - 90 <br>;
GREAT TASTE - 95 <br>;
SAN MIG - 110 
</td>

<td> 
WHITE - 60/KILO <br> 
BROWN - 45/KILO <br>
MASCOBADO - 90/KILO <br>
</td>

<td> 
BEAR BRAND - 250 <br>
NIDO - 195 <br>
ALASKA - 175 <br>
CARNATION - 25 <br>
</td>
</tr>

<tr>
<td> 
<b> Soap: </b> <select name = "soap"> 
<option value = "Tide" > Tide </option>
<option value = "Ajax" > Ajax </option>
<option value = "Surf" > Surf </option>
<option value = "Pride" > Pride </option>
</td>

<td> 
<b> Cooking Oil: </b> <select name = "oil">
<option value = "Baguio Oil" > Baguio Oil </option>
<option value = "Minola" > Minola </option>
<option value = "Canola" > Canola </option>
<option value = "Olive Oil" > Olive Oil </option>
</td>

<td> 
<b> Toothpaste: </b> <select name = "tp">
<option value = "Colgate" > Colgate </option>
<option value = "Close Up" > Close Up </option>
<option value = "Happee" > Happee </option>
<option value = "Beam" > Beam </option>
</td>
</tr>

<tr>
<td> 
<b> Quantity: </b>
<input type = "text" name = "asoap" size = "20" maxlength = "40">
</td>

<td> 
<b> Quantity: </b>
<input type = "text" name = "aoil" size = "20" maxlength = "40">
</td>

<td> 
<b> Quantity: </b>
<input type = "text" name = "atp" size = "20" maxlength = "40">
</td>
</tr>

<tr>
<td> 
<b> PRODUCTS: </b>
TIDE - 35 <br>
AJAX - 33 <br>
SURF - 32 <br>
PRIDE - 38
</td>

<td> 
BAGUIO OIL - 135 <br> 
MINOLA - 133 <br>
CANOLA - 132 <br>
OLIVE OIL - 238
</td>

<td> 
COLGATE - 185 <br>
CLOSE UP - 197 <br>
HAPPEE - 122 <br>
BEAM - 38 <br>
</td>
</tr>
</table>
<center>
<br>
<br>
<input type = "SUBMIT" name = "submit" value = "SUBMIT">
<center>
</body>
</html>

1 Comment

Code only answers are generally discouraged. Try explaining your answer, or where the OP went wrong.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.