php PHP中的回声表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7907807/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Echo table in PHP
提问by Rich2233
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?
到目前为止,我让我的代码从 .txt 文件中读取,解析信息并通过 html 输出。我的问题是如何将我的 $name 和 $email 变量回显到一个两列表中?
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.
提前致谢。
回答by arahaya
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>";
}
回答by Tom
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.
基本上,这里发生的事情是您正在设置一个 HTML 表格。通过用户循环的每次迭代都会添加一个新行,并带有列。
回答by onatm
<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
回答by Naftali aka Neal
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.
使用 php 的力量将tr
andtd
放在它们应该在的地方,在 for 循环内外。
回答by Robot Woods
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>";
?>
回答by Explosion Pills
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>"
回答by knittl
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>';
回答by Aaron
<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>