PHP PAGINATION IN 10 MINUTES

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome back to PHP basics my name is Shaun over the past couple months I've had some requests to go over pagination or pagination whatever you want to call it and if you're unfamiliar with what pagination is essentially it's where you display a certain number of rows per page and you can go through different pages to eventually display all of that data kind of like I've got here on the screen so that's what we're gonna look at today so let's just jump into well I tell you what first of all let's just kind of look at what I've got set up here I've got some records in a database obviously and right here I'm displaying the ID the make the model and the serial number of some computer desktops that I have in my inventory system so what we'll do is we'll just go ahead and hop into our PHP editor and let's just wipe all this out so if i refresh here we should just get back to my main page there we go alright so the first thing that I want to do is connect to my database what we'll do is we'll just create a variable called MySQL I and it's going to equal a new my sqli and this has four parameters we have the server name which for me is localhost the user name that you'll use which for me is password or I'm sorry root your password and then the name of your database and mine is just called test and I also don't have a password so I will leave that blank all right so now let's determine how many records we want per page right currently I've got it set up as 10 so I'm just gonna create a little acronym or acronym here are PP results per page well equal 10 we can make that whatever we want and now we need to check to see if we're actually getting the page from the URL and to do that what I'll do is I'll just set up a shorthand if statement so I'll say is set this will be the gets page variable and the page is going to equal get page if it is available if not then page will just equal zero into that of course you'd want to use my sqli real escape strings or make these integers and that would protect your page right here we're going to check for check for set page we'll say all right so now we need to check to see if it's actually on page 1 or not because the formula that we use to display the records will not work on page 1 because we're starting at 0 okay so what I'll do is check for page 1 I had to make this a little bit simpler I'll just use a regular if so we'll say if page is greater than 1 then our start page is going to equal the page times the results per page - the results per page alright so basically if I'm on page if I'm displaying 10 records per page right if I'm on page 2 then that's going to equal 20 well I don't want to start on record 20 I want to end on record 20 so we're going to multiply it times the number of Records which would be 20 and then we're going to minus or take away 10 which will bring us back down to 10 so 10 on page 210 will be our first record okay this will say else start equals 0 because that means we're on page 1 all right so the first thing that we need to do is query the database for the total number of records not just the first 10 or the second 10 of the third 10 but the total number of Records to do that I'm going to create a variable called result set and this will be a MySQL like query i'm just gonna say select ID from inventory let's make a comment here and then get total records let's call this number o's equals the result set num rows alright so with this we can calculate how many number of pages we're going to have to display so we'll say get total number pages and total pages is going to equal the numbers divided by the results per page so currently I have 81 records I believe so that's going to be divided by 10 alright so now that we've actually got the total number of records we can start selecting the records within the criteria that we want so we'll we'll use results set again because we're done with this result set result set equals a MySQL query we're just going to say select all from inventory limits and we're going to limit from the started value the starting value to the results per page okay and you'll see what I mean here in just a minute so real quick let me jump in and refresh this and see if I've got any syntax errors and I do and there we go all right great so let's jump down into actually displaying these records I should say query results alright so what I'll do is I'll start a table echo table all right now what I can do is the standard while loop and we'll say rows equals a result set fetch a sock and this is pretty typical from just displaying any data on to a database or on to your page so we'll make a variable for ID and that's going to equal the rows ID and I apologize if I'm kind of going through this quickly this seems to be a process that I do in every single video so you should have this part down the next will be make model and serial all right what I'll do at this point is I will just echo the row so let's just say we have a new row for TR and then we're going to have a column for the ID now let's copy and paste these make model and then serial number and then we'll close that row and then whenever we're done we'll break out of our table so echo table okay so right now we're on page one it should show record one through ten whenever i refresh the page and it does okay so now we need to tell you what we can do if we do a question mark and page equals two you can see now we're looking at eleven through twenty okay so if we didn't use that minus or subtract from the number of records per page we'd actually be starting at 20 so this is just kind of how we tell PHP to go back all right so now we actually need to set up the links all right so what I'll do here is I'll create this is break that paragraph here what I'll do here is I'll just create a for loop basically to step through all of our our total number of Records so we'll say 4x equals 1 which is page 1 and X is less than or equal to the total pages then we're going to tell X to increase by 1 okay and then here I can just echo our link so a graph equals question mark page equals x and then we'll display X and then close the link okay so that's pretty simple basically we're creating a link 1 through almost 80 I don't remember it should be like nine eight okay so if as I click through these you can see that we go to the appropriate page however I know that I have more records to display and it's only going to eight well the reason is if I was to echo out total pages here at the top of the screen it's eight point one okay so that creates a problem so what I can do is I can just do total pages plus one and we'll get rid of this all right so now I have nine and on that ninth page it shows that 80 first so in my code now I can I can check to see if it's an odd not or if it's a if it's a whole number or not but for simplicity I'll just say plus one okay we probably need to separate this out a little bit okay so now we can just step through here and that creates our basic pagination now in the original example that I'd shown it was styled with CSS and things of that nature of course you can plug that into it as well but this is kind of the easiest overview that I could come up with to show you how to use pagination so if you have any questions please leave comments below and thank you guys so much for subscribing that definitely means a lot to me it means that you're into the things that I'm doing and it makes me want to keep doing this so I definitely appreciate that I'll see you guys on the next one
Info
Channel: phpBasics
Views: 16,699
Rating: undefined out of 5
Keywords:
Id: 35imHB54djM
Channel Id: undefined
Length: 11min 58sec (718 seconds)
Published: Thu Dec 15 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.