0

I can't seem to connect my sell_index.php (Where I get the symbols in with query) with my view, sell_form.php, because $symbols is empty.

I have sell working if I directly get symbols in sell_form.php, but I think I shouldn't do this as this file is accessible. Or is it safe to just have it in my view?

Here is my code:

sell_form.php:

        <?php
        
        //$symbols = CS50::query("SELECT symbol FROM portfolios WHERE user_id = ?",$_SESSION["id"]);
                ?>
    <form action="sell.php" method="post">
        <fieldset>
            <div class="form-group">
                
                    
                     <?php
                         echo "<select name='symbol'>";
                         foreach($symbols as $row)
                            {
                              echo "<option value='".$row["symbol"]."'>".$row["symbol"]."</option>";            
                            }
                             echo "</select>";
                    ?>
                    </select>
            </div>
            <div class="form-group">
                <button class="btn btn-default" type="submit">Sell</button>
            </div>
        </fieldset>
    </form>

sell_index.php

    <?php
    
     require("../includes/config.php"); 
    
       
            
        $symbols = CS50::query("SELECT symbol FROM portfolios WHERE user_id = ?",$_SESSION["id"]);
        
        render("sell_form.php", ["symbols" => $symbols, "title" => "Sell"]);
    ?>
1

Looks like you're mixing up your view and controller, which appear to be sell_view and sell_index, respectively. If so, sell_index should contain all the php code from querying mysql, storing the query results in an array and passing this array to sell_view via the render function. The line doesn't belong here:

<form action="sell.php" method="post">

nor does any html code. This goes into sell_view. Sell_view can contain php, as you have done in your code where you're implementing a drop down menu.

1
  • Sorry, I had copied the wrong code in sell_index.php. I realized I didn't even need it, and did all the work in sell.php. – Useful_Investigator Jul 18 '16 at 6:32

You must log in to answer this question.

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