在SQL表中循环多行并正确显示信息时遇到问题。我得到的最接近的是:
<table border="1" cellpadding="10">
<th>Stock</th><th>Price</th><th>Shares</th><th>Value</th><th>Sell Stock</th>
<?php
$transactions = mysql_query("SELECT * FROM transactions WHERE email='$email'");
while ($rows = mysql_fetch_assoc($transactions)) {
foreach($rows as $row) {
$symbol = $row["symbol"];
$price = $row["price"];
$shares = $row["shares"];
$value = $price * $shares;
?>
<form name="sellStock" action="sell.php" method="get">
<tr>
<td><input name="symbol" type="hidden" value="<?php echo $symbol ?>"><?php echo $symbol ?></td>
<td><input name="price" type="hidden" value="<?php echo $price ?>"><?php echo $price ?></td>
<td><input name="shares" type="hidden" value="<?php echo $shares ?>"><?php echo $shares ?></td>
<td><input name="value" type="hidden" value="<?php $value ?>" /><?php $value ?></td>
<td><input name="sell" type="submit" value="Sell"></td>
</tr>
<?php
}
}
?>
</table>while/foreach循环继续将行中的信息显示到HTML表中,但它显示每列中的第一个字符,而不是要显示的列中的所有字符(symbol、price和shares)。有什么想法吗?
发布于 2012-01-18 09:58:04
<table border="1" cellpadding="10">
<th>Stock</th><th>Price</th><th>Shares</th><th>Value</th><th>Sell Stock</th>
<?php
$transactions = mysql_query("SELECT * FROM transactions WHERE email='$email'");
while ($row = mysql_fetch_assoc($transactions)) {
$symbol = $row["symbol"];
$price = $row["price"];
$shares = $row["shares"];
$value = $price * $shares;
?>
<form name="sellStock" action="sell.php" method="get">
<tr>
<td><input name="symbol" type="hidden" value="<?php echo $symbol ?>"><?php echo $symbol ?></td>
<td><input name="price" type="hidden" value="<?php echo $price ?>"><?php echo $price ?></td>
<td><input name="shares" type="hidden" value="<?php echo $shares ?>"><?php echo $shares ?></td>
<td><input name="value" type="hidden" value="<?php $value ?>" /><?php $value ?></td>
<td><input name="sell" type="submit" value="Sell"></td>
</tr>
<?php
}
?>
</table>你只是有太多的循环。while循环将一直持续到!$row,每次执行一行,因此您不希望有foreach循环。
https://stackoverflow.com/questions/8904124
复制相似问题