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?

share|improve this question
    
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. – mathematical.coffee Jan 18 '12 at 1:48
    
Can you show the code where it's actually echoing into the table? – declan Jan 18 '12 at 1:48
    
Yes, your code looks fine. The problem must lie in subsequent code that affects $symbol etc. or their output. – Borodin Jan 18 '12 at 1:50
    
more code added – scrolls Jan 18 '12 at 1:52
up vote 4 down vote accepted
<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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.