 Hello, this is Christian. Welcome to episode three of this PHP Stocks application using PDO. Now let's go and then modify our code to include some logic for our application. So here is the site. We previously created the DB file already to access the database. We'll, as you can see, it's already working, right? There's no error. So let's go into the code and do some more logic in here. So if you open the index file, now we need to go ahead and put some logic in here. So what should happen when we click those buttons, right? So we're going to have, I think, just three functions, three main functions that we want to do. And so what that means is when we click the, if you go to the link, the functions page, right? We click the home page. There is no parameter pass in the query string. And if you do the reset, then we check for the T reset stocks review, okay? So in the index file, I can close config. Now let's close that as close these for now. Well, I'll leave that open. So up here, let's do the logic first. And I'm just going to go out of the way to the left side. So I want to set a variable up here called task, okay? Or set it to blank. That is going to be the task either, you know, the reset, views, or stocks, okay? Those are the ones. So when I visit blank, if it's blank, then we will show the landing page, which is this right here. And this will be replaced with a variable. Let's replace that with a variable called message. So we display the message by default, if it's no task. So I'll put the bear, the message here equals to the default message, which is that message, okay? So by default, it will display that. And then we also want to display a, and then if it's, well, we'll go as we, we'll create more variables as we go. But the first thing is we do check for the get, right? So it's set the get for the T, right? We use that T. If that is set, then go ahead and then get the task. Task will be coming from the get of T, okay? Whatever that is, we don't know where it is. We can just, we can switch it, or you can just use an if block here. So which is to if here, okay? So the first thing is to say, let's wipe out, wipe out the table first. This is, that means if the task is equal to a reset, right? If it's a reset, they're going to wipe out the database, if that's the case. Else, if the task is a, what is it? Stocks, then we're going to load the stocks, right? So here we'll put check, what should we have? Yeah, you load, load stocks, data to DB, okay? I will read the file first, but then we'll do that. Else, if it's a, a view, then we're going to show the information on the table. So here we'll basically, first thing you do is you check, right? Check the, if the data is, well, no, no, we're not checking anything, we just get the record. So we're going to get the records, get all records from the database. So you do that very simple. You can say something like row, I'm doing an easy one first, okay? As you can see here, row equals, we have the DB object, get all records right here. We're passing the table, and the table is table, not user, table stock, right? We pass that to the function, and then when it comes back, you can check to see if there's any records or not. If there are no records, then there's no point of displaying the data, right? If there is at least one record, then we show the data. So here we can check to see if there is any records. So if there is data, then, well, there's no data, right? Then display a message. So that means you're not going to print the data. So here we'll do, if the row, I should put a row, because it's plural, right? It's common to do that. If the row of the row count, if it's greater than zero, I mean it's equal to zero, if it's not. If it's zero, that means there's no data, no records. So therefore, we'll set the message to say something like, what do we say? No data. Yeah, no data. I'm making it a little more interesting. There are no data, okay? And then we'll set the, yeah, that's all we do. And else, otherwise you don't need to show the table. So we have another variable. Maybe show table is equal to false, okay? No, I mean true, right? True. Otherwise, we do have data, so therefore show the table. So that means up here, we're going to have a variable up here, call it show table is equal to false. Okay, so we check the false table on that. So when we do that, then that means down in the body, we can, you know, perform this logic, right? So here we have the error message. So what do we do? We just basically, you know, display the messages. If there's any message, otherwise we don't do any, right? So we're going to have an error. Always check that first, right? If the error, we're going to set that. If there is set, that means we do have errors. Otherwise there's no error. And then we're going to echo a message, something like a div. I should have had, you can do it differently, but they use the error message. We'll create a classical error message to make it red and things like that. And then we will do a loop to display all the messages just in case if there are any. And the errors will be an array, okay? So we'll say for each errors as error, oops. And then we're going to print each of those out. Put an h1 here, and then we'll put a star, and then the error, and then we'll just end that here. We'll display those out to the place here. So let's go up here and create our errors before I forget. So we have here, we have, actually, no, we don't have to do that here because when we set it, it could be down here, right? We do the database connection stuff. We'll put the errors down there as we go. But for now, that part is done. And then show the table, right? We show the table we check for. What are we checking for? We could, you know, I could just leave that out. It's still in the same space. So we will put all the way down to the end of this list here. End it down here, okay? And this will be an echo. We'll echo that out for that landing page. And this will be, let's put a comment here for that show landing page. This will be for the show table. Okay, so how do we show table? Well, we check to see if the show table is set. So if show table, if that is true, then go ahead and then show the table, right? Otherwise, then we show the error, or the standard message down here. This is not landing page. Whatever the message is. So this could be something else. I show table here. And then we just display the rest of the messages here, like not found, or we delete the database, which you see, you know, it's deleted and things like that will put that message in here, okay? So the table, all we have to do is then what do we do? We're going to call a function called maybe show stock, show stocks, or table, right? Something like that. And then we pass in the rows. Because this rows here, remember we up here, if we do have some data, then we create a variable called rows, and we pass in the rows down to that function. And we don't have to check it because if we get to this point, you know, that means the rows has been created. If most of this tells you says it's probably undefined. Yeah, because we don't know that, right? So it doesn't know that. But once we get to this space, it's already been defined. So we're good here. Otherwise, we'll never run this line of code anyway. Okay, so this is the logic down here. So I think that's pretty good here. And so let's go up here again and see what we need to do. So this part here just for reading data, that's fine. And so we need to write a function called show stock data. Okay, so I'm going to copy this and go to the functions page and write a function. Let me collapse these. So let's save it some space. I'll put it right down here. Function, oops, show stocks. And so this is where we do our table, right? What it should look like. And so what does it look like? Well, I'm going to do a table of five columns. And we'll show each column with a counter. We're starting from one. And then here, let's build a table first. And then we'll close it back in here again. Okay, so what does the table look like? Well, table and t-head. Then each of these will be t-h. We have the number to counter, right? Duplicate that five times, four times. This is the company name followed by this symbol. And then the sector and the price. Price here we'll put here in dollar. So we have five fields. And then down here we have the t-body. And then we have a tr here, right? So this is the repeating stuff, the t-d here. One for each. So this is where we do the row. So now we go back to the PHP mode. Not pp, hp. And then we're going to process that. And then, yeah, so let's do this first. So we're going to say, we're going to print each row, okay? Each row of data. So we do a for each of rows, okay, as a row. I wish you just like the dollar sign. So for each row as a row, then we're going to put that inside here. And I'll just echo them out, okay? So echo the row, the tr. And then we echo the t-d. And then we echo the tr again, the closing tr. Okay, so in here is we'll put the information here. This will be for the first one, it'll be the counter, right? So counter, no, if I do this, I have to put the counter. You know what, yeah, that's fine. So I have the counter, I'm going to have the company symbol, sector and price, okay? So this would be the row of the company. And then what else do we have? The row of the, oops, why doesn't it like it? I can't use a row for reading. It has to be this way. Let's see what's going on here. It's been a long day. Okay, well, it looks like it doesn't like this. So I mean, you will probably end up using the dot like this. And which is fine, but well, I should have just used the standard text. Now what's going on here? All these two down here, let's fix this to echo that. We'll combine these two together. And then we're good here. Okay, so that part should be good. And then what else? That opened the PHP here. And then I have to close it down here. Take this out. Yeah. Right. Yeah, we'll clean up this later. Okay, so let's duplicate this a couple of times. Company the symbol. This is the sector. And then the price. Okay, so that's our table. And just again, right click and I can't do here. Okay, well, code reformat. All right. So that is our table. And again, we won't know until we add data to the table to the database. We can go and just, you know, do one and see how that looks just to make sure it does work. We can do that. So what else do we need here? When for the price, I'm going to format it to use a dollar used like currency. And so you can have something like you can wrap this with a function called a number of format. Okay, so you can do something like number format. Let's send in the price information here and then follow up on the decimal number like two places like that. So you format your number with a two decimal place. Then it will have a comma after the thousands place. If you don't do that, it just look a little bit weird. So let's save that and see if we call that in here. So that's good. Why don't we go to the database and just make one record in there and see. Well, before we see, let's see if this works first. Make sure we have no error, right? So we're going to use stocks. See, we have an error. We couldn't connect her stocks, host, local host. Okay, do I not have it correct? Or is it? Let's see her stocks. Yeah, I call it her stocks. And I don't have any data, but let's go and add some data in here first. So let's go back to the loading port, just to insert where to go. Let's insert some record in here. So this one will be just, you know, ABC and AA. And then sector will be just a news, right? And price will be 123.45. Okay, so we add one record to that. So make sure we access the data. Now let's go back fixing why line 27 didn't work. Okay, local host for, I thought we had that correctly set up. I guess you never know. So here, no, the config file. DB is, yeah, it's called her stocks. And then inside the class, we just call the host. Make that DB. Let's see. We set, well, none. We set the DB, DB host host driver driver. Okay. Well, let's see if something is wrong. Let's check in here. I think it has to do with the connection. Okay, let's see. The driver colon and then name colon. I think right here, this is the TPSM colon. Yeah, I should have not included in here. I should have put that outside. Because this is not part of that driver. This is mainly just this part, right? The driver colon DB name that and then you host will be in a separate one. So I think that's it. And then we'll, I could have put the host up here will make the connection. Get the SN and then host user DB pass and so forth. All right. Anyways, I don't know. Let's see if that's correct or not. All right. Let's go back and try it again. Okay. So here we go. Yeah. Actually, we did pull some data already. You can see it's not centered yet, but we have to style it. As you can see, it does work when I do a view stock, right? When I do a reset, then as you can see, there's no data because we did not, we didn't show the view. The homepage will also not show anything because we probably didn't do it. We probably didn't do it. Did something there correctly. And then the view stocks does work. So looking good there. So let's go find out why the message was not been displayed. When we click the home should show the message here. Again, could be a typo. Someone let's check it out. Trouble shooting time. So the main show your message. Each one. Oh, yeah, it has it's commented out. It should be right there, but it's coming to that. Okay, let's go and check why. And right here. Well, yeah, because I, you know, I use it as a comment. Escape those. Take that out and just show the message without the thing there because we'll actually echoing that out. So now I should show the message. All right. It got me before in the previous videos too. So there we go. Home page will show us that reset will show a different message with something like once we set it would say, you know, data's been wiped out, whatever it is. And then we get the stock. We're going to perform a read file, process the data. And then when we review stocks, we either show the table here or we show nothing. So to end this video, I'm going to go and style the table and then we're done. All right. So let's go back in here to the file and go to our CSS. And so we have a table. What do we, what do we do down here? So let's go down here and make some spaces. Put it here for a table. Okay. So stocks table, we call it stocks table. I'll give it a class and we call that in our function. Let's give it a name here. Okay. So the class will be stocks table. There you go. You call it that. Just make it so that just in case if you ever change it, right? That's what we always want to do. Make sure it always think ahead. So stock table, we have the margin. I was set to zero, top and down. And then left and right, make the auto. So it will be centered. Okay. It's centered on your table. And then we'll add some padding. Probably that's good. The width of this table will make it so that it's it will maybe like 65%. And then we add some border. Just for solid black border. And you want the text aligned to be maybe text left. Okay. You don't want to center because the company names and things like that are a little bit weird. And then we want the background color to be, I want it black. Okay. Black switch has a nice border around it. So let's see if you go back and just to view this quickly. Did it work? Oh, there it is. Okay. Well, almost, right? It's black, but then we'll have to show each row here in white. It has a different color. So that's looking good. So let's go and then do the table head. Stocks table to T-head. We want that to be the background color of, come on, background color. Let's do a dark green. Yeah, dark green. And we want the color to be white off white so we can see the text. Okay. So that's the head. Let's do the T-body. I mean the T-H for each column. You want just some padding here. Okay. So 10 will be good. And then the T-Ds for each column in the body. Kind of same. So you could just put here T-D or just keep those as same for each of those. So down here we have, what else? The stock table, T-body. Okay. I want those to be striped. So put a TR here. The nth child. And we'll put for the odd. The odd child will have a background color of, let's do the green, yellow. Yellow, green. Okay. Yellow, green. And then we'll do here for the even. Okay. So copy that. Put it here. The even. What do we want for the even color to be? Blue. Oh, Azure. Okay. Well, Azure looks, I guess, looks kind of like green and blue. I want the green theme, right? That's what I'm doing here. And then I think that's it for this one. Let's go and check it out. There we go. So it looks good, right? As you can see here, I think it's formatted. And let's go and just make one modification here. Let's add another one here. Maybe, and then B does matter. The price would be, you know, something big like that. Okay. So if you go ahead and execute that again, you should have now two records. And then, oh, it didn't do it. Oh, maybe I didn't save it in here. Let's go try again. I did save. Actually, I have two records. And why didn't do that? Yeah, I'm not sure why, but we'll check it out. But at least, you know, it should be, you know, TO, I mean, this Azure, and then it goes back to the yellow and then, and so on. But yeah, I think we're good here. So the next video will fix this next video. And also we'll do the other functions to process the files and insert those data to the database.