>> [MUSIC PLAYING]
>> ROB BOWDEN: Hi.
I'm Rob, and I hope you're ready to put stock in this solution.
First, let's take a look at register.
So remember that here we're checking to see if a form was
posted to this page.
>> So first thing we're going to do is go to the else.
And we're going to render the register form.
So the register form is going to post to register.PHP.
And what is it going to send?
It's going to send a username that the user is going to fill out, a password,
and a confirmation--
the password typed again.
>> So now when that form is posted to register.PHP we'll execute this if.
Looking at this if, we're first going to validate the input.
We want to make sure that the username and password weren't empty and that
the confirmation actually matches the password.
Once we've verified that we can actually register the user.
>> What does that mean?
Well, we want to insert the user into our database.
And this is how we'll do that.
So we're going to insert into the users table the fields user name,
hash, and cash.
>> The default value of cash is going to be 10,000.
And we're going to pass as username, username, through the POST super
global that's submitted from the form.
And we're going to encrypt the password.
>> So if that succeeded, then results will be non-false.
If it failed, then we want to apologize.
Something went wrong.
And what could have gone wrong?
>> Well, there needs to be a unique username.
And so the query could have failed if the username already
existed in the table.
So assuming it was a unique username, then we're going to query to grab the
ID of that user.
Remember that the ID is auto-incrementing.
>> And so if that happens to fail for some reason, then we want to apologize
that we couldn't grab the ID.
But assuming that it didn't fail, then we grab the ID from what the query
returned, store that in our session-- so we want to log this user in by
storing the ID in the session super global, and finally
redirect to our portfolio.
And that's it for register.
>> Now we're going to move on to quote.
So quote is going to have a really similar set up.
We see here that this is the code that we're going to execute when a form is
posted to this page.
But first we actually have to render that form.
So taking a look at quote form, what fields are there?
>> We see that all quote has is a single text box with the name symbol.
And so when quote form is posted to quote.PHP we're now going to
execute this code.
And the only variable in our POST super global is going to be symbol.
>> We validate that to make sure that they actually posted the symbol.
And if they didn't, we say you must provide a symbol.
Assuming they did provide a symbol, we look up that symbol.
>> Now remember that look up might have failed since, well, maybe it wasn't a
valid symbol to begin with.
So if this look up return false, we want to apologize that the
symbol was not found.
Once we've found the symbol, now we can render the quote.PHP template.
>> What does that look like?
That's just going to print that a share of whatever the stocks name was
is worth whatever the stock price is.
Now why do we use this htmlspecialchars function?
That's because the stock name and symbol might actually contain special
characters that should not be interpreted as HTML.
>> All right, so that's it for quote.
Now we want to look at index.PHP and portfolio.
But first we actually need to construct the portfolios table.
Here's how we're going to do that.
>> So let's take a look at the structure.
And we see that the portfolios table is going to have an ID.
So that's going to be the user's ID that's inserting the shares.
We have a symbol, which is going to be the symbol of the company that we're
inserting the shares for.
And then shares is the number of shares that is being inserted.
>> So remember that per the pset spec, we specify that ID and symbol--
we check out indexes, ID and symbol is the primary key.
So a user ID and symbol pairing should only appear a single
time in this table.
Now let's look at the code.
>> So now index.PHP is going to grab all of our portfolio information and
display it to the user.
So first, we're going to grab the cash that the user currently has from the
cash table.
Remember, that query is always going to return an array of arrays.
So even though we only selected cash from a single row, we still need to
grab that cash by indexing into the zeroth index of rows and grabbing the
cash index.
>> So now we want to select all of the information from the portfolios table
that's relevant to the currently logged in user.
We of course need to validate that that actually succeeded, which we
should always do whenever we query.
Once we have all of that information, the pset spec informs us that we
should do this in order to nicely store all of the information in this
positions array.
>> So we're looping over all of the portfolio information, looking up the
stock associated with each row in the portfolio information, and then
storing in the position array the name, price, shares and symbol all
associated with that stock.
And finally, we're going to render portfolio.PHP, passing in the amount
of cash we currently have, the positions array that we just
constructed, and the title of this page which will be portfolio.
>> Let's take a look at portfolio.PHP.
And we see that the major interesting part is this loop.
So we're looping over the positions array, creating a
table, where that table--
we're populating each row with the information that we put inside the
positions array.
>> Again, we need to use htmlspecialchars in case this symbol or the name
contain HTML characters.
And here we're multiplying the price and the amount of shares that we have
in order to get how much that's currently worth to the user.
And that's it for portfolio.
>> Now we'll take a look at sell.
So sell is going to go back to the format that we had in register.PHP.
We see that a form is going to be posted to this page.
But first, when we load the page, we're going to do this.
>> So what is this doing?
Well, we could just have the sell page have a single text box that the user
enters this symbol that we want to sell.
But we're going to be a bit more clever and we're going to have a drop
down that allows the user to select actual symbols that they already have.
>> So we're getting the user's portfolio.
We're going to select from portfolios all of the symbols that the user
currently has, the currently logged in user.
Make sure that that succeeded.
And now we're going to loop over the returned information, just grabbing
each symbol, and storing it in this symbols array.
>> And now we're going to render the sell form.
So the sell form is going to just be a drop down menu, a select.
And each option in the sell form is going to just print out the symbol
that we grabbed from the portfolios table.
>> So the sell form is going to submit back to sell.PHP.
Looking at sell.PHP, this is the code that's going to execute when we submit
to this page.
We want to validate that the user actually entered a symbol.
>> Now assuming that they did--
so now we want to determine how many shares the user is actually selling
and how much cash the user should get for selling that many shares.
So we grab the number of shares that the user has for this symbol.
We're looking up in portfolios for the given user and the given symbol.
>> Now make sure that that actually returned a row.
Because if it didn't, the user doesn't actually have that symbol to sell.
Assuming they do have that symbol, we want to grab the number of
shares that they have.
And now we want to look up how much each share is worth.
>> So we used the look up function.
We're looking up the value of the symbol.
Assuming that the look up succeeded, now we're going to actually update all
the information.
So we want to delete from portfolios the shares that we're selling.
We want to update the user's amount of cash.
>> And we're updating it by shares times the stock price--
so that's how much money the user just made.
And now we want to update our history.
So we haven't taken a look at the history table yet.
So we'll come back to this.
>> Now finally we're going to redirect back to the portfolio.
Now let's take a look at buy.
So, buy should be pretty similar to sell.
>> We see that we're again going to check to see if we're
submitting to this page.
Assuming that we're not, we're going to load the buy form.
So what does the buy form look like?
>> We see here it's just a regular form that's going to
submit back to buy.PHP.
And it's going to have a symbol that the user is entering, the number of
shares that the user wants to buy of that symbol, and that's it.
So when we submit back to buy.PHP, we're now going to execute this code.
>> We again want to validate that the user entered something valid.
So here we're making sure they actually entered a symbol.
Here we're making sure that they actually entered shares.
And here we're making sure that they entered an integer for shares, so
they're not trying to buy ABC shares.
>> Now we want to look up the price of the symbol so we know how much cash we
should subtract from the user.
Now we'll select how much cash the user actually has and make sure that
that succeeded.
Here we're grabbing the cash.
And now here, we're making sure that the user has enough cash.
>> So if the number of shares the user wants to buy times the price of each
of those shares is greater than the amount of cash that we have, then the
user can't afford that.
Assuming that the user has enough cash, now we want to insert into the
user's portfolio.
Well, we'll insert into the user's portfolio if this happens to be the
first time the user is buying that particular symbol.
>> But what if they already happen to have some Apple stock?
Well, now we're making use of the on duplicate key update statement.
So this is why earlier we specified that ID and symbol should be a joint
primary key, so that if we try to insert an ID and symbol that are
already there, we'll just update the shares to include the new shares that
the user is buying.
>> Now we want to update the amount of cash that the user has, since they
just spent some money on those shares.
And finally, we'll update the history table again.
Which, again, we'll look at in a second.
And finally we'll redirect back to portfolio.PHP.
>> So let's take a look at the history table.
Now remember that the history table is supposed to keep track of all buys and
sells that all users make, not just the current number of shares that the
users have, which is what portfolio is for.
So we're keeping track of the user that is buying or selling, whether
this particular transaction was a buy or a sell, the symbol that's being
bought or sold, the number of shares that we're buying or selling, the
price of a single share that's being bought or sold, and finally, the time
that this buying or selling is occurring.
>> And that's all of the history information that we need
to keep track of.
So when we looked at sell, we saw that we were inserting into history sell,
as whether we're buying or selling, the current time stamp, and the
current user, the symbol that's being sold, the number of shares that are
being sold, and the price of the stock at this time.
Similarly, in buy, it'll look almost the same.
The only difference is instead of selling, we're buying.
>> So in sell and buy, we're inserting into the histories table all of the
buys and sells that are happening.
So all history.PHP needs to do is grab the information from the history
table, make sure it succeeded, and render that information.
So looking at the history.PHP template, the interesting information
is right here.
>> We're looping over all of the transactions, printing whether this
was a buy or sell, formatting date time that we made this transaction.
Remember we need to use htmlspecialchars on the
symbol, just in case.
And finally, formatting the number of shares that were bought and the price
of a single share at that time.
>> And that displays all of the history information that we need.
And that's it for this pset.
My name is Rob, and this was CS50 Finance.