Join Kevin Skoglund for an in-depth discussion in this video From instances to HTML, part of PHP: Object-Oriented Programming.
- [Instructor] In this movie we're going to learn to create instances of our newly created bicycle class and to output their properties to the HTML page. So if as a starting point we go and we look at the current HTML page that we have. So that's chain_gang/public/bicycles.PHP. Right? You have to make sure you use public cause that's where this is located. Our web server of course would just go straight to this, right? So everything there that's highlighted in blue would just be ww.chaingang.com/bicycles, right? Something like that.
But we have to type all of that in. So bicycles.PHP is going to return this table to us. Right now that table just has placeholder data for brand, model, year, et cetera. What we want to do is create an object and use that object and ask for its properties for each one of these. So its year property would display in this box right here. So let's switch over to our code and let's think about how we would do that. So here we have our inventory. Down here there's our header row. That's TH for the header row. TD for the table data cells.
So right down here before we start this, let's create a new bicycle. Let's create our PHP tags and let's make bike=new bicycle. Alright so that would create a new bicycle for us. We know that we can also pass in arguments to our construct method. So let's do that as well. Let's do args. I'm just going to make a simple array here. I'm going to type in a whole bunch. Brand, we're going to make this one be Trek. And let's type in model and we'll make this one be Emonda.
And year let's make it 2017. So let's just stop there for now and let's just pass in those args to our bicycle. So now we have a new bike, right? It creates an instance and it populates those values for us in there and now we have this bike. So now what we want to do is take this instance of the bike and we want to output its values inside this HTML. So we can do that by using our PHP tags with echo. And then we can tell that instance to output its brand.
Close our PHP tags. Let's copy that. And let's put it right here. This one's going to be model. And this one's going to be year. Okay, so let's just save that and let's go take a look at it. See how we're doing. Let's go over to Firefox. We'll reload our page. Look at that, Trek, Emonda, 2017. It's that easy. Now there are a couple more things we should do here. We want to make sure we follow good security practice and escape each of these values for HTML. I have this function H which calls HTML special chars for it to make sure we escape those values and make them safe for HTML.
So we're going to do that on all of these. And we know that we want to keep doing this all the way down the list. We got category. Category. We've got gender. We've got color. We've got weight. And we've got condition. And we've got price. Now we need to make a few more changes here.
We know that weight is not going to be weight, it's going to be weight in kg, right? For kilograms. And then we can have a dot then let's put a little slash here and let's copy this. Put it right here, and that's going to be in pounds. And that's actually a method so we want to put parenthesis after it. And condition we know is also going to be a method, right? It's going to return the value from condition ID. We're going to get a string back for that.
And for price let's format it. Right now it's not going to look very nice, so let's format it with something a little better. We'll use money format, and we'll put parenthesis around this. And at the beginning the first argument to money format should be the format string that we want. You can look up what those ought to be, but dollar sign and then %i, is going to give you a pretty good one to start with. Okay, so let's try those out. Let's save it. Let's go back to our page. Firefox, let's reload our page. And look at that. Now we're missing some of these values though, so we need to add in a few more of those.
Let's add those in quickly. We've got gender. We'll make this one unisex. And color. Make it black. And weight in kilograms, we'll make this one 1.5. And price will be, we'll just make it $1000. Nice even number. Alright let's save that. Let's come back over here and let's reload our page.
Look at that. We've now populated all of these. Now category is not being set. I forgot to set category ID in here let's do that real quick. So let's add in category as well. Category, we'll just make it road. There we go. One more time. Reload it. Okay, so now we've got values in there. So you see how we're able to take this object with its different properties defined, and to output it to HTML.
In the next movie we'll work on pulling those from a CSV file. But before we do that, I want to do one other thing here. Which is we know that we want this to loop through different values, so just as a placeholder, I'm going to create a PHP loop. I'm just going to make a simple for loop for i=0, and let's say i, less than or equal to zero, dollar sign i++. Okay so this is just a real simple PHP loop.
And it's just going to loop ten times PHP, and end my PHP tag. There we go. Alright so now, I've got a PHP loop defined. Let's go back and look at that page one last time. Let's go back and reload it. And now you can see it just repeats it. So it's looping through over and over again. Now what we want to do is loop through the different rows in the file. So even though we're not reading from that CSV file yet, you see we have a proof of concept. We're able to take a new object and to assign it values for its properties.
And then to output the values of those properties in our HTML. In the next movie, let's see how we can actually get those values out of a CSV file.
- Defining classes
- Calling methods
- Class inheritance
- Extending and overriding classes
- Accessing and controlling access to properties and methods
- Static properties and methods
- Magic methods: constructor, destructor, and clone
- Creating a PHP OOP project