William Rosener: Hello this is Dr. Bill. Welcome back, and this video will be looking at looping you seen PHP. And as I mentioned with the JavaScript.
William Rosener: The loops here can save us a lot of time if we've got some code that we risked repeat over and over and just kind of reduce it to the for loop or here's how we're gonna do something just that we did in JavaScript.
William Rosener: Going to put the numbers increase in order to whatever value is in this text box decrease in order will print them even numbers in this range.
William Rosener: And will print a square and I'll probably see you guys can figure out the multiplication table at least them. Okay, well let's get started here.
William Rosener: So it takes. I'll tell you what, let's just plug it in a couple numbers here on let me plug in, for example, eight
William Rosener: And the only differences. This is a PHP. So you got to look really quick up here and hit display results. It goes to the server and it comes back and in this case it's going to believe, Arizona.
William Rosener: And just like that, it's got the answer. So if I put an eight. We're going to get the numbers increase in order
William Rosener: Decrease in order to even numbers 22 and a credit little square and multiplication table all the way from eight to eight there and let me just do one more here. I'll do say 15
William Rosener: And now we get the numbers increasing or decreasing the even numbers are big old square and the multiplication table all the way to
William Rosener: 15 by 15 there. Okay, let's get started here. First step, I'm gonna do is open up some basic editor. I've just got a notepad open here.
William Rosener: And then I'm just going to grab a basic form here. I'm going to copy it and let me come right over here and paste
William Rosener: So just take a look here. I've got my HTML HyperText Markup Language. I'm gonna start at the top, close it here. Here's my total a ball type in PHP.
William Rosener: Type in the for loop, then let's go ahead and grab this code right here. I'm going to copy it and paste it right here within my header to tag.
William Rosener: And take a look at the pattern. And again, like the previous video, lot of times I'll just simply go zero, which is the top right bottom and left. So the only padding. I'm going to add is to love just move that off a little off the chair.
William Rosener: Little header to tag, put your name in here, the paragraph I've now got a basic form here if you want. We could give this introduction
William Rosener: Let me just type of form and I'll do the same thing here, on the idea
William Rosener: We're actually are not even be using the ID in this case here, or the name. So, but that's fine. Let's go ahead and give it a little name case for you later on, decide to work with something that does need the idea there.
William Rosener: Okay, tell you what, let's go ahead and not tell you I can put my name here.
William Rosener: So go ahead and save this and then we'll upload it to the website just to make sure we got this information working right here. So to save it. I'm gonna come up here to file. I'm gonna go to save as
William Rosener: Make sure you know where you're saving nuts. I'm going to call this one assignment nine
William Rosener: Dash PHP dot PHP. So three things here. Make sure you know where you're saving it. I'm saving it as a director called a temp.
William Rosener: I'm going to make sure it ends with PHP so assignment nine dash PHP dot PHP and it's a text document when you get everything done. Go ahead and press Save.
William Rosener: And if I come over here. Now I can see here it is Simon on PHP. But what I want to do is now come over here and upload some hit View refresh. And I'm gonna take my assignment nine. I'm just going to upload it to a directory called temp and I'm just going to simply right click and upload
William Rosener: Okay, so we were simply looking here into the completed. I'm not going to come back.
William Rosener: And here it is. And let me hit the refresh reload button. And here's my assignment nine PHP. Okay. And that's all we've got so far is we've got a little header to tag. And we've got our name there.
William Rosener: Okay, well let's maybe start off with the body. I mean, maybe down. Yeah, the body here working on our forum and I'm going to save a little time here and I'm going to grab just a little carrot here.
William Rosener: So I'm already starting the form. I'm gonna
William Rosener: This is going to close the form. So I start the form I close the form when I press on this here command button.
William Rosener: Display results. What's it going to do, it's going to call itself the PHP self them. Okay, so now I've got an input box with a max value and a text or a tag. Let's go ahead and tell you I will go ahead and save this.
William Rosener: And with PHP every time you want to view it your change. You have to upload it to the server. So upload it.
William Rosener: I'll come back here and I'll hit refresh. And that's what we should have at this point right here, nothing's working yet but
William Rosener: We should have a basic text box with a command button and this text area tag. So again, when I press on display results. What's it going to do, it's going to call itself over and over and maybe with that in mind, let me go ahead and just plug in some values down here.
William Rosener: Well, I can just grab it, or tell you. I'll just come right over here. And so when I call it a second time I need to make sure that I keep passing this value. So whatever user
William Rosener: Number. The user has entered here when I call it a second time or third time I need to keep passing the value from the previous one.
William Rosener: And to do that, I'm just going to start a little PHP so little angle bracket, question mark, PHP, is going to start the PHP I'm then going to just simply echo what's in the variable.
William Rosener: max value simply close by PHP.
William Rosener: Okay, right here, rather than displaying
William Rosener: I'll tell you what we can put something there but table. Let me just go ahead and
William Rosener: do the exact same thing here. I'm going to start my PHP and this closes the PHP and then right inside. I'm going to simply ECHO.
William Rosener: Whatever information.
William Rosener: Is in the variable temp string. Okay. And I'll tell you why. Just so we actually have something up here working. Maybe we could come up here to the top.
William Rosener: And sometimes actually will place this again. If I'm working with images I definitely place it at the very top, because if you have anything else trying to get displayed initially, you're going to find with images. It doesn't work. So maybe I'll just come up here to the very top.
William Rosener: And let's just go ahead and put well we could do max value.
William Rosener: So big thing here with a PHP is variable. Start with $1 sign. And let me just go ahead and for now and we'll be modifying this I'm gonna say temp string equals yes or test just
William Rosener: Put something in there to know that we're working OK, so the first time we call it are now going to assign a value to max value value to temp so it's going to put a five here and it should put test in my text area tag. Let's go ahead and save that.
William Rosener: I'm gonna go ahead and upload this
William Rosener: Come back to my browser hit reload.
William Rosener: Or what I can also do is come back here and press it again. And sure enough, I'm now populate in this value and I'm getting test inside there.
William Rosener: Okay, let's sell. Let me grab a little code here. You probably already got this code here. Let me just
William Rosener: You're going to find if you go out there on the web and everyone kind of uses the same code here and it's just going to make sure
William Rosener: That whatever value. So again we call ourselves but call this code over and over and over again. We want to make sure that there's no malicious data.
William Rosener: In here, that's just going to, so we're going to test her input on data. Well, what am I going to test me about us. Go ahead and grab this as well. This is going to be a little bit unique here.
William Rosener: Okay, so we've seen this line numerous times. So what's going to happen is it calls itself. It's going to post the information and effect call itself.
William Rosener: using PHP south. So, it calls itself. So the first time through, it's going to use the default value of max value from now on.
William Rosener: It could use Mac, they could use this five, but where it's going to use the new value that the user has entered. So I'm going to take a look at max value.
William Rosener: And that's not explanation point there is not empty, then I'm going to go ahead and say, Okay, call test input with this value and then store it.
William Rosener: And the variable called max value. So just make sure it's going to take again what's ever in there. Make sure there's nothing stripping out special characters, etc. Like delete Asterix Asterix except for them, making sure there's nothing in there. Okay.
William Rosener: And we're now ready to actually start working on some code there if you want to
William Rosener: We could check it out. A real haven't done anything yet, to be honest. Um, so let's go ahead and get just a little code in here.
William Rosener: And I'll tell you what, we'll start off with, we take a look here. I come back under completed the very first separate one do has habits say increasing order.
William Rosener: Okay, let's for the first step. I'm gonna do is I'm going to come along and I'm going to go ahead and tell you what I'm going to delete this line right here. Well, I won't delete it. Let me move this on up here.
William Rosener: Say, I'm going to say temp string is equal to the string. Now I'm going to go ahead and say, we'll start our little loop here. Very similar to almost identical to a JavaScript here.
William Rosener: One huge difference. There were some variables on PHP have that dollar sign here.
William Rosener: Another thing is just as I typed it there and JavaScript the concatenation was a plus sign here, it is a period
William Rosener: increasing order.
William Rosener: And a little in new line their picture is lowercase.
William Rosener: OK, so I'm going to take what was intense trying
William Rosener: to concatenate increasing order. So it's always had this static string here, increasing order. Now we can actually start our loop here.
William Rosener: And x is equal to one.
William Rosener: All index as less than or equal to
William Rosener: max value.
William Rosener: And just like in PHP. We can or JavaScript, I can just put plus plus, or I could put an x equal x plus one.
William Rosener: Okay, we'll go ahead and start it and close it and now here. I'm going to go to Australia and I should make a mistake here. I'll tell you, I won't make a mistake, quite yeah but let me get this much work and then we'll make a mistake, just to show you again one more time on that error log
William Rosener: Okay, what do you like we did on JavaScript. What kind of crown these numbers in here initially
William Rosener: Okay, so at this point. What are we doing well, first of all, we're going to go ahead and when we press on this here button called display results.
William Rosener: It's going to post the information, calling itself. And what's it going to post, it's going to transfer the value in max value.
William Rosener: So it's going to come up here. Initially, it's going to set max value to five. But then that's going to come down here, like the second time we've called it the user is under 10 and it's going to go.
William Rosener: Okay, as a new max value, is it not empty has a value been posted to this PHP function. If so,
William Rosener: We're going to take this we're going to test it. Make sure there's nothing wrong with the data and then store it in max value. So maybe the first time is five.
William Rosener: But then after that, maybe the user has changed it to 10 and which point it's going to come down here and override the initial default value there.
William Rosener: I'm not going to set Tim string of crypto THAN AUSTRALIA. I'm gonna concatenate Tim string concatenation with increasing order slashing new line.
William Rosener: I'm now going to start my loop. I'm gonna set my initialization phases index equal one wall index is less than, let's say, five or 10
William Rosener: And then I'm going to be included in it by one here. I'm gonna say Tim string as he was whatever's in 10 string concatenation with index 12345 except for them. Let's go ahead and check this out. Come to File, Save.
William Rosener: Come up here and reload this
William Rosener: And will come along and here I I can press back or I could hit the refresh button. And sure enough, it is working here and not table. Let's let me even
William Rosener: Change this to attend and display results. And sure enough, it's working. And again, it's hard to sell this thing is going so quick.
William Rosener: I'm in scale in several states over the Arizona and back. But look right up there and you can see how this is actually calling itself on the server and the results are coming back here table. Let's go ahead and throw a little space in here.
William Rosener: And to do that, autumn to do is come along and concatenate a little space.
William Rosener: So one other thing here before we even check it out, let's go ahead and let's get ready so that we can start adding some more information and Alma do is take a look what's in Tim string.
William Rosener: And or cannot cabinets.
William Rosener: To lung feeds harem new lines. Okay, that we, we've got. So the first one to tell you what, let me go ahead and I say this
William Rosener: Well, upload that
William Rosener: And our refresh.
William Rosener: Answer, not forgetting space. So the first new line simply causes it to advance the cursor to advance here. The second one is going to move it on down.
William Rosener: Okay, well let's go ahead and just simply copy and paste this code. So I'm just gonna, I'm not going to copy that copy this first one here, well then I'm going to start off an effect temporary was going to have no value. So I'm gonna copy from here on down. I'm going to copy that.
William Rosener: I'm going to paste in here. I'm going to go in decreasing order.
William Rosener: Okay, so just like we did with JavaScript, rather than starting with one I'm gonna start with max value.
William Rosener: And now, rather than index being less than or equal to max value. I'm going to continue as long as it's greater than or equal to one.
William Rosener: And here again, I've got the two choices. I can put dash dash or I can put an X minus one, either either approach there. And I think everything subset there decrease in order everything there. Let's go ahead and save that.
William Rosener: Upload it
William Rosener: And will come along and I can either hit the reload refresh button or I can come back.
William Rosener: At assignment nine. And sure enough, I've got the increasing order decreasing order.
William Rosener: I can change the value. And sure enough, it is going from this case one to seven.
William Rosener: Okay, let's move on. So next step here was
William Rosener: This with a completed. We're not just going to do the even numbers.
William Rosener: Okay.
William Rosener: Very similar to what we just got done doing. I'm not going to grab all this code, copy it.
William Rosener: Paste it. And I'm going to type in the even
William Rosener: Numbers.
William Rosener: Okay. So in this case here, what am I going to start off with. I'm going to start off with two
William Rosener: And I'm going to go is index as long as it's less than or equal to
William Rosener: max value.
William Rosener: And in this case, I'm going to be incremental. Now here's the first time. So up until now, we've been able to get either plus plus or minus, minus, but in this case I now need to make sure
William Rosener: I'm gonna have to kind of spell it out because I'm going to go index equals index plus two. So the even numbers I'm start off with index equal to two.
William Rosener: Well index has less than max value for example to 10 so it's going to go to 468 10 and committed by to concatenate and that along with the space and then these two things are now getting ready for when I want to input some more information. Let's go ahead and save this.
William Rosener: Go ahead and reload.
William Rosener: And
William Rosener: Press on assignment nine. And sure enough, looks like it's working fine 2468 what I meant to show you guys earlier was let's assume right appear for example I had just left off.
William Rosener: You just got done working with a JavaScript. Now you're in a PHP and it's really, you kind of like, oh wow I forgot about putting that dollar sign in front of a variable. So if I was to save this now.
William Rosener: And upload it.
William Rosener: Take a look right over here. I currently have no air log. If I was to come along here and hit display results I get a big old
William Rosener: blank screen just because I'm one little mistake there. But if I come along, over here and hit View refresh. If I was to download this 100 actually have to right click and download
William Rosener: And then take a look at that file.
William Rosener: And drag that on over here. Sure enough, I can tell me there's a line on air online 18
William Rosener: And if you're an account, you're going to find that seating. So again, this was one of the reasons why I mentioned earlier in one of the other videos.
William Rosener: You're going to eventually want to use some other editor, like Notepad plus plus that has the line number. So let's go ahead and save this one more time.
William Rosener: Just to make sure we don't have a little error message and tell you what I'm going to delete this way. If I get another error message. I'll know it and I'm going to simply right click Upload go right back to where we were.
William Rosener: And assignment nine. And let me just check this out.
William Rosener: And sure enough, everything's working here. Okay, one last step here is to take a look at the completed. I'm gonna let you guys do the multiplication table. I'll get you guys started here on the square, this one is going to be used and just like native JavaScript nested loop in
William Rosener: So I'm gonna come back over here.
William Rosener: And so we just got done doing decrease in order, we did the even numbers. Now I'm gonna do is I'm just grabbed this code right here, copy it. Paste it and I'm going to put square here.
William Rosener: Okay, just like I did with JavaScript, just so you can kind of make sure you know, follow your lips here. I'm gonna call this one index one index one index one
William Rosener: Okay, let me go ahead and copy that.
William Rosener: I'm going to paste it and change this to index to
William Rosener: Index to
William Rosener: Make sure you get the, the one up here and then this one is going to be index to and index to and I'll take a look at all this code here in a second. Make sure I haven't messed up here.
William Rosener: And I said, well, let me go ahead and grab this code just to get you ready for the last step there.
William Rosener: Okay. So I'm now going to concatenate just the against static text here square. It's always going to display the concatenate as exact letters there. I'm not going to go index one. Whoops. This needs to be one here.
William Rosener: Okay, I'm gonna start off with one index one is one wallets less than max value for example 10 man. I should have copied the one other than the even numbers here.
Okay.
William Rosener: Okay so index one is equal to one wallets less than 10 I'm going to be incremental by one. So this is my outer loop. And that's going to be looking at the rose
William Rosener: Now I've got my inner loop here which is going to be in effect, creating the column, but rather than display. And I'm not sure I saved this thing by copy and paste in that we want to put a star here.
William Rosener: Okay, so what's going to happen is this loop is going to go one is for example 10 display in the stars there 123 so this is going to, in effect, create all the columns.
William Rosener: Then I'm going to get a hard return which wraps it around comes back up prints haha that another home and effect of Row row row or called spin on how you will look at that.
William Rosener: Okay, so let's take a look here index one index one index one adding 12222 and we're just going to be concatenate a single Asterix to in effect, create a square here.
William Rosener: Let's go ahead and check this out there. So pretty good chances working here. We'll find out in the second term.
William Rosener: I'm going to go ahead and upload that
William Rosener: Come back. Click on assignment nine PHP and sure enough, it looked like it worked perfectly. I put in, for example, eight. Sure enough, I'm getting the square. So,
William Rosener: Let me do one last one here. If I want to put a big number in like 15 I'm going to get the numbers and increasing order decreasing
William Rosener: The even numbers and the big old square. But again, take a look up here, all the big differences. It's going to the server executed on the server, and then
William Rosener: The results are displayed, and as I probably mentioned the past. One of the huge differences anytime you want to deal with anything, secure, you don't want to do with a PHP because
William Rosener: The user cannot view your code. So if the user came over here and went to view page source, they only get to see the results they don't actually get to see your code against go while they're accessing this database, etc. Or table within the database etc them.
William Rosener: Whereas with JavaScript, you don't want to spend too much time working on the JavaScript. I came up here.
William Rosener: To the completed and if I put a value in here.
William Rosener: If I came over here and hit view page source, someone can just instantly take all the code. So I don't want to spend too much too many hours working on
William Rosener: JavaScript known very well that someone could literally, in effect, steal my code that I spent so many hours working on. So if you're kind of
William Rosener: vert. The nice thing about JavaScript is. It's so quick. What we're doing here, even though it's going this one so right change this value.
William Rosener: To non you're going to come up here and notice it does not call the server, and then have the new results in displayed it's instantaneous
William Rosener: Whereas the other one depend on your internet connection, etc. There it can that can drag on for a couple seconds or more as the new results are being downloaded them.
William Rosener: I think that's going to kind of conclude this video. So these are the complete completed ones here. So if I put in a value. For example, a 10
William Rosener: I'm going to I've given you increasing, decreasing the even numbers little square here. I'm gonna let you guys work on that multiplication table on both the JavaScript and the PHP their own kitchen. The next video. Thanks, man.