‫So now we're going to make this this search function work.
‫And basically what we're going to do is submit it to a specific route that will map to a controller
‫method.
‫And then we'll do our searching, do our database stuff.
‫And then it's going to load the listings view.
‫So this same view but it will be just the it will be the results of the search.
‫And we can even change the heading.
‫We'll have a conditional that will say if it's a search then show search results instead of all jobs.
‫All right.
‫So let's start off by going to the form which is in views.
‫Whereas it it's a partial.
‫Right.
‫So we're going to go in partials.
‫And then it's the showcase search.
‫So the form tag is right here and it doesn't.
‫All it has is our classes.
‫So we want to add to this.
‫Now since this is a search form and we're not doing anything with the data except looking something
‫up where it's not we're not inserting it or anything like that.
‫I'm going to make this a Get request, okay.
‫In fact, you could just leave off method and it's a Get request.
‫So that means that the parameters will be in the URL, which is fine for something like this.
‫And then the action is going to go to slash listings slash search.
‫Okay, so that should be all we need to do here.
‫Just make sure you have your name which is keywords and name location.
‫So we'll save that.
‫And now let's create the route.
‫So in the routes PHP we're going to put this.
‫Let's see.
‫We'll put it right under.
‫Actually.
‫Yeah.
‫We'll put it right under the the edit.
‫You know what?
‫Let me just make this a little bigger.
‫I'll put it right under here.
‫I want to put it above this because I don't want it to mistake in search listings.
‫Slash search for an ID.
‫So let's say router and it's going to be a Get request.
‫To slash listings, slash search.
‫And we're going to have that go to the listing controller.
‫And we're going to have a method called search.
‫All right.
‫So that's that's that.
‫Close that up.
‫Go to our controllers listing controller and then down at the bottom here.
‫And we're going to create a new method in the class.
‫Let's say search listings by.
‫Let's say keywords slash location.
‫Oops.
‫And then let's add return void.
‫Call it search.
‫And then right now all I want to do is show the get superglobal.
‫So we'll use inspect and die and pass in money sign underscore get.
‫And let's see what that gives us.
‫So if I come over here, just reload and let's pass in development search.
‫You can see I get an array with keywords, development and location.
‫Just an empty string because I didn't add anything in there.
‫And then up here we have in the URL we have a query string with keywords development and just location
‫nothing.
‫If I put something in location.
‫Obviously that's going to be put in that that array value.
‫All right.
‫So we know that the form is hooked up now.
‫Let's, let's put the location and the keywords in variables.
‫So we'll go ahead and say keywords.
‫And let's set that to I'm going to use a ternary here and say if is set.
‫Get keywords.
‫Then.
‫Let's put.
‫Get keywords in that variable.
‫So get keywords.
‫And I'm also going to just wrap it in trim just to trim any white space.
‫Else than just an empty string.
‫Okay.
‫And we'll do the same with location.
‫So let's take this.
‫So these three instances of key words and change those to location.
‫And then for now, I'm just going to create a very basic query with just a hardcoded word that we're
‫looking for.
‫And we're going to do that using the like operator.
‫So let's create the query in a variable.
‫I'm going to use double quotes here.
‫And we're going to say select all from listings where.
‫And we'll just check the title for now.
‫Later on we're going to search more than just the title, but I want to start simple here.
‫So we'll say where title.
‫And then like we don't want to use equals because that means it would have to match.
‫Exactly.
‫We want to just search for something inside the title.
‫So we're going to say like and since it's a hard coded string, we need to wrap it in single quotes.
‫And if I just do software, what's going to happen is it's basically just going to look for a title
‫with software in it.
‫I'm sorry, not with software in it.
‫That is just software.
‫Now we can use these percent signs.
‫So if I put a percent sign at the beginning what that represents is zero or more characters that occur
‫before that string.
‫So if we have, um, I don't know, build software, then it would match it right now at the same time,
‫if I have it on the end, then that represents zero or more characters that occur after the the specified
‫word.
‫So after software.
‫So this would match software developer.
‫Now we want to find it anywhere within in the title.
‫So what we're going to do is add a percent sign on both sides.
‫So it doesn't matter what is before or after it.
‫As long as software is in it, then it should match.
‫So that's how like works.
‫And then let's say listings set that to.
‫And then we want to make our query.
‫So this.
‫DB query.
‫And we're just going to pass in our query.
‫Okay.
‫And then for now we'll just do an inspect.
‫Inspect and die and will pass in listings to see what we get.
‫Actually, we have to put our fetch all here.
‫Since we're fetching data and it's more than more than one.
‫So fetch all.
‫Okay, so now it doesn't really matter what we put in here because we're searching for software, just
‫hard coded.
‫So I'll just click search and you'll see what it matches is the software engineer.
‫And if I had more listings that had the word software in it, then it would match those as well and
‫they would be included in this array.
‫So now let's step it up a little bit.
‫And instead of searching for hard coded just software, let's get rid of that, including the single
‫quotes.
‫And let's put in a placeholder for keywords.
‫And then just like every other query where we're putting in data, we want to add a params array.
‫And we're going to set that keywords placeholder to not just keywords, but we want to have our percent
‫signs around it.
‫So we'll go like that.
‫And then inside curly braces we'll say keywords.
‫Okay.
‫And then of course we have to pass in params into our query method.
‫And let's try that out.
‫So we'll save it and let's go back to the home page.
‫And then I'm going to search for let's see what do we have here.
‫So we have web developer and front end web developer and back end web developer.
‫So let's search for developer.
‫And it's case insensitive.
‫So even if I have a lowercase d you can see it still matches web developer front end back end web developer.
‫Okay, so that's working.
‫Now I'd like to search more than just the title.
‫So we're going to extend this query a bit.
‫So for instance I want to also search the description.
‫So after this keywords we can use or and then.
‫Description.
‫And then like.
‫Keywords.
‫Make sure you add the colon in front of it, or tags like.
‫Keywords.
‫Or let's do company.
‫So if they want to search for the company name like.
‫Keywords, and you can put more stuff if you want.
‫If you want to search like the requirements and all that.
‫I mean, you can put as many fields as you want, but let's let's try that.
‫So we'll go back and let's look at the.
‫Let's see.
‫Let's look at the descriptions.
‫So this one has react in it in the description.
‫So I'm going to search for react.
‫And I won't even finish the word.
‫I'll just say rake and search.
‫And that comes up.
‫So that's working pretty well.
‫Now I also want to add the location and I want that to match the city or state.
‫So first off.
‫Let's add in here in our params.
‫We also want to add location.
‫And we're going to do the same thing.
‫We're going to have our percent signs.
‫Oops.
‫And inside that, then we'll have the location variable.
‫Now we have to change the query a little bit.
‫So basically everything that I'm doing with with the keywords I want to wrap that in parentheses.
‫So after this where right before title I'm going to add an opening parentheses.
‫And then at the end here we're going to add closing parentheses.
‫And then we're going to put the and operator here.
‫And then in parentheses we'll say city like and then colon location.
‫Or.
‫The state.
‫Like colon.
‫Location.
‫So it's going to match the the keywords to these and the location to these to the city or state.
‫All right.
‫So let's try that out I'll save that.
‫And let's see we'll keep the area actually let's reload this and we'll say react.
‫And this this one is in Boston.
‫So I'm going to just say Boston.
‫I'll just do boss and search.
‫And that matches.
‫Now if I do react and then let's say.
‫New York and search, I get nothing.
‫So it's only going to match if it's in that in that city or state.
‫So if I put nothing in keywords and I just search for either the city name or the state, like let's
‫say NY.
‫That gives me everything that's in the New York state.
‫So if we look at state, NY, state, NY.
‫So the search form is working pretty good.
‫Now we just need to show the view and show the results.
‫Obviously we don't want to just show an array.
‫So let's come down here and let's get rid of the inspect and die.
‫And let's say load view.
‫And the view we're going to load is going to be slash listings slash index.
‫You could create a separate view if you want, but there's really no reason to.
‫We can reuse our code.
‫And then we want to pass in here.
‫The listings.
‫Because remember, it's already looking for a listings variable.
‫If you just go to the index page.
‫Then it's going to be all everything from the database.
‫But now we're changing it to be this which is the filtered or the search listings.
‫And then I'm also going to pass in along with that keywords.
‫So set that to keywords.
‫That way we can use that in the template and also location.
‫And we'll set that to location.
‫All right.
‫So let's save that and then.
‫What I'm going to do is, well, we can try it out first.
‫So let's say.
‫Developer.
‫Search.
‫There we go.
‫Now, I don't want it to say all jobs because that doesn't make any sense.
‫It's not all jobs.
‫So let's go into the view.
‫So listings index and.
‫Uh.
‫Let's see.
‫So where it says all jobs, let's get rid of that.
‫And let's check for keywords.
‫So we'll say.
‫Let's open up our PHP and I'll say if.
‫Actually, how do I want to do this.
‫Let's do a ternary.
‫So we'll say is set.
‫And we want to look for keywords.
‫So if is set keywords then let's say.
‫You know what?
‫I'm going to use the long form if statement because I want to type out like search results for and I
‫think it would just be neater.
‫So let's do that.
‫Oops.
‫So if I put this on a separate line.
‫Okay.
‫So we'll say FPF and then let's do PHP ls.
‫And then.
‫And if.
‫All right.
‫So I'm going to search for let's say if is set.
‫Keywords.
‫Then I'm going to want to show search results.
‫Let's say search results for and then we'll show.
‫The keywords.
‫So we can just use our shorthand syntax and we can say keywords.
‫Else, then we'll stick with our all jobs.
‫Okay.
‫So let's try that out.
‫We're going to refresh the page.
‫We get search results for developer.
‫Now this works.
‫It looks good.
‫But what's the mistake that I made here.
‫I took the user input.
‫Right.
‫Which is.
‫Key words coming from get.
‫And then I passed it into the view and I and I displaying it.
‫So what if someone went like this?
‫Say alert.
‫Close the script tag.
‫So what if what if someone does that?
‫So we need to sanitize that before putting it out.
‫So let's say HTML special chars.
‫And let's wrap that.
‫Let's resubmit.
‫And we just get search results for and this so it doesn't actually run the JavaScript.
‫And I mean, when you're dealing with vanilla PHP, there's a lot of little things like that.
‫And I think that that's the worst part of working with vanilla PHP.
‫I mean, there could be something like that in this project that I missed because there's just there's
‫so many ways to screw up.
‫And I think that's why sometimes PHP can get a bad rap.
‫Now, if you're using something like Laravel, all those protections are built in.
‫So you don't really have to worry much about SQL injection or anything like that.
‫But yeah, when you're doing it from scratch, there's there's so many things that you can easily miss.
‫And if I did miss something, I'm sure I'll hear about it.
‫And this is probably the section where I'll add anything, any, any fixes or if I add any new features
‫or anything like that.
‫But I think for now we're pretty good.
‫You know, we've built a project from the ground up, not using any frameworks, not using any libraries,
‫just straight vanilla PHP with our own personal little infrastructure here.