Hot answers tagged

4

The form has an input element whose name is "symbol". The spec requires a select element. Require that a user input a stock’s symbol, implemented as a select menu whose name is symbol. Here is doc on select from MDN.


4

The request method is GET by default. When the user visits http://pset7/sell.php, she is requesting the page (i.e., sell.php in this case) using GET. Therefore, the condition $_SERVER["REQUEST_METHOD] == "POST" is initially false and the else branch is executed as a result. To fix this, you need to render the template if the requesting method is not POST (...


2

the notice tells you what exactly the problem is -- there's no index called "shares" in stocka. recall that query returns an array of rows, even if the result was a single row, in which case it would be in the zeroth index.


2

if you look at your sell_form.html, you will find that at this line <option value="symbols"><?= $total["sellsymbol"] ?></value> you have an attribute value set equal to "symbols" you're always passing the string "symbols" to the server side, so whenever you use $_POST["symbol"] on your server side, it's actually as if you're using the ...


2

First, for closed tags, if you open a tag, you should close it. I see you're not closing the <select> tag. More information about that tag maybe found here. Also, you have </div> within the body of your for each loop which means </div> will be there n times where n is the number of iterations that this loop will iterate. Next, you probably ...


1

check50 is looking to see that if you bought, say, 5 shares of ABC and then sold 3 shares, your index page will show the remaining 2 shares with a value using the current quote. (since that is what your index page should always be showing ... current holdings). So if ABC was worth $28/share and you had 5 shares (worth $140.00) and sold 3 shares, then your ...


1

I suspect you copied buy.html to create sell.html form. I also suspect the form action was not updated to "/sell". The first query in the "strange" does not exist in this sell function. It would likely exist in the buy function, though.


1

Your guess: is that it has something to do with a rounding error as it is looking for a specific number. is probably right. Usually using the 'usd' function in helpers.py will fix that right up. In quote you likely need to format price returned from lookup. The buy problem would likely be coming from index.


1

You forgot the : for :stockprice in VALUES (:userID, :symbol, :shares, :amount, stockprice). And to quote Cliff B: If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)


1

Problem is with line db.execute("INSERT INTO history(symbol, shares, price, moment, id) VALUES(: symbol, : shares, : price,: moment, : id)", symbol=info["symbol"], shares=shares, price=usd(info["price"]), moment=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), id=session["user_id"]) (which style50 wouldn't like for being too long) Use :symbol, ...


1

Those errors all mean that there's an earlier test that failed and has to be corrected before the remaining tests can run.


1

Your guess is correct: the lookup function isn't finding symbol. The problem is here <option value = "Symbol">{{ stock.symbol }}</option></option>. "symbol" will always have the value "Symbol". (And it looks like it will create invalid html because of </option></option>, which can also cause problems.) Any print statements ...


1

Just after I posted a question i got it. I should put all data manipulations under else from request.method == "POST".


1

It is right. CS50::query() always returns an array of rows, even when the result is just one row. That's why you should use the [0], as you have correctly done. You can ctrl+F this: // first (and only) row $row = $rows[0]; inside the login.php file, which was written by the staff, and you'll see that they, too, had to use $rows[0] to extract a $row. ...


1

The query returns what? An array. From Pic 1, $rows looks like this: $rows[0]["symbol"] = "ATT"; $rows[1]["symbol"] = "MMM"; $rows[2]["symbol"] = "X"; At the foreach in Pic 2, $stocks is an array of arrays, each $value is an array. Thus the echo provokes the "array to string conversion" error.


1

Maybe the problem isn't $ticker[0]["shares"]; there are two operands in the statement. From the spec: Assuming the value of $_POST["symbol"] is a valid symbol for an actual stock, lookup will return an associative array with three keys for that stock, namely its symbol, its name, and its price.


1

// to make sure the stock is in the user's stocks $rows = CS50::query("SELECT symbol FROM stocks WHERE id = ? AND symbol = ?", $_SESSION["id"], strtoupper($_POST["stock"])); if ($rows["symbol"] === false) { apologize("You do not have any stocks in that company."); } Instead of doing this, why not check to see how many $rows are returned by your CS50::...


1

You are using the wrong return value. From the pset specs: If query is instead passed a DELETE, INSERT, or UPDATE statement, it will return a non-negative integer that represents the number of rows deleted, inserted, or updated, respectively.


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"> ...


1

In the line $funds = $money[0]["cash"]; you are trying to access first element of $money array. However, $money is not defined as an associative array, it just contains a numeric value.


1

To get the price per share, try: $price=$temp_array["price"]; and to get number of shares, remember that query returns an array, and you want the 1st (index=0) row. So try: $numshares=$temp_array2[0]["shares"];


1

This means that $positions array don't have the key value in it. Check isset($positions["value"]) before hand.


1

Just like _POST your positions is an associative array. Just as you get $_POST["symbol"] you get $positions["price"]. If I understood correctly you want to do something like the following <?php $values = [ "name" => "GOOG", "price" => 3.5 ]; printf("Name: %s\nPrice: %f\n", $values["name"], $values["price"]); ?> ...


1

If you want to use the popup window for any browser you probably want to use javascript. The popup window you have above is called a confirmation box. Here is how to use it: function foo() { confirm("Message to be displayed"); } The above function should be inside a <script> tag. Then on your <input> tag with type="submit" also define the ...


1

i presume you have got the variable $number from the function lookup. this gives an array of associative arrays, (although there is only one associative array) try $numbers[0]["shares"]


1

Try the following <option value="<?= $symbol["symbol"] ?>"><?= $symbol["symbol"] ?></option>


1

If you're submitting using POST as a method, then you should be able to access the currently selected item in the list from sell.php using something like $_POST["stock"] If you're submitting using GET as a method, then you should be able to access the currently selected item in the list from sell.php using something like $_GET["stock"] where "stock" is ...


Only top voted, non community-wiki answers of a minimum length are eligible