Projects In PHP | Creating A Job Lister Website From Scratch | Eduonix

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey guys we're gonna start on a pretty decent-sized project it's gonna be a job listing application where we can go and list jobs with certain fields and you can see this is the homepage here we're using a customized bootstrap theme and we have the latest jobs here and as its they have a title a job title a description and the category and if I click on the View button it takes us to a separate page an individual job post page that has the the user that that submitted the date description the company name the salary and the contact email alright and then you see we have a go back link we also have an edit and a delete button alright so we can also filter by category ok so there's nothing in business actually both jobs are in technology but I can go ahead and edit and change that let's say business ok so you can see your job has been updated now if we go and choose business you can see we're getting this one here alright we can also create listings so let's say test listing will choose retail job title say salesman description this is a test location say Boston salary will say 60 K and contact email submit and there it is ok and you'll have the latest jobs at the top all right so that's what we'll be building in this project and we're actually going to be using PDO this time to interact with our MySQL database we're going to be using classes we're going to create a job class a template class we're going to be implementing templates so it's a pretty decent-sized project and this the structure is railing is really nice and something that you could use in future projects all right so let's go ahead and get started all right so we're gonna get started on the job list our application now I'm gonna really quickly create the database all right and I'm gonna include an SQL file in the resource files that you can import if you want so if you go to if you create a database and then go to import you can just upload that file or you can just create it manually it's not it's not that big there's only two tables all right so I'm gonna create a database called job lister all right and then I'm just gonna paste in some SQL to add the fields okay so you can see I'm creating a table called categories with an ID and a name ID is gonna be an int with 11 characters name will be a varchar' 255 and then jobs which will have an ID a category ID company job title description salary location contact user and contact email and all except the ID and category ID will be a varchar' with 255 all right and then we're just setting the ID to the primary key in both the categories and jobs table we're also setting them until Auto increment all right so let's go ahead and run that and then if you look at our structure we have two tables categories which just has an ID and a name and then jobs which has these fields here ok so you can go ahead and create these if you want or just upload that file all right now let's add some data here just a couple fields so let's see categories we'll say insert ok so let's say business and all we need to put is the name we don't need the ID it's auto increment we'll say business technology say retail and construction okay so we'll click go and now our categories we have four categories all right so now let's go to jobs and go to insert and for category ID we'll say one which is business I believe company let's say say JP mortgage I'm just making stuff up job titles a senior investor description I'm gonna grab some text from lips I'm calm I'll just grab a couple sentences ok we'll paste that in and then salary will say 90 K location say Boston contact user click my own name and then contact email will just say brad at gmail.com alright let's add another one here this will be category 2 which is technology will say tech guy job title let's entry level programmer description we'll grab some text salary let's say 50k location I'll say Springfield contact user John Doe and contact email ok and then we'll click go alright so two rows inserted so you can see if we go to jobs now we have these two listings alright so the database is all set for now let's go and I'm gonna open up my htdocs folder and we're gonna create a folder here and we're gonna call it job lister all right and we're gonna create quite a few files and folders in here I'm like I'm gonna open this in sublime text as well okay so let's first of all create an index.php file alright and let's just make sure that this works so we'll go to HTTP localhost I'm sorry localhost / the job Lister and we get test good now what I'd like to do is set up our template class alright because I don't want to include all of the HTML inside of these files that we create in the route I want them to basically point to a template and then I want to be able to pass values to the template all right so we're gonna create in here a folder called Lib and that's where all the classes will go okay all of our libraries so let's create one click file called template dot PHP and class names I want to have to begin with an uppercase letter all right so we have a template inside here let's say PHP class template okay we're gonna include a couple properties here this is gonna be the path to the template so it's gonna be protected which means that we could extend this class and still use this property I'm going to call it template and then we're going to have a variable for the variable the variables that are passed in okay we'll call that VARs and that's actually going to be an array all right let's create a constructor so we'll say public function double underscore construct okay and that's going to take in that path variable or property template and then we're just going to set it to two the actual property so we're gonna say this template equals the template variable that's passed in okay we also want to set a get function or a get method to get the template variables so public function double underscore get okay and this is going to take in a key and then what we want to do is return oops this VARs and then in here will be the key all right and then we also want to be able to set them so say public function double underscore set and that's going to take in the key and value all right so we'll say this VARs and the key will equal the value okay so very short methods and then finally we want to string because we want to be able to basically use this as a string so public function double underscore to string now we want to extract the variables we want to be able to just use a single variable instead of having to use the key value inside the template so we're going to wrap it in a function extract and pass in this VARs all right so basically what this does let me just show you real quick so if we have like template name okay we set that and then we want to use it inside to actual template we just want to be able to use name okay instead of using this template name that's what this is that's what the extract is gonna do all right so extract this VARs and we wanted to find the path name we're going to use this CH dir function and we want to pass into here to our name and then the template path which we can get from this template all right and then we want to be able to output the template so what we're gonna do is we're going to start a buffer using the OB start function alright and then we want to include the template path so we're going to save include base name and then pass in this template and then finally after the buffer we need to run OB get clean okay that's what we're going to return so OB get clean alright and that's it so this is a template class that you could use in a lot of different projects and it might look confusing now but once I show you how it actually works when we instantiate it it'll be a little bit more clear alright so let's save that and we shouldn't have to go back into this file okay that's our template library so now we want to do is we're gonna have to include that file that class along with some other files in all of our route files now instead of doing the include you know five different files at the top what we're gonna do is create a config file where we can just put all that stuff all right we're gonna set up an auto loader too so we don't have to manually include every class okay so let's go and create a folder here called config and then we're gonna have a file we'll save it as an it dot PHP alright and in here is where we would create all of our requires and includes so I want to auto load our classes alright you know instead of doing require require once and then going into Lib slash template dot PHP alright because we're gonna have a database Lib we're gonna have a job class a job library and I don't want to have to do that for all of our classes alright and you might add more later on so it's easier to use an auto loader and what that does is it'll include the file whenever you instantiate the class so if we instantiate the template class or the template object then it's going to require it alright so let me show you how to set that up so we'll say function and it's a special function name it's double underscore auto load ok and that's gonna take in a class name and then we just want to say require once actually don't need the parentheses require once and it's gonna be in the Lib folder Lib slash and then we're gonna just concatenate here whatever that class name is alright and then concatenate dot PHP so whenever we instantiate a class it'll look for that class name as a file so make sure that your file name matches your class name alright now we're also gonna have another config file which I'm going to call config dot PHP and that's going to hold our database parameters and all that so we need to make sure we include that as well so for that we'll do require once and config dot PHP alright so let's go ahead and save that and then in our index.php file we should only have to include that in it alright and that's it so up at the top I'm gonna use include once config slash and knit dot PHP all right so let's save that and unexpected oh I spelt that wrong ok reload and just to make sure that this is being included let's just say echo test ok good and let's make sure well we can't see if we this isn't being included yet because we didn't instantiate a template object yet ok once we do that the file will be included so let's go down here and let's see actually we'll just say template equals new template now remember that's gonna take in a path okay so the path to the template we want it'll be in a templates folder and we'll call it front page dot PHP alright so let's go ahead and create a templates folder whoops what happened where is it there it is alright so inside templates we'll create a file and we'll save it as front-page dot PHP and let's just write in here front page and then we'll go back to our index and let's see we want to echo template all right so save that reload and there we go so now we're loading the front page template now we can also add values dynamic values let's say we want to assign template I'll say template and then a variable name will say title and let's set it to latest jobs so now we should be able to go into our front page template and say PHP echo title save that and there we go latest jobs all right so we're able to do to use just title here because of our template class where we did our extraction of our variables alright so this is a really handy class to use in your PHP projects now the last thing I want to do is just set up our interface a little bit we're gonna be using bootstrap I'm gonna use a boot swatch custom file here okay we're gonna grab which one did I use this right here flatly okay so I'm gonna grab this link right here and what we're gonna do is since we're gonna be using code on the same pace for instance the the navbar or the the head tags things like that we want that on every single page in every template so we're gonna create an includes file so in the templates folder we'll create another folder called Inc four includes and I don't know why this isn't refreshing and then inside there let's create a file called header dot PHP and we're also gonna create a file called footer dot PHP okay so that's in the Inc folder and inside header let's put in some HTML tags we'll put our title and then we want to link that bootstrap file so link rel stylesheet' href is gonna go to that link I just copied and let's also link a custom CSS file so that'll go to CSS slash Styles dot CSS and then for now I'm just gonna put down here after the body tag header okay now I don't want the ending body tag and the ending HTML because that's gonna go in the footer so let's save this and then we'll go into the footer and we're gonna put our ending body and the ending HTML okay and then up here we'll put in footer all right now in the front page we just want to include both of those files so PHP include and that's gonna be in the ink folder slash header dot PHP okay we want to do the same thing for the footer all right so let's go ahead and save that and if we go back and reload now you can see our head our title says job lister and if we look at the source code we have the code from both the header and the footer all right so that's good now as far as the HTML let's go to get bootstrap and we'll go to get started and then examples and we're gonna grab this Jumbotron niro Jumbotron template so we'll do a control U and we're gonna grab the div with the class of container down to where the container ends okay so there to there all right and then let's go ahead and paste that in here and we need to figure out what we need to put in the header which will be let's see from here down to here okay so I want the navbar in the header so we'll put that right here all right and then we'll save that and let's go back to front page and then let's figure out what we want in the footer so for the footer I want the the footer tags and also this container div so we're going to cut that out and we'll save that and put that inside the footer okay and we'll just change this company here to jobless sir save that now if we look at our application you can see that it's it's missing something it's not narrow like the the template is that's because there's a CSS file we need to copy so right here this Jumbotron narrow CSS I'm gonna grab everything in there copy it and then put that in our custom CSS file which we need to create so let's create a folder named CSS and inside there will have a file called styles dot CSS and we'll paste that in and save it alright so now you can see it looks just like the demo alright it does have different coloring because we're using a boot swatch custom CSS stylesheet all right now let's see you see how we have these two rows down here I only want one so let's go to our front page template and see how we have two of these div class call lg6 I'm gonna get rid of the second one okay and then we're going to change this to right here this will be that'll be ten and I'm gonna change it to MD 10 because I want it to be applied when we're at medium which is this this range here alright and then let's get rid of these to that and we're gonna put another div here with the class of call MD 2 and this will be the View button so give it a class of BTN BTN default and now we'll just say href and it's not gonna go anywhere right now okay save that and there we go okay we'll have a few of these now obviously these are gonna come from the database so just static just for now actually no what we want this row marketing want that to be around each set so let's see that ends here I'm actually gonna delete that and put it around each set of columns so right there okay we're also gonna put it around this one and this one I don't know why it does that why it actually deletes okay so sorry if that's confusing we just have this row marketing around each set of the ten column div and the two column div alright and now if we reload they'll be they're more separated okay let's see for for the the navbar let's go in a header.php for the navbar let's change project name to job lister and we're only gonna have two links so we can get rid of that one I'm also gonna get rid of the active class and then home is going to go to index dot PHP and then the second one will be create listing and that's gonna go to create dot PHP which we don't have yet alright so okay alright guys in the last video we set up the template class so we can now load templates now we want to connect to our database and we want to be able to grab listings and display them on the home page so we're gonna have a database class that's completely dedicated to connecting and fetching data we'll also have a job job class that will be responsible for working with jobs in categories all right so we're gonna start off with our config so in the config folder we have config.php and we're gonna just throw in some PHP tags and let's add our database parameters alright and these are gonna be constants because they're not going to change so I'm gonna say define DB underscore host and that's gonna be localhost okay we're gonna have our DB underscore user which for me is route they also need the password okay make sure you put your password and then the DB name which is job lister alright so I also want to define the site title in here so that we can change it if we need to so I'll say site underscore title and we'll call that job lister alright and then we can actually add that to our header right here let's say PHP echo site title okay so it's still job Lister so now that we have that config file set up let's go ahead and create our database class or database library so I'm gonna create a file in the Lib folder and we're going to save it as database dot PHP alright and then what we want to do here is create a class called database and we're gonna set a bunch of private properties here so we want the host and that's going to be that constant we just set DB host we also want the user which is DB user pass and DB name alright and then we want a couple more properties down here one we need our database handler so we're gonna call that DB h we want an our property and also a statement so st MT alright those are our properties now let's create a constructor okay we're going to set the DSN so this is basically a connection string where we include the host the database name and so on okay so this has to do with PDO we're using PDO which is PHP data objects it's a alternative way to connect to your database so let's call this DSN and we're going to set it to MySQL okay you can actually use other databases with PDO postgrads is one of them I think there's about 12 okay but obviously we're using MySQL so I'm going to say MySQL host equals and then we're going to concatenate in here this host alright and then put a semicolon and then DB name equals and then we need to concatenate this DB name actually yeah okay and then we want to set our options so let's create a variable called options set that to an array okay and inside here we're gonna say PDO double colon adder underscore persistent and we're going to set that to true we want a persistent connection and if you go if you look at the PDO documentation and it'll give you all the different options there's there's quite a few alright and then we just want to set our error mode so our mode set that to PDO double colon arrow mode underscore exception okay so that's the options and then down here we need to create PDO instance and we're gonna throw this in a try-catch block so say try-catch okay so it's basically this is a way of catching your errors if you put something in here it's gonna run and then if there's an error we'll catch that exception all right so we're gonna say this and then we want to use our database handler so DBH equals and now we're going to instantiate our PDO object they want to pass in that DSN variable and then this is where we want to put our username and password as well so this user and then we want the password so this pass and then the options okay and then down in the catch right here this actually should be parentheses and we're going to throw in PDO exception and E so if there's an error we're gonna say this error we're going to set it to that object and then get the message okay whatever that error message is we're gonna put it inside of this property alright so that's basically how we can connect to PDO or through PDO and through a class so after the constructor we're going to create a method called query all right and that's gonna take in a query and then we're gonna set it we're going to use our statement we're going to say this St MT and that's going to equal this DB H which is our database Handler and we want to use prepare ok we're using prepared statements here and then just pass in our query all right and that's all we need for the query method so when we have values that we're working with we need to bind those values so we're going to create a function called bind ok and this is gonna take in RAM value and type which is optional and we're gonna set it to a default of null all right so first we're gonna check to see if type is null so I'm gonna say if is null and then pass in type okay which it is by default then we're gonna use a switch statement okay which is just a conditional I'm gonna say switch true and then basically this is going to be setup for the different types of values and integer a boolean and a null and a string which will be the default so I'm actually going to paste the cases in here all right so we're saying if it's an INT okay if if it's an int then we're going to set the type to PDO per am int this is I don't like this some indenting okay so if it's a boolean then we're gonna set it to per Ambu if it's null will set it to pram null and if it's none of those then we're gonna set it to string per am straight and again this just like the template class this is something that you can use over and over with different projects then we just want to go after that if statement and we're gonna say this s TMT bind value and then just pass in RAM value and type all right so that's the bind function the next one we want is execute so public function execute okay and what all this is gonna do is return ya return this statement execute okay next we have our result set so when we fetch the data from the database we're returning it as an as a result set so it's a public function result set okay and then we're going to run at the execute that we just created we can say this executes all right and then we want to return as a statement so this statement fetch all alright and then you can tell it what what you want to fetch it out what you want to return it as alright so an associative array or what so we're going to return it as an object so in here we'll say PDO double underscore fetch underscore aaj okay now this may be a little confusing to you and this is actually one of the easier PDO classes you can see the functions are really small there's some really complicated ones so I'm trying to keep it as short as possible and still give us the functionality we need so hopefully it's not too bad so the last one I'm gonna put in here is the single function and that's gonna be responsible for fetching a single value all right because we're fetching for instance all of the jobs that's going to be returned as a result set if we're getting one job then we want to return that as a single value so we'll say public function single okay I'm gonna just I'm gonna grab this and let's see this right here the only thing different is going to be instead of fetch all we just want fetch all right and that should do it for our class for our database class let's save this alright now this is not dependent on the jobs or this application you can use this anywhere okay so we're gonna extend our job class we're gonna extend it to the database class so we can use this stuff inside of that class all right so let's go ahead and create that so inside Lib inside Lib we're gonna create another folder I'm sorry another file and we're gonna save it as job dot PHP I don't know why this isn't refreshing alright so inside job let's create a class called job and we're gonna create a property here called DB and then we'll add our constructor okay and then we're going to instantiate our DB object with the database class so we're gonna say this DB and we'll set that to new database okay so when stanching in the database class we're putting it inside of this object right here alright so then we'll create a function to fetch all jobs all right so let's put a comment here so we want to say public function get all jobs and then we can use that database class we can save this DB query all right since we since we're using this database class we can use this query right here this query function so that's what we're doing here all right so let's pass in the query you'll say select all from let's see I want to I want to get all the the jobs fields but I also want to join in the categories table so that we can get the category name all right so we're gonna say select let's say jobs dot also everything from the jobs table and then we're gonna put a comma there and we're gonna say category or categories dot name and let's select that as cname okay so that's an alias we're just going to be able to use this for our category name all right now I'm not going to go over the queries too much because we do have them in SQL section in this course alright but what we want to do is we want to inner join the categories table all right so let's let's go right here we'll say inner join categories and we want to join it on job stock category ID and we want to set that to categories dot ID all right so this is the primary key in the categories table this is the foreign key in the jobs table all right and this actually should be up here so from jobs okay so from jobs note that should be before the inner join from jobs and then inner join categories on this alright and then the last thing I'll put is an order by because I want to order by date so order by oh wait a minute I even put a date I don't think I did let's see jobs no we don't have a date field so let's go ahead and add that so if we go to structure and I'll say add one column I'll call it post date and let's set it to a timestamp and then the default will set to current timestamp all right and I'll edit the SQL file that's included so it's the order by post date and let's make that descending all right so that's our query now after that we want to assign the result set so I'll say results equals this DB result set and then we just want to return the results okay you'll save that and now we should be able to use this function so let's go to our front page PHP I'm sorry not front page index.php and we want to create the job object up here so it's a job equals new job and let's see let's go down here and we're going to assign a template variable of jobs and we're gonna set that to the job object and then we want to call the get all jobs that we just created okay and then we should have access in our template to those jobs okay let's make sure there's no errors that's not it okay so let's go to our template now which is front page PHP and go down to this area now I just want one of these rows so we can get rid of this one and this one all right and then what we want to do is wrap this row div in a for each all right so let's do PHP for each and then we'll end it down here and let's say for each jobs as job and then here let's get rid of that and we'll say PHP echo job job underscore title okay and then this will be the description ok let's save that reload and there we go so now we can see the jobs that are in the database all right so we're now seeing the jobs from the database we're seeing them on the home page so what I want to do now is I want to be able to filter by category so we're gonna have a form in this jumbotron area to select a category all right so before we start to edit the HTML here let's go to index.php and we need a way to get a list of the categories so that we can fill the select box because I don't want to I don't want to have a static select box I want them to come from the database so let's set a template variable here we'll say template categories and we're gonna set it to job get categories which we don't have yet we have to create alright so let's save that and then we'll go to our job class in the Lib folder and let's create that function okay so I'm actually gonna just copy this and then this is gonna be a really easy query we're just gonna say select all from categories okay then we're gonna get the result set and we're going to return that so let's save it and now we should have access to it from within our template so let's go to templates and then front-page PHP up to where we have the jumbotron and let's change the heading here we're going to change that to find a job all right and then under that we're gonna have a form so we can get rid of this paragraph and the button and I'm going to put in a form and let's put a select and we'll give this a name I'll give it a name of category and I'm also gonna give it a class of form control all right now in here let's put an option and just gonna set the value of this to zero and we're gonna say choose category okay now the rest of them have to go within a for each loop because we want to loop through the categories and then output them so let's do PHP for each and in here we should be able to say categories as category and then let's grab this option now the value is gonna be the ID so PHP echo category ID and then the text will be the name category name alright so let's go ahead and save that and reload and now you can see that we're getting our categories from the database so that's all we want as for where this form is gonna go it's gonna go to the sent to the index page but I want it to be a get okay since we're not actually submitting data to the server we're gonna make it have a method of get okay and then the action will be index dot PHP okay so it's going to submit to index PHP and what we want to do is we want to test to see if that value is in the URL because if I go when I select business and we need a button we don't have a form submit button so let's go ahead and add that that's gonna go under the select we'll say input type submit and let's give it a class and let's see we'll give it a class of BTN BTN LG and BTN success okay and then the value will say find okay let's put a line break under the Select so business find and now up here in the URL you'll see we have category equals one okay businesses category one so we want to check for that so let's go to index.php and let's go up here and say category equals and we'll say is set so this is gonna be a shorthand conditional so if set get category then get category Al Snow so what this is saying is that if there is a get cat if there's a category in the URL we're going to set this variable to it if not we're going to set it to no all right and then down here we can test for it so let's see I'll say if if category then what do we want to do all right now instead of just get all jobs we want to call something else okay which will get just jobs from that category so what I'm gonna do is just grab the these two lines and cut them and put them in the else okay so if there's no category nothing's change it's just going to load what it's been loading but if there is then we're gonna call template you'll say template job's is gonna equal job and we'll have a function called get a get by category okay and then we'll just pass in here category alright now I also want the title to say for instance latest jobs from business or latest jobs in technology so we're gonna set the title as well I'll say template actually we'll do that after let's just do this for now so we need to create get by category so let's go to our job class okay and we'll say public function and get by category okay that's gonna take in a category and this is gonna be very similar to get all jobs so I'm gonna copy this and then paste that in here okay and then all I want to do is I want to create a where clause right here so we'll say where jobs dot category ID is equal to the category that's passed in all right and then everything else would be the same so let's save that and let's see let's go ahead and try it so if I choose technology and find you see it returns entry level programming if I go to business find senior investor retail there's nothing for retail okay so that's working good now for the title let's go back here yeah and we're gonna say template title and let's set that will say jobs in and then what we'll do is let's concatenate here we'll say job get category which we have to create and I'm gonna pass in category and we want the name so I'll say name all right now if I save that and reload we're gonna get an error if we go ahead and find because we don't have get category so let's go create that inside of our job class okay I'll pass in category and we're gonna do this DB query and let's pass in select all from categories where ID equals colon category underscore ID all right now this colon category ID this is a placeholder okay we have to bind the value so for that for that we're gonna do this DB bind and let's pass in colon category ID which should be in quotes and then category ID which should be passed in up here okay and then we'll do we'll assign the row okay so we're going to use the single method or this DB single and then we're just going to return the row all right and that'll give us a single category along with the name so let's go ahead and say business and what we didn't include it in our template so let's go to our front page and let's see we'll go let's go right above the for each we'll put it in h3 and say PHP echo title okay so there we go jobs in business and if we're just on the regular home page it says latest jobs so we can now filter by category all right so we can now filter our jobs by category we have them listed on the home page now we want to do is create our individual job pages so we want this view link right here want that to go to job dot PHP and we also want to pass along the ID so let's go to let's see templates front page PHP and this is the view link right here so we're gonna make this go to job dot PHP and then we're gonna add a parameter we're gonna say ID equals and then echo job ID okay so reload you can see up here job PHP ID equals one okay now we need to create that job PHP file we're gonna create that in the root so it's a new file and save it as job dot PHP and I'm gonna copy what we have in index.php paste that in and let's change the template I'm gonna change it to templates slash job single dot PHP that's going to be the template name now we want to get the ID from the URL so right here we'll change category to ID and then change this and this okay so if there's an ID parameter it'll get put in this variable alright and let's actually call this job underscore ID alright now we can get rid of all this stuff here this if category okay and then as far as variables we just need the job itself so template job and that's gonna come from a function in the class call to get job alright we're gonna pass along the job underscore ID that we're getting from the URL all right so let's save that and then we need to create the template so inside the templates folder let's say new file then we're going to save it as job - single dot PHP okay and for now let's say PHP echo job job title okay no that's not gonna work yet because we don't have this get job method yet so let's go create that inside of the job library okay so down here say public function get job okay that's gonna take in an ID and I'm gonna grab this what we haven't get category and we'll paste that in here and then let's change the query we want to select actually it let's yeah let's do select all from jobs where ID equals ID and then we'll bind that okay bind ID to the variable ID and then we're assigning the row and returning it so let's save it and now if we go and click view you can see we're getting the job job title all right now we want the rest of the template in this page as well so we need to we need to add our header and footer okay so I'm just going to grab this from the front page that there and we also want the footer okay so let's add the rest of the data and make this look alright actually I'll put that back and that's going to go in an h2 with a class of page header okay and then under that actually know what let's put the location next to it in parenthesis so right next to it will say PHP echo job location all right and then under that will put us and put some small tags okay and that's gonna say posted by and then we want the user the contact user pretty sure that's what the field is called and then we'll say on and then let's echo the date post date alright let's save that okay there we go and then underneath the small tags I'm going to put in a horizontal rule and then a paragraph and inside the paragraph will have the description I'm actually going to give this a class of lead and then under the description we'll put a ul with the class of list group okay and then each list item will have a class of list group item and let's see in here we're going to put the company I'm gonna put it inside strong tags label and then PHP echo job company okay we're also gonna put the salary so I'm going to copy that paste it in twice this one's going to be the salary okay then we're also going to have the email contact email all right now we're gonna also put a go back button so that we can get back easily so let's put a couple line breaks okay this is gonna go to index dot PHP and it'll say go back and then a couple more line breaks okay reload and there we go there's our job listing okay nothing special but it has all the data we need all right so we can now view our jobs all right so what we're gonna do now is we're going to create our form so that we can add a job through our application interface okay we're going to create a file in the root called create dot PHP all right and let's for now just copy what we have in index and we're going to change the template here - job - create dot PHP and we don't need this or this we do want this because we're going to have the Select list of categories so we need to get the categories so let's go ahead and save it and then in our templates folder we're gonna create a file and let's save it as job - create dot PHP okay and that'll be our create form alright so I'm going to grab the let's see it's grab the header include here and also the footer and let's put in h2 here with the class of page header okay and we're gonna say create job listing and then we'll have our form so the form is going to have a method of post and an action of create dot PHP okay so let's put in a div with a class of form group and inside here we'll have a label company and then an input okay this will be a text input and we're going to give it a class of form control and then a name of company okay so that's our first field let's go ahead and save it and see if we can go to up here create listing and there we go alright now we're gonna have quite a few fields here so I'm gonna copy this paste it in a couple few times okay so after the company we're gonna have our select box for categories so let's change this to category and this input here is actually going to be a select and the name is gonna be category okay and we need to loop through our categories so what I'm gonna do is just copy the the one we have on the front page here so we'll grab this option and then this for each okay and we'll paste that in there save that check it out and there we go alright so after the category we're gonna have the job title okay change the name then we'll have the description but I'm gonna make that a textarea so let's replace this name will be description okay that needs a closing text area tag as well and then after the description will have location then salary and then we want our contact user and then the contact email okay then let's go ahead and put in our button it's gonna be a submit give it a class of BTN BTN default value of submit and I'm also gonna give it a name of submit all right let's save that that should do it for the form okay so now what we want to do is when we submit this it's going to submit to create dot PHP so let's go there and what we want to do is check to see if it's been submitted so we'll do an if we're gonna say if is set post submit and then what we want to do is grab all of the data from the form and put it into an array okay this is kind of long so I'm gonna just paste this in okay so we have in a data variable set to an array and then we're setting these job title company category I think the only one I'm missing here is contact user so let me just okay and then what we want to do is pass that to a function in our class but we're gonna stick it in an if statement to make sure that it actually works so say job create and then we're going to pass in the data okay now what I want to do is I want to be able to redirect and I don't want to have to use the header function so we're actually going to create a helper file with a redirect function all right so it's going to look like this it's going to take in the location which will be index dot PHP it's going to take the message that it can display so we'll say your job has been listed and then it's also going to take in a third parameter which will be either success or error okay and this is gonna we're also going to create a helper that will display an alert that'll be the red or green depending on if it's a success or an error all right then we'll do an else and I'm just gonna copy this okay and if it doesn't work then we're gonna say something went wrong and that's gonna be an error all right so before we create that redirect and all that let's do this job create alright so let's go into our job class ok and we'll say public function create and let's pass in here that data array now this part I'm gonna paste in okay so we have an insert query and basically we're just saying insert into jobs and then all the fields and then values and all the play up placeholders that we need to bind to all right so now under that we'll say find data and let me just grab this and paste this in okay so we're binding each value here it's just yeah okay so we're binding whoops we don't need that okay so all the the fields up here we're binding down here to the the value that's in the data array alright and then we need to execute it okay so we're gonna say if this DB execute then we're going to return true okay else then we want to return false all right and we'll save that now to create this redirect function we're actually going to create a folder in the root of the application and we're going to call it helpers and then inside there I'm going to create a file and save it as system on the score helper dot PHP now I'm gonna paste this in just tab this over all right so redirect takes in a page which by default will be false message which will be now by default and then message type and basically what we're doing is we're taking in a path and we're gonna redirect using header ok that's nothing special but we're also taking the message and then the message type and we're putting them into session variables so that we can access them after we get redirected okay because we want to redirect first and then display the message but if we try to display the message and then redirect well that's not going to work out all right so we also want a function to display the message all right not only display the message but also unset the session variables all right so we're gonna check for a message in the session we're going to assign it and then we're going to check to see if it's an error if it is it's going to send out this with alert danger that'll make it a read message if not then it'll be green with alert success and then it's going to unset the messages down here the message and the type all right so let's go ahead and save that now in the config file in in it we need to add a session start up here okay in order for these session variables to work so we just want to say session start okay and we also want to include that helper file so we'll say require once and let's say helpers / system helper dot PHP okay we'll save that now the last thing we want to do before we try it is we need to run this display message somewhere so we're going to put that right in our header ok so right at the bottom here I'm gonna say PHP display message okay so let's go ahead and try this what let's see system helper on line 3 Oh missing the ending parenthesis here okay so create job listing let's say 1 2 3 communications technology we'll grab some text well that's the description the job title I'll say database admin location say New York salary 70k contact user John Doe okay let's go ahead and submit your job has been listed and there it is alright so that display message is taking care of this for us showing us that message and then the redirect obviously redirected us to the home page all right so we're almost there with our application we just want to be able to delete and update these job listings so we're gonna add a couple buttons to the job page so let's go to our templates and then job single dot PHP and we're gonna go down under the line breaks down here and create a div give it a class of well and then inside there we're gonna have first of all an edit button okay so this is gonna go to let's see this is gonna go to edit dot PHP and it's gonna pass along the ID as a parameter so job ID okay we'll give it a class of BTN BTN default and let's see you say edit okay so that's the edit button now for the delete it's we're gonna do something a little different this is actually gonna be a form okay it's gonna be a form with just a button and I'm actually gonna give it a style attribute because we wanted to to display inline so it's to the to the right of the edit button not underneath so let's say display inline alright and then we're gonna say method equals post and action action is going to be job dot PHP okay now this is gonna have an input it's gonna have the type of hidden okay we just want to send along the ID so we're gonna give it a name of del underscore ID and then a value and in here we'll do PHP echo job ID okay then we want a button or an input with the type of submit okay we'll give it a class of BTN BTN danger and let's give it a value of delete okay save that reload and now we have an edit and delete button so let's take care of the delete first okay we're submitting to job dot PHP so let's go to that file and we're going to check for that post value so right here let's say if I will say if is set post and we're looking for the post value del underscore ID okay so if that's set then let's create a variable called del ID and set it to that okay and then we're gonna do if and we're gonna have in the job class will have a function called delete so if job delete and then we just want to pass along that del ID value okay so if it deletes then we're gonna call redirect and let's pass along the path index dot PHP and then the text will just say job deleted and that's gonna be a success what success okay and then if not we'll just say job not deleted and we'll change this to an error which will make it red okay let's save that and then we need to go into our job class and let's go down to the bottom here I have to create okay and we'll say public function delete and that's going to take in an ID and I'm going to actually I'll just grab this and create okay and let's clear out this query and then just get rid of these this binding so the query is gonna be delete from jobs where ID is equal to the ID that's passed in okay that should do it let's save it okay so we'll go back and let's see entry level programmer will view delete job deleted and it's gone okay so we can now delete them now we want to be able to update listings so let's create in the route we're gonna create a file called edit dot PHP and let's see in here this is gonna be similar to create so I'm gonna just copy everything we have in there okay now we're gonna be checking for an ID just like we did in job PHP so I'm actually gonna grab this put that there and instead of calling job create it's gonna call job update and in addition to the data we want to pass along the job ID so it knows which one to update okay and then here we'll say your job has been updated and then the template is gonna be not job create but it will say job edit alright so let's see we also need to grab this the job itself so just like we did in job PHP right here we're gonna grab that all right so let's save that now we'll create the template so in the templates folder we'll create a file and save it as job - edit PHP and then this is going to be the same form that we haven't create so we're going to just copy all of that okay except let's change it to edit job listing up here and then the action is actually going to go to edit dot PHP and then we want to send along the ID as well like that and then our values we're gonna have values in our inputs so value is gonna be PHP echo job what is this company so I'm gonna copy that value all right and the Select will do that in a second let's do these text ones okay this one will be job title and this is a text area so we don't need the value we just need that this will be job description okay this one location this one is salary contact email okay that should be good for the inputs so let's save that and let's see if we can actually see the page edit all right so there's our form now notice the category is not chosen we need to treat this a little different than the inputs we're gonna go let's see I'm gonna go right here and say PHP if it's gonna have an else as well okay so if job category ID is equal to category ID then let's cut out let's oh you know what we want this to be in the for each so I'm going to cut this and we want to put this right here all right and then we're going to take this option and cut that and let's paste that in both of these except this one here we want to add right here selected okay so if the current iteration of the category is what the the main job by the current job ID category ID is then it's going to say selected which will make that the default choice so let's save that and reload and you can see we get technology okay if I click on this one edit we get business all right so the form looks good now when we submit it it's gonna go to update which we can see right here it's gonna go to job update so we need to create that so let's go to our job class and I'm going to copy what we have for create okay we'll put that here and change it to update okay and it's also going to be taking in an ID and the query is gonna look pretty different so I'm going to just get rid of it and I'm gonna paste this one in okay so we're gonna say update job's set and then we're going to set each one except I'm missing the contact user okay and then binding is gonna be the same down here and then everything else should be the same as well so let's save that okay and then we're gonna go and let's go to this job here view edit let's change it to one two three four communications submit and let's see we're getting an error and why is that that's because there's no I forgot a comma right here alright so let's go ahead and try that again has been updated now if we save you now it says one two three four communications alright so we can now update listings [Music] you
Info
Channel: Eduonix Learning Solutions
Views: 121,426
Rating: undefined out of 5
Keywords: eduonix learning solutions, eduonix, PHP, learn php, Projects In PHP, php tutorial, php for beginners, learn php for beginners, learn php fast, learn php online, Creating A Job Lister Website From Scratch, create a job finder website, Command-line scripting, server-side, open source, Desktop Application Development, PHP project, learn php practical, php tutorial step by step
Id: LEkjrQMmIK0
Channel Id: undefined
Length: 97min 15sec (5835 seconds)
Published: Thu Jun 28 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.