首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >循环(和显示) SQL中的多个表行

问循环(和显示) SQL中的多个表行
EN

Stack Overflow用户
提问于 2012-01-18 09:40:30
回答 1查看 8.3K关注 0票数 1

在SQL表中循环多行并正确显示信息时遇到问题。我得到的最接近的是:

代码语言:javascript
复制
<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)。有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-01-18 09:58:04

代码语言:javascript
复制
<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循环。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8904124

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档