1

Having trouble looping through multiple rows in a SQL table and getting the info to display correctly. The closest I have got is the following:

<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>

The while/foreach loop goes on to display the info from the rows into a HTML table, but it displays the first character from every column as opposed to all the characters from the columns I echo to be displayed (symbol, price, and shares). Any ideas?

3
  • Do you mean you do (e.g.) echo $symbol; and it only shows the first character? We might need to see the code you wrote to print the HTML table - trimmed down that still reproduces the problem if possible. Commented Jan 18, 2012 at 1:48
  • Can you show the code where it's actually echoing into the table? Commented Jan 18, 2012 at 1:48
  • Yes, your code looks fine. The problem must lie in subsequent code that affects $symbol etc. or their output. Commented Jan 18, 2012 at 1:50

1 Answer 1

3
<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>

You just had one loop too many. The while loop continues until !$row, with one row per execution, so you don't want the foreach loop you had.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.