0

i am trying to show user his current portfolio but from the output it seems that i am not able to access pre-built lookup function.also i am directed to portfolio page without asking for login or registration.

here is my index.php

<?php

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

        // query which returns user id,symbol and shares of current user from database
        $rows = CS50::query("SELECT user_id,symbol,shares FROM portfolio WHERE user_id = ?",$_SESSION["id"]);

        // a array which stores all information about user
        $positions = [];
        // loop to iterate over associative array
        foreach ($rows as $row)
        {
            $stock = lookup($row["symbol"]);
            if ($stock !== false)
            {
                $positions[] = [
                    "name" => $stock["name"],
                    "price" => $stock["price"],
                    "shares" => $row["shares"],
                    "symbol" => $row["symbol"],
                    ];
            }

        }

        // returns current cash of the user
        $balance = CS50::query("SELECT username,cash FROM users WHERE id = ?",$_SESSION["id"]);
        $cash = $balance[0]["cash"];

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

    ?>

and here is link to portfolio.php:- portfolio.php

and below image shows the output of the code: enter image description here

please help.

0

Try clearing the session by using logout(); and then try.

Make sure you are including helpers.php and sometimes lookup(); takes time to perform the request.

| improve this answer | |

You must log in to answer this question.

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