0

When I open my website and log in, the index.php page shows a table with headers, but no data, even though I think it should be showing the data according to my code.

Index.php:

<?php

// configuration
require("../includes/config.php"); 

$rows = CS50::query("SELECT symbol, shares FROM stocks where id = ?", $_SESSION["id"]);

$portfolio = [];

$positions = [];
foreach ($rows as $row) {
    $stock = lookup($row["symbol"]); 
    $positions[] = [
        "name" => $stock["name"],
        "price" => $stock["price"],
        "shares" => $row["shares"],
        "symbol" => $row["symbol"],
    ]; 
}

render("portfolio.php", ["positions" => $positions, "title" => "Portfolio"]);

?>

And portfolio.php:

<table class="table table-striped">
    <th>Symbol</th>
    <th>Name</th>
    <th>Shares</th>
    <th>Price</th>
    <th>TOTAL</th>

    <tbody>
        <?php

            foreach ($positions as $position) {
            print("<tr>");
            print("<td>" . $position["symbol"] . "</td>");
            print("<td>" . $position["name"] . "</td>");
            print("<td>" . $position["shares"] . "</td>");
            print("<td>" . $position["price"] . "</td>");
            print("</tr>");
            } 

        ?>

    </tbody>

</table>

Any help would be very much appreciated! Thanks

5
  • are you sure $rows in index.php isn't empty? try calling dump() on it to see what it contains.
    – ronga
    Jul 25 '16 at 3:46
  • How does dump work? I tried calling it and I just got a blank page... I'm confused because there should be something in $rows. I added some stocks to the person that I'm testing on, so it should work.
    – user11799
    Jul 25 '16 at 15:28
  • Why did my SQL statement: $rows = CS50::query("SELECT symbol, shares FROM stocks where id = ?", $_SESSION["id"]); not work?
    – user11799
    Jul 25 '16 at 15:46
  • dump() prints an associative array's key value pairs. You can call print_r($rows) directly, which is also what dump() does. If you still don't see anything, then it means that particular id doesn't own any stocks. Verify the $_SESSION["id"] you're using.
    – ronga
    Jul 26 '16 at 2:51
  • Thanks for the help!
    – user11799
    Jul 26 '16 at 18:29

You must log in to answer this question.

Browse other questions tagged .