PHP中的回声表

时间:2011-10-26 19:28:14

标签: php html echo

到目前为止,我的代码是从.txt文件中读取,通过html解析信息和输出。我的问题是如何将我的$ name和$ email变量回显到一个双列表中?

这是我的代码:

<?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 />";

}

?>

提前致谢。

8 个答案:

答案 0 :(得分:6)

只需添加一些标记

// 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>";
}

答案 1 :(得分: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>

另请查看此链接http://www.w3schools.com/html/html_tables.asp

答案 2 :(得分:2)

表格如下:

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

使用php的强大功能将trtd放在for循环内外的位置。

答案 3 :(得分:2)

使用类似的东西:


<?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>";

?>

基本上,这里发生的是你正在设置一个HTML表。通过用户循环的每次迭代都会添加一个新行,其中包含列。

答案 4 :(得分:1)

你是说更喜欢这个吗?

<?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>";
?>

答案 5 :(得分: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>"

答案 6 :(得分:1)

没什么特别的:

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

答案 7 :(得分: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>