 Hello, this is Christian. Welcome to episode 4 of this PHP Stocks application using PDO. In this video, we're going to process the text file and insert this data. We're going to sanitize this data and insert to our database. So let's go and see this is done. Now, in the previous video, I've mentioned that I wasn't able to see the second row here. Actually, it's there, right? If you notice it one time, I actually able to highlight it, you can see there. The error is because we forgot to change the CSS in here. So let's fix that first, just do one of the errors in here. So the index file, and I also noticed I did not forgot to close my div tag here in line of 43. So after this for loop, I want to just close it. So close the div. OK, so now in the CSS, right down here, the last one, although I mentioned in my talks, I did not actually change this to be an even. So it should have been an even number here. And the one more thing here, the TD here, I know this will apply for every table. But if you want to apply only for the stocks table, then there's also, say, stock and then TD here, table ID. Yeah, those are the changes I needed to make in here. And just to see if it works now. So there we go. And the counter, I forgot to increment that. So it looks good. And just to make sure we can do, just add one more here just to make it really, really works on that support here. TD, TD, and then just some test data. So OK, and there we go. Looks good, right? So I like the color. OK, let's do the counter. And then we can move on to the next one. So the counter is in the functions right here. The counter should increment that. You can do it like we increment down here if you want to. Or just do here one time. You would just do that dot. You got to escape out and then go into PHP mode. Because you cannot put plus plus inside the string. If you do that, it's just going to treat it as a character. So it won't work. OK, so that would now increment the counter. And we can move forward with that one last time. OK, there we go. All right, so now let's go back and delete all the data. So go to the DB level and then click on this empty here. So this is the truncate function. We're going to delete that. And our index will be started back at 1 again. And that's what we want. And then now when we go to the get data, we're going to process this data and put a appropriate message. OK, so let's go and do that now. So right in the functions file, I'm going to have a function down here. And the first thing you want to do is to see if you can read the file. So you can write a function to like maybe you can not get the file, can get file, and then you pass in the file name. And here I'm just checking if the file exists or not. So you're not doing any processing here yet. If it does, it'll return true, otherwise return false. So this file name here can be a directory too. It doesn't be a test file name. We can also check for directory. So here I would say if the file exists right here, it's called the file name. If that exists, then return true, otherwise here just return false by default, right? Very simple like that. And that's all we need here. Really just simple here. OK, so that's done, right? That's pretty quick. Next one here is a function to actually process the data now. So we do the actual reading here. So we still need the file name and we need to pass it to the database. So I need the db object. Now this is the db handler, right? Not the class itself, but the actual db we define in here in the config file. If you remember, this guy right here, we pass it to the function. We're going to process the data here. OK, so before we do here though, I want to go to the index file and perform the logic up here first. The very top, if you remember, when we do a reset, if we do something, we do the stocks, this is when we load the data, right? So what should we do here? What should happen when we click the stocks and we load the data? What should happen? Well, the first thing is we go into read the data, right? From the file. When we check, we can get the file first. I want to say here, let me just put a comment here. Check if file exists, right? That's what we need to do. So we're going to put here a result, is SFN can get file, right? And we pass in the file name. I'm hard coding here, you should not do this, but you should put it into a string. Actually, we should do that. Maybe way up here in the file here. You could put the file is stocks TXT, okay? Yeah, so we put that there. And then so you're just passing the file name. If that is true, right? If it is true, and then we can go in and process the data. So now we say, I'm going to send to the process data file and I'm going to check to see if it's successful or not. So I'm going to return a true and false value back again. I'm going to do an SFN, process data. Here we're going to pass in the file name and then the DB handler. Okay, so if both of those are true, then we can emit a message successfully added, otherwise have an error. So you can check here, you can say you expect to be no error. So no errors, yay, right? So if the result is true and the success is also true, then we can display a message saying all data, the stocks data added, something like that. Otherwise, because there's an error. So the error will be, we add it to the error array. It would be, you know, couldn't insert data. So we know something failed. And you don't want to throw the message to the user because you don't want that to be shown either. So, and then we are good here. So basically this is it for this one, right? And so let's go and do the process data here. We have the file name and the DB. So back in here, we have the file name and DB. So what is the first thing we should do here? If we have the data, then we want to read the file name first, we already got that, we checked it already, that means it already exists. You could check it in here, right? Notice I did up here, I did it separately here. I could have done it inside the file in the process. And so maybe that's better, right? So let's do this, take this out and put it inside this function here. You check it in here, right? You check it, you can say if this, that function, right? If that's successful, if it's true, then only then will you proceed, right? Otherwise you can just say, oh, I couldn't do it. So in that case, I had to go back and change it. I don't have a result here, I'm successful. Just result, that's successful now, okay? So go back in here again and say if result, then I'll continue, right? Else you're gonna return a false, okay? You can do that or I'm gonna do a couple things in a way. Yeah, we'll return false here. If it fails, then we'll stop here, right? If it's true, then go ahead and process the data. So now what do we do? We're going to read the, open the file. So I'll do a fp handler to open the files, f open, and you just pass in the file name. And then also a mode, the mode will be for us, we just read it, so you put R here. Now again, I'm hard coding here, okay? Usually you put this into another variable, like a mode, and then you can pass that and you can use it for different purposes, but here we're just doing read, so that's fine. You can put out a variable and then you pass it a function as a mode, okay? But it's good for now. So if we're able to open the file, then put a comment here, open a text file for reading, right? And then now the next one is we're gonna read each line, read each line. That means we're gonna do a loop, right? We don't know when is it gonna end, so use the function called f-e-o-f and you check the fp. And if we reach the end of the file, then we're done. If it's not, so you put the not symbol here, if it's not the end of the file, then go ahead and read each line, right? So here we have the first line, it's gonna be the f, it gets, it gets here and the s means a string, and then we're gonna get as a string, so we can get the first line of the fp. So every iteration, this gets it, it's a cursor, okay? So cursor means it's gonna point to the first line of the stocks here, points that, it reads that in, and then now we have access to the variable inside the line here. So this line has this text here now, okay? So what do we need to do? Well, we need to parse it, we need to break it at each of the separator, the semicolon, and then put each of these elements into an array and then we can process the array, right? So here you would do a data equals to, you can use the explode function, explode function would do exactly that, takes two parameters, the first, as you can see, is the string, which is the delimiter with the separator, us, it's just a semicolon, okay? Again, I'm putting, I'm hard coding here, this is not ideal, so usually you could do something like this, I'm just gonna make it work, you can say here, mode is R for reading, and then here the separator, s, e, p, or the whole thing is the semicolon, okay? So that you can change up here anywhere else, you could just change it. So here then I change this to the mode, so this is actually better coding, right? The separator, and then the string would be the line. Okay, so it's gonna break this into an element of array inside this data here. So this data will contain one, two, three, yeah, three, four, five elements, all right? So I'm gonna show you what it looks like line by line. So at this point, this data array looks like this. I'm putting an array here. So the first one looks like that, and then put a comma, the second here, and then the third is a string, and then fourth, and then the fifth. Okay, so this is our array now, so you can see it's an array, so you can access it using the index. And again, we're gonna ignore the first index because our ID is auto increment, right? That's where our data, if you remember, when we process it, when we pass the data of one, two, three, and four, we skip the first one, all right? So here we go, we're gonna go in the process of data. We're gonna insert the DB, and if we're able to insert it successfully, then we'll keep going to the next one. If it's not successful, for whatever reason, if you fail at any point, then you wanna terminate the entire process, okay? So we're gonna check to see if it's successful. So if the DB insert record, this is what we call, right, insert record. Yeah, insert record, the table, and then the data. So insert record, the table is just our table stock. The data is this data here, right, this local data. If it's successful, then we do nothing, we go to the next one. If it's not successful, then we're done, return false. This will stop execution here. We'll exit out the well loop. We'll exit out the function and returns back to the success and we get a false, so before we have an error message, right? But before we do this return though, we want to close the file because we open here already. So that means we have to close it. So f close, the fp, and then we'll return the false and we're done here. And if it's not, no errors, then we go to the next line until we're done. After that, if the well loop is done, that means we're successful. So here we can again close the file. And then when we're done, we're done. And then we skip that and all the way here, you could return true down here, or just return true here, okay? Either way. Because once you reach here, or once you reach down there, then we're done. So you could return true as the default here, or right below here. Again, it doesn't matter where you put it. In this case, I put it in the bottom as a, it's gonna go down here anyway. Okay, so I think that's what we wanted, right? We turned something like that and then we're done. So when we check over here, we check for successful or not. If it is, all the stocks added and so forth. So let's go and give it a run. So back to the browser and okay, cross your fingers. Whoops, nope. SFN at line 70 didn't work. So line 70, is it in these functions? Like 70. Oh yeah, now SFN, it would be the, just can't get, sorry. Didn't catch that. The reason we call this because we're in a class, right? You have to get the, can get filed through this object. Okay, thank you IDE. Okay, okay, it says all stocks added. Okay, we can make sure it's there. There it is, ooh, lucky. Okay, so everything's all here, as you can see we should have 50 of them. All right, you can also verify the database. Just again, just refresh or just browse it. And there they are, all 50 of them. So notice it starts at one again, right, the ID. Now if I go back, if you see if I go get stocks again, I did another add. If you go view again, now if you go scroll down, you'll see that we have more than 50. So here we go, we have 51, all the way to 100. So basically you double the insertion here. We don't want that, right? We just want to add the first, that 50 I want to text file. So what do you do? Well, we have to wipe out the table first. Either that, right? You don't have to wipe it out. Or just check if this OD data exists. Because you have two options, right? Because the reset will do that, wipe out the data and then the get will be, you can check it. If it's OD data, then just don't do anything and otherwise you can start it. So let's go back and clear our data first. Go back again and truncate. Okay, so now we should have no data. Okay, good, let's go and just do that little process there. So here, right before we do the success here, before we process the data, you want to check to see if there's OD data, right? So you can do something like here. Okay, let me just undo that and go up here. I can say check if data already exists, okay? Then you can say the result, you can say exist, yeah, well, result is fine. Can make up my mind. Okay, so get all the records, okay? The table is table stock. Get the records, you check if there's any record or not, right? So you check the result and then the row counts. If the row count function has more than one record, that means there's OD data, right? Otherwise, then you process this whole thing here. So we're going to move that all the way to the end of this block right down here, okay? So you would do only if there's only additional data. If there is, you put message here. Say something like stocks, data already added. Okay, so we know that there's already data. Oops, too many punctuation marks. Okay, so let's give it a try. And, all right, so let's do that really quick. So if I add stocks, there should not be any, right? So add all the stock data, there they are. If I do it again, and there it is. This is stocks already added. Cool, huh? Okay, so the next part is reset. Reset is we're going to just truncate the data. And then that should be a really simple one. So let's go to the reset here. Now, what do we do here? Again, back to the process. What do you do? What's the first thing you do? You can just truncate it, right? You don't care. You can say if the data is already empty. You can do this again if you want. This could be a put to a function. Okay, put to a function and then you can check that to see if it's already or not. If it's not, then you can go ahead and then truncate it. That's another option too. You can do that if you want. But, or you can just go ahead and truncate it. If you don't really care, just go and truncate it, then just go truncate it. And I'll use the truncate to get the rows back, okay? So rows is, let's see, what do we do for the truncate? What do we get? The truncate, I mean the DB truncate. When we truncate, we return a query. Okay, so the query we turn in the true or false. Okay, so, is that right? Yeah, I think so. Yeah, so we're gonna do a truncate. Either successful or not. So we're gonna do a truncate table, pass in the table, call stock, okay? If, I shouldn't call a row, should just do success. If successful, then we'll just put a message saying, all data removed or reset. Maybe I better stop using this exclamation mark. Sounds like I'm mad. Otherwise we have an error, right? Some kind of error. So errors, we're gonna patch that with the error message, say something like could not delete or truncate. Something like that, so we have a message. Maybe just say could not delete, however. Okay, let's save that. And I think that's all they have to do it, right? You just do it. I'm forcing it to be truncated. Again, I mentioned earlier, you could check to see if there's any records first. If you do exactly the same as this, if the count is greater than zero, go ahead and truncate, otherwise you say nothing to truncate, okay? But this should do fine. So let's go and give it a try. Here we go, reset, all data removed. If I do it again, I get the same message, right? Because that's what it does. Now if you go view it, there's no data. I get stuck, and here's my stuck added, and there's my data, okay? I do a reset again, remove, and there we go, right? You notice when I add it back, my data always starts at one. I mean, if you look at the, this is not what it's like counter is different, but in the table, you see it all starts at one again because of the truncate, right? Okay, I think that's it for this one. So I hope it was helpful. If you have any questions, let me know.