I've been working on CS50 Pset7, CS50 Finance, and I have everything working bar the sell page. Everything on the sell page works, except for the fact that when I click my drop-down menu to choose what shares to sell, I get blank options. I think my syntax is wrong inside the foreach loop, but I'm not sure. Have been stuck for ever! Below is part of my code.

$symbol = CS50::query("SELECT symbol FROM portfolio WHERE user_id = ?",$_SESSION["id"]);?>


<option disabled selected value=''>Symbol</option>
        <option value=''></option>
                foreach ($stocks as $symbol)    
                    echo("<option value='$symbol'>" . $symbol . "</option>");

1 Answer 1


How are you passing the php variable from your controller to your viewing page (sell page)? You are storing the array returned by your mysql query in $symbol, but it looks like you are passing $stocks variable to your sell page.

Also, how did you set up your table in mysql? If you did it as given in pset specs, then I'm guessing you want to access the stock name by $symbol["name"]? However, if you set it up differently, or you populated a new associative array called $stocks with all the symbols (which is what I'm assuming below), then you're ok.

Once you've passed $stocks to the sell page via render, you need to enclose all php code in your html body between '< ?php' and ' ? >' even if you have to break it up into pieces. Try this:

<select name="stock_picked">
  <?php for each $stocks as $symbol ?>
    <option value="<?php echo $symbol; ?> "><?php echo $symbol; ?></option>

You must log in to answer this question.

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