1

Here's my code for index.php

<?php

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

    // render portfolio
    $rows = CS50::query("SELECT symbol, shares FROM portfolio WHERE id = ?", $_SESSION["id"]);

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

    $cash = CS50::query("SELECT cash FROM users WHERE id =?", $_SESSION["id"]);

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

?>

And here's my code for portfolio.php

<table>

    <tr>
    <th>Symbol</th>
    <th>Name</th>
    <th>Shares</th> 
    <th>Price</th>
    <th>Total</th>
    </tr>

    <?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("<td>" . $position["total"] . "</td>");
            print("</tr>");
        }

    ?>
</table>

<pr>You have <?php print($cash); ?> balance left.</pr>

However when I try to load https://ide50-username.cs50.io/, I only get the table's header row and nothing else, even the line that tells me how much cash I have left prints a weird array instead of telling me how much cash I have left. Here's a screenshot of the website

Can anyone tell me where I went wrong? Also, how do I debug a website like this? It seems that gdb doesn't work so well here.

1

the interpreter is complaining about array to string conversion. I assume it's talking about $cash since $cash is an array, yet you're passing it as a string as that's what print takes. see http://php.net/manual/en/function.print.php!

4
  • 1
    Even so, I get an error if I change $cash to $cash["cash"]. Also is there any alternative to gdb/printf for php which will allow me to see the contents of my variables? – Wet Feet Mar 23 '16 at 11:49
  • 1
    @WetFeet recall query returns an array of rows! and I think dumping/printing the values of your variables is probably the easiest approach with PHP at this level. – kzidane Mar 23 '16 at 12:12
  • 1
    What function can I use to print to the terminal instead of the Web page in php? Looking for something like printf in C for PHP but googling doesn't seem to bring up any results. – Wet Feet Mar 23 '16 at 13:15
  • 2
    @WetFeet the problem is that you don't run the PHP interpreter yourself (e.g., from a terminal) to be able to see what's being printed on the standard output. the server is the one which runs the PHP interpreter. a simple hack would be to dump your values to a file, maybe using file_put_contents. – kzidane Mar 23 '16 at 13:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .