‫So in the last video, we were able to fetch listings and just inspect them and print them out on the
‫screen.
‫But we want to be able to pass them into this view so that we can load them here instead of these hard
‫coded HTML ones.
‫Now, doing this is actually pretty easy.
‫So this listings variable, we want that to be available in our view which is in views home view dot
‫PHP.
‫So what I want to do is this load view function.
‫I want this to be able to take in an array.
‫Right.
‫So as a second argument, we want it to be able to take in an array.
‫And we want to be able to.
‫Put for instance listing.
‫So it's an associative array.
‫So we'll have listings.
‫And for the value we'll have the listings variable which will be the data.
‫Okay.
‫So let's save that.
‫Now obviously that's not going to do anything just yet.
‫We have to modify this load view function to accept this array and to make it available to us.
‫So to do that let's go into helpers dot PHP because that's where our load view is.
‫And let's add an addition to name.
‫Let's add data.
‫And we'll set this to a default of just an empty array.
‫So if nothing's passed in, the array will just be empty.
‫Then what we can do is down here, we check to see if the file exists.
‫And this is where we're we're requiring that file or that path.
‫And right above it I'm going to use the extract function.
‫And I'm going to pass in data.
‫And doing that is actually going to give us access to the listing variable.
‫Because what this does extract, you can see it imports variables into the current symbol table from
‫the array.
‫So if that's confusing what that means is if we have an array where there's a key of listings and a
‫value of of whatever this is coming from, the data coming from the database extract will make it.
‫So we now have a variable called whatever this is.
‫So we'll have a variable called listings which will equal the data.
‫It'll equal an array of data.
‫So now if I go to the home view and let me just come up here in the PHP and let's do an inspect.
‫Of listings just to see if it's available to us.
‫And let's reload.
‫And there it is.
‫And you can see it's within the view.
‫So we have we now have access to this listings variable.
‫It's as easy as that.
‫And I mean, that's something that I would have thought would be more difficult to do this, to pass
‫something into the view.
‫But simply using extract gives us access to that data, and we can pass as many things as we want in
‫here.
‫If I want to pass in, let's say name, right.
‫And I set that to Tom, and then I go into my home view and I say inspect name and reload.
‫We get Tom so easy as that.
‫All right, so now that we have access to the listings, we want to loop through them.
‫Let's get rid of that.
‫We want to be able to loop through them and output them.
‫So we're going to clean up a bunch of this HTML because right now we have like 5 or 6 hard coded jobs.
‫So we just need one of these.
‫So let's see job listing one.
‫So this div which ends right here, that's the only one we actually need.
‫So let's get rid of the rest of these ones.
‫So that's five.
‫And I believe it ends.
‫Let's see, where does it end?
‫We have details a and then two divs.
‫Um.
‫So details A and then to div.
‫So we'll get rid of that.
‫Right.
‫So we have one job listing.
‫If I save this and we reload we should only see one.
‫And what we want to do is surround this with a for each.
‫So let's do that.
‫Let's.
‫Go right here, right above the comment.
‫We'll say PHP for each, and we're going to say for each listings as a listing.
‫And then let's just close that up down here after this ends.
‫So right here PHP and.
‫For each.
‫All right.
‫Now, if I save this and I reload, I should see whatever five, however many in the database I have.
‫But it's all hard coded data, right?
‫It's just in the HTML.
‫So we want to just replace that.
‫So let's go to the title.
‫And let's get rid of that.
‫And.
‫We'll use the shorthand syntax and we should be able to just say listing.
‫And remember we're fetching as an associative array.
‫So we use this syntax.
‫The brackets.
‫If I save that reload now you can see all the titles are different.
‫Do the same thing with the description I'll just copy this.
‫And paste that in there, let's say description.
‫Then we have the salary.
‫So we'll.
‫I'll leave the money, sign there, and then just paste that in and we'll say salary.
‫And then for a location.
‫I'm actually going to comment out this local span.
‫I'm not sure if we're going to do that, but for the location, let's paste in the city.
‫We could do city comma and then the state.
‫And let's see what else we got.
‫Tags.
‫So right here for tags.
‫We'll say listing.
‫Tags.
‫And then let's see for the the link here that goes to the details page.
‫Get rid of this details HTML.
‫And this is going to go to slash listing slash and then the ID so we can put in here ID.
‫And then I think that's it for now.
‫So let's save that reload.
‫And now you'll see we have all different data and it's data coming from our database.
‫So hopefully you can see the whole process here.
‫We start at public and I know I kind of reiterate things a lot and repeat myself, but I just want to
‫kind of drill it to and drill it into people's heads that, you know, have a hard time understanding
‫some of this stuff.
‫So we come to the website, right.
‫It sees the Uri and the method.
‫It passes it into the root.
‫We have a routes file that maps to where is it maps to a controller.
‫So if we're at the home page it's going to load the home controller.
‫Right.
‫And it's going to be it's a Get request.
‫So home controller then gets the database instantiates it.
‫We create a query here to get the listings.
‫We load the home view which we're seeing here.
‫We pass those listings from the database in and in the view.
‫We now have access to listings.
‫So we loop through them and we display each one using an associative array.
‫So that's that's the essence of of what we've been what we have here.
‫Now you can keep it as an associative array if you want, but I prefer to to fetch as objects where
‫we use the arrow syntax rather than the brackets.
‫So it's really up to you on how you want to do this.
‫I want I just want to nip this in the bud.
‫Now before we add other views where we're where we're where we put data that's getting fetched.
‫So what I'm going to do is go into the database class and let's see instead of.
‫Instead of PDO fetch assoc, I'm going to say fetch.
‫OBJ.
‫Right now if I reload we're going to get an error.
‫Cannot use object of type std class as array.
‫So we just have to change up the syntax a little bit.
‫So instead of using brackets like where we have listing title, we can get rid of the brackets.
‫And we can just put an arrow like that.
‫So I prefer this syntax.
‫You know we don't have to have quotes and brackets.
‫We just simply use an arrow.
‫So we'll do the same here with salary and all the title where we did the title we need to do the description.
‫So description.
‫What else?
‫We have the city.
‫Get rid of that arrow.
‫That.
‫And tags.
‫And I'd.
‫Okay, so let's try that.
‫We'll reload and there we go.
‫It's back.
‫And we're now using this syntax.
‫But like I said it's it's up to you.
‫It's all preference.
‫Now one last thing I'd like to do before we end the video is the salary.
‫Remember how we formatted the salary and the little kind of demo application we were doing.
‫So we would use commas.
‫So let's do that and we'll include the money sign in that instead of putting it just directly in.
‫So where we have salary let's get rid of this outside money sign.
‫So right now it'll just look like this.
‫And then we'll create our function our format function I'm sorry format salary function in the helpers
‫because that's really what it is.
‫It's the helper.
‫So we'll come down here.
‫Let's give it a comment and we'll say format.
‫Salary.
‫And let's see, it's going to take in a pram of a string and that's going to be the salary.
‫And then it's going to return.
‫It's going to return a string of, say, formatted.
‫Salary.
‫Or it's not going to be a variable.
‫Let's just we'll just say formatted.
‫Salary.
‫Okay.
‫So let's say function.
‫The format salary.
‫Pass in a variable of salary.
‫And then we're going to return.
‫Let's do a money sign and then we'll concatenate.
‫And remember we have the number underscore format function which will take care of the the commas and
‫stuff.
‫And then before we put the salary in here I'm just going to wrap it in float Val.
‫And then we'll pass in salary.
‫So same thing we did in the other little project.
‫And then we can use that in our home view.
‫So right here listing salary, I'm going to just surround that in format.
‫Salary.
‫And let's save, let's reload.
‫And now the salary has been formatted.
‫Now we also want dynamic data on the listings page.
‫So if we say show all jobs it takes us to slash listings.
‫This is still hardcoded.
‫So we basically want to do the same thing we did in the home controller in listings and then index.
‫So if we open that up and then just copy this, in fact we can just copy this whole thing here, paste
‫that in oops and change right here.
‫The home will change that to listings slash index.
‫So now we'll have access to the listings in the views Listings index dot view.
‫So let's change a couple things here.
‫Actually we can probably just copy the for each from the home view.
‫So this right here for each.
‫I'll just grab that and in the index view.
‫Let's see.
‫So we have job listing one.
‫So basically we just want to get rid of all the job listings.
‫So this div that surrounds them the grid basically let's see where that ends.
‫And we can just get rid of everything in that.
‫So it ends right here.
‫So.
‫That means that everything in here.
‫So to the grid.
‫Yeah.
‫So we'll cut that.
‫And let me just make sure that this.
‫So this div and no that's not right.
‫Oh yeah.
‫So this div ends here.
‫So that's one div.
‫So I believe we need one extra in here.
‫Right.
‫I think that's.
‫Yeah.
‫And then what we'll do is paste the for each in and let's try that out.
‫I'll save that and reload.
‫And now they've changed to data from the database.
‫All right, so I think.
‫Think we're good.
‫For now, as far as the home page and the the index listings index.
‫Next we want to do the details or the show page where we're showing the individual listings.
‫So we'll get to fetching that data next.