---
title: 'How to Split Data into Pages Using PHP (Pagination Tutorial)'
source: 'https://youtube.com/watch?v=jFU6PbOEw8M'
video_id: 'jFU6PbOEw8M'
date: 2026-08-10
duration_sec: 2993
---

# How to Split Data into Pages Using PHP (Pagination Tutorial)

> Source: [How to Split Data into Pages Using PHP (Pagination Tutorial)](https://youtube.com/watch?v=jFU6PbOEw8M)

## Summary

This tutorial demonstrates how to build a basic pagination system in PHP, allowing you to split a large set of data (like products) into multiple pages. The video covers setting up a database connection, creating a sample database, and implementing the core pagination logic to display data and navigation buttons.

### Key Points

- **Introduction to Pagination** [00:00:01] — Pagination is used to split a large set of items (products, images, videos) into multiple pages, allowing users to navigate through them.
- **Database Connection Setup** [00:02:38] — Create a `dbh.php` file with variables for host, database name, username, and password. Use a PDO connection with a try-catch block to handle errors.
- **Creating the Database and Sample Data** [00:09:12] — Create a `products` table with columns ID, name, and price. Insert sample data using SQL commands in phpMyAdmin.
- **Calculating Total Pages** [00:14:47] — Set a variable for results per page, then query the database to count total products. Use `ceil()` to calculate total pages.
- **Determining Current Page** [00:22:08] — Determine the current page from the URL parameter, defaulting to 1. Use `min()` and `max()` to validate the page number.
- **Calculating Starting Point** [00:28:56] — Calculate the starting point for fetching products using `(current_page - 1) * results_per_page`.
- **Fetching Products from Database** [00:30:32] — Use a prepared statement to fetch products with a LIMIT clause, binding the starting point and results per page.
- **Displaying Products** [00:34:26] — Loop through the products array and display them in a table using a foreach loop. Use `htmlspecialchars()` to prevent XSS.
- **Creating Navigation Buttons** [00:40:39] — Create previous, next, and page number buttons using anchor tags and a for loop. Show/hide buttons based on current page.

## Transcript

create a peaganation system inside PHP which is basically when you have a bunch of something for example a bunch of products or a bunch of images or videos or something and you have so many of them that you need to do something in
order to not have them all shown inside one page. So, a paganation system basically allow for you to have however many products you want inside a page. And if there's so many, then you can swap to page two, three, four, so on
until you've seen all the different products or images or whatever you might want to show inside your website. Right? So, basically, you can go through tabs in order to see all the different products. And a paganation system isn't
very difficult to make, but it does require that you know how to make it. Right? So this is why I'm here to just kind of show how you can create a basic pegation system. And just to kind of show what I have inside my example here,
I have a database called Peggynation_DB. And inside this one, I have a products table that basically just have a bunch of products inside of it. Um, so this is just to kind of show that you can use a database if you want
to, but you could also, if you had a bunch of images inside your website folder, show those instead. You don't have to use a database. Uh basically this pegation system here is just to give you something to start with and
then you can build it in however you might want to build it. You know if you have images, videos, a database in this example here. Um you can customize this in any sort of way you want. So to begin with I'm going to go back inside this
with I'm going to go back inside this tab here because this is actually my raw uh starting point for this tutorial here just to kind of show what this is. Inside my index.php PHP file. I just have the basic setup that you need to
have in order to have a web page. But inside my budget tag, I did actually inside my budget tag, I did actually include a H2 element, a table with a table row inside of it with a couple of headers just so we know exactly what is
going to be shown inside uh my table here. So, for example, the ID, name, and price of my products. And then you can see I just added in three separate table rows with just product ID, product name, product price. you know, we we could
also just have one. So, you know, to make this a lot less complex. Uh this is basically what I have inside my body tag, right? So, if I save this, go back inside my website, you can see now I just have one product. Um so, this is
just a starting point for this tutorial here. So, you can go ahead create this here. So, you can go ahead create this here, copy it down, and then we can get started. Now, specifically for this tutorial here, we're going to be using a
database. And again, after we're done with the tutorial, you can customize it in any other sort of way you might want to. But to begin with, we're going to do this using a database, which means that we need to have a connection to our
database from our website. And the basic way you do that, if you've never done that before, is you create a new file. And you can call this file anything. You can, in this case, I'm just going to call mine
dbh.php because that stands for database handler. So basically this just handles the connection to the database. Inside this file I am going to create a basic connection and the way I'm going to do that is I'm going to include four
different variables. So again we need to open the PHP tags here because otherwise you can't write any sort of PHP. And inside the PHP tags we're going to add four different variables. The first one is going to be the host name, database
name, username and password. And basically what we're doing here is we're just adding the information to our database that we have connected uh using in my case here XAMP. If you don't know how to use XAMP, I do have a basic
tutorial on how to use it and how to set it up so you can actually get a uh server started with a database connection. Um I I'll link it somewhere here. There's going to be like an overlay or something, right? Um, but
basically this is going to allow for us to connect to our database because we need this information to do so for a clean X amp installation. This is the the database host is going to be local host. Database name is going to be
whatever you might call your database. We'll talk about how to set up the database in just a second. And then you're going to have a username set to root and a password set to nothing. And I do also have a tutorial on how to
actually change these different um like the username and the password. So if you want to change that inside your database. So you can actually write a custom username and password for your local database. Um I'll have a video for
that too. It'll be linked below in the description or something or here on next thing we're going to do is we're going to go ahead and actually open up a connection to our database using this information here. So, we're going to run
a try catch block. Uh, basically, it's going to be a try with a pair of curly brackets and then we have some code in between here. So, it's going to try to between here. So, it's going to try to run this code and if it catches a error,
then it's going to display a error inside this block down here. So, inside the parenthesis of the catch block, uh, we're going to set a PDO exception connection to our database. So, you can see we have PDO exception popping up
there. And basically, we're just going to say that we're going to reference to a variable E for exception if we do actually encounter any sort of errors database. So, we're going to try this block of code. If we catch a error, then
we're going to run this code down here. Now, our database connection is going to look something like this to begin with. Now, I'm just going to wrap it down to is going on. Uh but basically we have a variable called con for connection and
variable called con for connection and we set it equal to a new PDIO connection which is a uh object inside object-oriented PHP. Um it's not really important to know right now what exactly that means but basically we're just
creating a new connection uh to our database. So here inside the parenthesis going to look like this but I do have some data inside the parenthesis here and because it's so long uh it's wrapping down to two lines. So the first
one is going to be inside double quotes and it's going to have this information here. Basically we're saying we want to set up a MySQL connection because we're using a MySQL database and the host name is going to be set equal to variable
host which I set up here. So that is going to be local host in our case here. You could also just write localhost down here instead. Um but because we want to uh we're going to create the variables up here instead and just reference to
those because it looks better inside PHP. Um the next thing is going to be our database name. And you can see actually separated with a uh semicolon. And the database name is going to be equal to variable DB name which is again
up here. Again you can call this whatever you want. If you have a maybe just a name for your website or something in this case because it is a something in this case because it is a paganation tutorial I call mine
peggination_db. But really this could also be the name of your website particular site that you're running. Right. Um the next thing I added in is character set which is equal to UTF8 which is just something standard you
need to put in. Uh the next thing after the comma which separates this first parameter. So we go down to the second parameter which is going to be the username for your database. And the last parameter is going to be the password.
And with these three different parameters you now have a variable that is going to actually create a connection to your database. So what we can do now is we can actually go ahead and set a attribute for this particular variable.
So we're going to say variable con and we're going to point to a uh method called set attribute and inside set attribute we have a couple of attribute set to certain things. So in this case here we say that we have a PDO and I
want to point to a static method called attribute error mode. And the same thing error mode_exception. So we can actually run this exception down here inside the cats block. So if we do actually get some
sort of error message with this connection here. Let's say it fails or something, something needs to happen which goes inside this cats block and we're going to basically just say we want to kill off whatever code we're
running here. uh we're going to run a die uh method which is basically just is going to be connection failed and it's going to tell us what kind of uh error actually happened. So it's going to get our exception meth message in
this case here and with this we now have a basic connection to our database. It's really this simple here. Uh so whenever we want to connect to our database we just reference to variable con and use that one in order to point to our
database connection. So we can actually start doing things inside our database. So with this file, we are now done with the database connection. And the next to start creating the actual peckination system because this was just in order to
system because this was just in order to get stuff showing inside our website. U because like I said, I already inside my database here have some actual products that I want to show inside my website. Now you don't have anything right now
inside this database here. So what you're going to do is after you install you're going to do is after you install XAMP and you have your PHP myadmin open which again I show inside that tutorial you're going to go inside where you have
the front page go to databases. Then you're going to go below here you're name. In my case here I called mine pegging DB and then you're going to click create. Once you do that, you now have your database over here in the left
side. You're going to click it. And then you can see right now, at least for you, you have nothing inside your database. Now, I have a table called products. And the way you create that one is after you click paganation DB, you're going to go
to SQL. And then you can write some SQL code here in order to create tables and other things inside this database. delete things, insert things, drop the table or whatever you might want to do. Um, and I do actually have some code
here and I will show it to you in order to actually create the exact same thing that I have inside my database. So, the first thing you're going to do is you're going to create the table which looks like this. So, this is the code that you
need to run in order to actually get the actual table created. Basically, we're saying here want to create a table called products. And inside this table, we need to have some actual columns that has certain data inside of it. So we
one that's the name. And then we have one that's the price. And then you know you have these three different columns where you can insert information about a product. Do keep in mind that I do not have a comma at the end of the last one
because otherwise you might get a error message. So after you paste this in, you're going to go down and it's going to say if I zoom out again, uh it's So you're going to click it and it should actually create the table inside
Peggynation_DB. Now once you do that, you're going to go back again. Yeah, I'm not going to save my changes here. I'm going to go back again to Pegganation_DB. Go to SQL. And then you want to make sure you paste in an actual
product. Now, I have a bunch of products here, and again, I'm going to paste them in so you can actually see them. I will go ahead and delete a bunch of these just so you don't get overwhelmed by what I have here. Uh, basically, this is
a insert statement that says I want to insert into a table called products that has columns called name and price. Now, I know there's also a ID column, but
because that one is actually set to auto increment, which basically means that it is automatically going to fill in a new ID for each product whenever you create with that one. You're just going to go ahead and fill in information when it
comes to name and price. And then you want to insert some values. So, in this case here, I have product number one, and then I have a price for that particular product. Then I have product number two and a price for that
just go ahead and copy paste this again and again and again until you have a bunch of products. So you can decide how many you might want inside this uh database here. Um and once you're done, you're going to scroll down. You're
going to click go. Again, if you want more products than just these, let's say you create three products and you realize, okay, maybe maybe I needed like little bit more exciting, you know, you can just go ahead and copy paste this
again, paste it in in the exact same spot. Make sure you click packination_db first and then you can just go ahead and add more if you wanted to. So, I can just say I want to copy this. Um, let's actually go and copy this properly like
actually go and copy this properly like so, comma. Paste it in. paste it in and have a comma. Right? So you just paste in as many products as you want. Change the product name, the price, and then you can just click go again and it's
going to insert those as well inside your products table. So that is just basically how you can keep creating new products. And it's really that simple. products. And it's really that simple. So now you should have a products table.
And inside the products table, you should have a bunch of products that has an ID, a name, and a price because remember the ID is auto inserted as you people might know how to set this up. So, this is more or less just a skip
part of this video here. Um, but some people, they'll watch this and they'll be like, "Oh, I've never set up a database before." Um, so I'll have to show you how to do so and actually get through this. Again, I have an entire
PHP course that shows how to do stuff like this. So, if you're interested, I have that. Um, but for now, this is just like a a very quick run over, you know, run through of how to set this up. Um, so going back inside our website and
then inside our editor, now we have a database with actual products. So, our database part of this tutorial is technically done because we have a connection now and we have actual products inside our database that we can
actually pull out and show inside the website. So, in order to create the paganation system, we're going to go back inside our uh website root folder. going to call this one peggy
nation.php just to give it some sort of name here. Inside peggynation.php, PHP. The first thing we're going to insert is, of course, our PHP opening tags. Now, we're not going to include closing tags, which you may have noticed that we
also did not do inside our DBH file because if you do that and then create something, let's say some HTML u I don't know, like a paragraph or something below here, um it might give you some error messages and stuff. So,
you some error messages and stuff. So, it's it's very important that if you only have PHP inside a particular page, just do not have the closing tags cuz it can give you error messages and stuff like that. So, don't do that. Uh the
first thing we need to do inside our PHP tags here is include a results per page variable that is going to tell our website how many products do I want to have shown inside each of these pegation pages that we're going to create. This
number here you can change to however many you might want to have. So in my case here I have five could also be eight or 10, you know, depending on what you might want to have. Uh, let's make it fun. Let's make it something like, I
don't know, let's just make it eight to have something. And the next thing I want to do here is I want to actually figure out how many products do I actually have inside my database because we need that number in order to
calculate how to create this peagan system here because we need to divide it by how many results we want to show to figure out how many uh tabs do we need for our peanation system. So, uh, what I'm going to do here is first of all,
I'm going to go in and actually run or create a SQL statement. This is going to be something we run inside our database in order to grab the data and do something with it. So, in this case, we want to just sort of count how many
products do we have. We're selecting all the ids from our products table and we the ids from our products table and we want to grab the count to count how many ids we actually get from our database table and I want to create a reference
table and I want to create a reference called total that I can use later on to thing we're going to do is we're actually going to run this SQL statement inside our database because we need to actually run this to get some data from
our database. Uh we're going to do this using a prepared statement because you should always do that in order to make this a safe uh at least safer way to grab data inside your database. So we're going to create a variable called
statement. I'm going to set it equal to variable con. And where do we have that variable con. And where do we have that from? We have that from inside our dbh.php file because it's right down here. We're grabbing our connection. And
then I want to run a method from within that connection because this is a object called prepare. And I want to just tell it what SQL statement to run inside this connection here. So in this case we grab
the connection to our database and then we run this SQL statement. Now we don't actually run the connection yet because we do need to do that below. Right now we're just preparing the SQL statement to be run. And then we go below and then
we actually say okay so now that we have this statement that has this SQL query now I actually want to execute it and run it inside the database. So once we do that we now need to grab the result which is going to be how many ids or in
this case rows we have inside our table. Uh so we actually know how many products we have and we're going to insert that inside a variable that we're going to call total results. And the way we grab the data is simply by again referencing
to the statement that we ran. And then we say we want to point to a fetch method which is basically grabbing uh all the data from inside the database but as a associative array. And then we're referencing to total which is the
one that we have up inside our query. in order to do so. You don't have to do as total in order to get this working, but it's just kind of a a neat thing to show reference to uh something specific
actually reference it inside your code. Um, so you don't need to have this total here as a reference inside this code here, but it's just kind of a neat way to do things. Basically, what this is going to give us, total results, if I
were to actually echo this out inside my web page, is it's going to give me the total count of all the different products I have inside my database. So, basically, we have one piece of data up here that is just the basic, you know,
how many products they want to show per page inside my packaging system. And then all of this code is basically just to figure out how many products do I have inside my database. And the next thing I want to do now that I have these
two pieces of data is I can actually calculate how many peagan pages is required inside my peagenation system in order to get this you know how many tabs
working inside my peagenation system. Um because we need to actually loop out the buttons and change the numbers from one two three four you know depending on how many pages you have. uh but in order to do that we need to figure out how many
tabs we actually need. So in order to calculate that you basically just take the total number of products that we have inside our database and we divide it by the number of results we want to show per page inside our peanation
you'll notice that I did actually include another function inside this calculation here. Uh so basically we have total pages pointing to a function called ceiling that has the calculation I just talked about. We divide this one
by this one. Right? To better explain this I think I might change this to 10 people to understand. Let's say I have 10 products that I want to show per page
inside my peanation system. But inside my database I have 15 products I need to my database I have 15 products I need to show. If I divide 10 into 15, I'm going to get 1.5. Now, we can't show 1.5 buttons
inside our paganation system because that means one and a half button. So, I would need to actually show two buttons to show all the products inside my database, right? Because we need to round up to two because that's the
number of buttons we need. So ceiling basically just rounds up to the next whole number. So if I were to get one and a half, that would actually give me two. So it's important that we run this function here in order to get a whole
number of pages we need to have inside our peckination system. That's why we run the ceiling function here. So with these three pieces of data, the next thing we need to figure out is what page we're inside of right now inside our
something we need to know inside when we create this system here. Um so if I'm inside page two, I don't want to show inside page two, I don't want to show product 1 to 10. I need to show product,
you know, 11 to 15. Let's say I have 15 products inside my database. Um, so we need to figure out what page we're inside of so we know which products to show inside that particular page. So to do that we're going to go down to next
line here and we're going to create a if else statement. Basically inside the if statement we're going to say that we want to grab a URL parameter called page. So that is going to be what we do here. We go inside and we run a get
method. Now, if we do not have a page set inside the URL, then we just want to set a default page, which is going to be page number one. But if we do have a page inside the URL set equal to a certain page, then I want to create a
variable called page and set that one equal to the page number and I'm going to cast that one into a integer in order to get an actual integer data type. Um, programming. We can cast it into a certain data type. uh in this case a
integer data type so we can calculate with it inside our code here. So basically we're just saying do we have a page inside the URL equal to something if so then grab that number if we do not have it then create a default page that
is equal to one and we do actually have a shorthand way of creating this which I just going to go and delete actually we can just comment this out so you can actually still see it. Uh if I were to comment this out, uh another way to
actually write this would be like this. So we're basically just creating a variable called page and we set it equal to a statement. In this case here, we're checking for this statement here. And if that one actually exists, then we want
that one actually exists, then we want to uh set page equal to this over here. the back here that says well okay so if this one is not actually going to return true then create this value instead. So basically this is the if statement and
this is the else statement. This is how you see people program later on. Instead of creating all these if else statements and stuff like that then you know you start creating one one liners like this in order to write the exact same code.
uh we're going to write it like this and we will I think use it one more time inside this tutorial here. Um so the next thing we need to do is we need to make sure that when we grab this page this page number from inside the URL
that is actually a valid page range. Let's say I have two pages inside my my paganation system but inside the URL the user the evil user goes inside the
website goes into the URL and writes five but there is no page number five because I don't have that many products but he does it anyways and he types enter and all of a sudden our code is going to throw error messages because oh
there is no page five um so what are we going to show right so we need to prevent that from happening uh same thing for minus2, you know, because we can't go into minus. I don't have minus pages inside my packagation system. Uh,
so we need to have some code preventing the user from trying to mess up our system here. Um, and the way we're going to do that is by writing this one line of code down here. I think the easiest way to explain this is just by grabbing
this method first and just showing that one first. Uh, basically we're taking two values and we're saying which one is the smallest. So in this case here we were grabbing page which is what is assigned
to page. So let's say uh the the current page number is one right and then we have the number of total pages which is two if I have 15 products inside my database. So basically this function here goes inside and prevents the user
from writing something like a thousand inside pages equal to inside the URL because then it's going to default over to um the number of total pages because it cannot be higher than two pages. If I have 15 products inside my database and
I'm dividing by 10, it's going to be two pages, right? Um so it's it cannot go pages, right? Um so it's it cannot go higher than two pages with this function here. But what if the user goes in and writes minus something? Well, then we
need to go out and also run a max function around these pieces of data here. Well, basically the first parameter is going to be one. And the second one is going to be this function we just ran that is going to not allow
the user to type something higher than the total number of pages. Because let's say the user actually went in and wrote minus 10. Then it's going to say, well, okay, but that shouldn't be allowed because the number inside page is equal
because the number inside page is equal to should not be smaller than one. If they write something smaller than one, it's going to default to one. I hope this makes sense because basically we're just going in saying uh we don't want
the total number of pages inside our pagination system inside the URL. But it we also don't want to to write something smaller than one. And the last thing we grabbing the data from the database and showing it inside the website is we
actually need to calculate where it needs to grab the data from. Like what is the starting point inside our list of products? Because let's say I'm inside products? Because let's say I'm inside page one and I need to show 10 products.
Then it should start at number one to 10. Right? Actually that is incorrect is 10. Right? Actually that is incorrect is to start at number zero to nine because when we calculate inside programming we always start counting at zero right
because that's just how we do things. So it in order to show 10 products is from it in order to show 10 products is from 0 to 9. That is going to be 10 in total. And let's say we're inside page number two. Then we start counting at 10 to 19
products. So basically we need to figure out the starting point. So inside page two the starting point would be 10. Inside page three the starting point would be 20. So we need to figure out where to start counting from inside the
database when we grab 10 pieces of data that we need to show inside that particular page. So in order to get that we do it by running this particular line of code. Uh basically we say we want to write a variable called start from and
then we set it equal to the current page the current page number we calculated up here. We can actually combine these because they kind of go together. Um, and then we say minus one because remember if we're inside page number
one, we need to minus one in order to start counting from zero inside the database because that's like I said how we count inside programming. So this simple little way of just minusing by one is going to solve the whole issue of
counting correctly inside the database. And then I want to multiply that by however many results per page I want to show inside this particular pegation system. Had the page number been four, let's say I had that many products
inside my database, then 4 minus 1 is going to be three, then 3 * 10 is going to be 30. So, inside page number four, inside my peanation system, it's going
to start grabbing products inside my database, counting from 30 inside the database. I hope that makes sense because this is like the best way I can explain it. But basically, it's going to start grabbing our products from uh this
particular starting point based on what page you're inside of right now inside the peanation system. So we don't grab product number one every single page, you know, we need to start counting from the next um group of products that needs
to be shown. And then basically we do the exact same thing as we did above here. We run a prepared statement that queries the database. So we're going to go down below here. In this case here, we're running a select statement that
says we want to grab all which is what this symbol means from our table called products. And we want to create a limit because we don't want to grab all the products in the entire table, but we want to start grabbing from a certain
point and then a certain number of products below that. So in this case here, we're going to do a start from and a results per page. And these two up here are references. So we're going to tell it what these numbers are further
below. Um so we're going to say we want to actually prepare this statement. And then we want to bind a value for each of these references up here. So we want to start from a certain point which is going to be our variable called start
underscore from which is the one we have up here the last one we calculated. So if I'm inside page number three then minus one is 2. 2 * 10 is 20. So we want
to start from product number 20 inside the database. And then we need to tell it how many products below we want to grab. In that case, it's just going to be results per page, which is the next one down here, which is the very first
one we have up here. So, we want to grab 10 products below that starting point, and actually have that we want to execute the statement and then bind that
to a variable called products, which is going to be passed in as a associative array. So basically it's just going to be a PHP array that allow for us to
reference the product data using the column names. That is going to make sense in just a second. So if I wanted to grab our product name, I would just simply go down reference to variable products which is a array with all the
data inside of it. I want to use these square brackets. And then I want to go in and say I want to grab name because that is going to be the product name. If I want to grab the product price, then I just reference to price because that
particular name inside the double quotes is going to be our column name inside our database. So we have ID, name, price. Like those are the things we're referencing inside the square brackets down there. So let's go and delete this
last line of code here. And this is basically all the code for the pegation system in order to actually start showing the data inside our page. So what we can do now is we can go back inside our index page and just kind of
adjust the code down here in order to actually show the actual database data instead of just like default static text like we have here. So the first thing I like we have here. So the first thing I need to do is actually pass in or
include the two different documents that we created over here, the database connection and the paganation system at the very top of our index page in order to actually use the code from inside of them. So I'm going to go at the very top
going to create our PHP tags. Now because this is not a pure PHP document, we want to make sure we have the closing tag. And then we basically just go in and say we want to reference our database connection and we also want to
reference our peanation page. And the order here is very important because first we need the connection and then we need the pagination code because in that order inside our pagation file we are actually using the variable con
connection. So we need to have that linked above this code here which means that the order inside the index page is also database connection first and then the peckination system. If you switch these around it's going to throw error
saying here. Um, so the next thing you need to do is you want to make sure you actually change the data inside the table here. So the first thing we're going to do is we're going to run a for each loop inside our code where
basically we loop out the table rows depending on how many products we want to show inside this particular page inside our pegation system. And of if I go back inside our pegation page here by the results per page we have up
here at the very top. So, inside our index page, I'm just going to go ahead index page, I'm just going to go ahead and delete this table row here. Then, I'm going to create a PHP loop. And the way I'm going to do that is actually by
using the opening and closing PHP tags two times. And just to not confuse anyone, me doing it this way is something that you'll see people do inside PHP. Uh, but basically this is the exact same thing as this right here.
At least right now that we have no HTML in between the tags. It's just basically I were to write like this, you can see that my text editor isn't really recognizing this as HTML. So I would actually have to echo this out inside uh
quotation marks, you know, like this. This is not really optimal when it comes to mixing PHP and HTML together. So instead by opening and closing right after each line of PHP code here I can just go in and write HTML without having
to echo it out first. So this is the preferred way of doing things. Um so by running this for each loop which we're using because right now our
products at the very bottom here is an array and when we want to loop out array array and when we want to loop out array data we use a for each loop that is like a basic PHP thing right whenever we want to loop out arrays we use a for each
loop so basically we're going in and we want to say that we have a products variable and I want to reference or create a reference reference called product. So whenever I go inside this loop here and reference to product, I'm
referencing to the first loop iteration or the first array data inside products. So if I were to go inside the loop and say I want to um let's say product again
square brackets go inside here and grab name and again we would actually have to open and close our PHP tags here because of the way we're doing things. So we're going to open and close. Then we would actually get the name of our product and
then it's going to go to the next loop or the next uh array index which is going to be the next one in line. And basically if I were to echo this out, it's going to actually output the name of all my products or at least the 10
of all my products or at least the 10 products I'm looping out um one by one actually save this and now before we actually see this inside the browser I just want to make sure we spell things correctly Daniel because inside my
require once up here I want to make sure I'm linking to pagination.php because that is the name of my file over here. So doing that and then going inside my website you can now see that we get ID name price but above
I'm getting product one product two product three product four product five and so on but I'm getting 10 products notice that um so if I were to go inside the URL here and say I want to grab my index php and then I want to say
question mark page equal to 1 then you're going to notice we're still going to get product 1 to 10. But if I were to change this number up here to two, we're now getting 11 through 18 because that is how many products I have inside my
database. So apparently I have 18 products in total. Um, but we're getting the next products or list of products depending on the page number inside my URL up here. And that is what we set up inside our code. Now, of course, right
now it's not really looking correct because the the headers are below and basically just because right now we don't actually have these um output inside an actual table row. As you can see, we have table row and then we have
table headers. These down here should also be table row and then the data inside those table rows. So, the way we would actually do this is inside the for each loop we're going to write them like this. So basically we create an HTML
table row. We open and close it. And then inside we create table data and have three of. You can see we have an opening and a closing tag. Opening
closing tag. Inside of each of those we're going to insert a PHP output where basically we're echoing out our product ID and then our product name, our
notice that I did actually include some other stuff in here like some extra functions. I'll explain those. Um, but we're basically just echoing out a piece of table data or three pieces of table data inside a row. Now, the HTML special
characters function we have up inside the first two here are basically just output any sort of data inside a website from a database in order to um not have people create malicious code that gets run inside your website. Um so you just
from a database that you include it inside HTML special characters which is a PHP built-in function. And below here, we just basically output our price, but I run a function that goes in and formats the number to two decimal
points. So, if the price has like a bunch of decimal points behind it, I want to say that well, okay, but does it only have two decimal points? And again, characters here because again, we're grabbing it from a database. Just going
to go and do that. Going to make sure we have it closed off at the end here. Um, so this is basically just to zoom out so you can see everything. Um, how to output the data inside our for each loop. If I were to actually go inside my
web page and refresh, you can now see that we're getting IDs, we're getting names of the products and the prices. So now you may notice that we're missing one more thing in order to actually have this completely done inside our website,
which is right now there's no buttons. Like we can't go to page one, two, so on. There's no previous, there's no next button. So those we have to create now button. So those we have to create now inside our index page below our uh like
the rows that we're actually outputting here. Um so below the table we're going to create a div container and the div is going to contain all the different
buttons that we have inside this paganation system. So inside the div different types of buttons. We're going to create a previous button if we're not inside the first page. Then we're going to create all the different pages for
on how many products we have inside our database. And then we're going to create a third type of button which is a next button that is going to be there if we're not on the last page inside this paganation system because if we're on
the last page you should not be able to go forward to another page because there is no other page to go forward to. So what we can do here is we can create our opening PHP code tags and we're going to close it off
directly after. And what we're going to do is we're going to create a if condition that is going to check for a certain thing. And we're actually going to go ahead and copy and delete the closing tag of this PHP code because we
want to close off our PHP code again right after the first curly bracket. And then we want to open the PHP code right before the closing curly bracket
same way as we did up here by opening the PHP and then closing it again you know immediately after in order to write HTML in between. So the way we're going to do that is simply by doing it like this. So inside the if condition we're
going to check for variable page which is going to be equal to a certain page number inside this paganation system and I want to check if it is greater than one. So if we're not inside page one then show this button inside this
particular if condition. So in this case here I'm just going to create a regular anchor tag that is going to link to the previous page. So a page less that we're inside of right now. So inside the anchor tag I'm basically going to say
that the hyper reference which is the link is going to be equal to dollar sign link is going to be equal to dollar sign page equal to which is the URL link that is going to be up there and I want to set it equal to the current page number
minus one because I want to go back one page right so basically this is going to output uh the previous page inside our peckination system and this little icon that you see over here the the what do you call the ambersand lequi is
basically just a way like a unic code for a certain symbol which you can that particular symbol there so we're just creating a back arrow and then I write previous right after the back arrow so basically this is just creating
something very simple if I were to actually show it here you can see we get arrow that I'm actually creating in there if I click this button you can see right now my page number is two but then it's going to B1 and then it disappears
because now we're inside page one. So it should not show anymore. Then I'm going to copy paste and paste this right below here. And this time I want to check if variable page is lesser than the total number of pages I should have available
inside this paganation system. And where do we get that variable? Well, we get it from inside our paganation.php. So if I go up here, you can see we have total pages as a variable. That is the total number of pages we should have
allowed inside this paganation system based on how many products we have inside the database. So I'm going to say I want to say that it has to be lesser than that particular number there. And if it is then I want to show
a next button. And in that case it's going to look a little bit different. So I'm just going to go and copy paste it here. It's going to look like this instead. So now we're just simply saying variable page, but instead of minus one,
variable page, but instead of minus one, we say + one. And then I create a next text with another symbol, which in this case is going to be a forward arrow because just to show the user that you're moving forward inside these page
systems here. So if I save this, go inside my website, refresh it, you can see, oh, we have a next button. If I click it, we go to the next page. And now we have a previous button. If I click this one, now we have a next
button. And we can keep doing that back and forth. Uh, but basically just to show that this is actually working. So the next thing we need to have is the actual, you know, numbers showing what page do you want to go to and that is
going to go in between what we have here. So it's going to go in between here. So it's going to go in between these two pieces of code. So basically But in this case here, we're actually doing things a little bit different
because instead of a if condition, we're actually creating a for loop. And in this for loop here, we're basically just going in and looping out the number of buttons that we need inside our pegation system based on uh a certain number we
have inside our peagan.php file. So inside the for loop, we create a variable I which is equal to one because we need to have a starting point. So in this case, it's going to be page number one. And as long as I is lesser than or
equal to the total pages that we should have inside this paganation system, which is the same number we have uh that we used down here in the bottom one, then it should just continue to loop out a new button that has a new number, so
basically just want to add one to variable I. So basically, we're just looping out page numbers until, oh, there's no more products inside the database. So now we're going to stop looping and the button is going to look
like this. So just to kind of explain what this does because it's not too complex. Um so we're creating a anchor tag and we're saying that the hyper reference which is the link to this particular page is going to be equal to
variable i. So right now if we're inside the first loop then the page number is going to be equal to one. If we're inside the second loop it's going to be equal to page number two. So we're basically just looping out buttons and
then we're saying that the button is actually going to be called also variable I. So button uh the button is going to have number one inside of it. And we're just continuing to loop until we run out of pages that we can actually
create which we already calculated inside the peanation. PHP file. So if I save this, you'll notice going inside and refreshing that we have page number one, two, and then next. So if we click on page number two, you can see we're
inside page number two and our next button disappeared because we're inside the last page. Uh just to properly demonstrate this because right now we have like 10 products per page. If I were to actually take that number and
reduce it inside my pecking.php PHP file just to properly show this. Let's say I want to have I don't know four products per page. If I were to do that, we're going to have a lot more pages inside our peanation system. So when I refresh,
you can see that now we have five pages because we have a lot less products per page. So we go to page number one, you can see that we have no previous button because it goes away. We can click on page number two, three, four, five, and
then we have the last two products on the last page. So basically we can just continue to you know see all the different products or images or videos whatever you might want to have inside this system here uh in order to create
this peagan system. So this is a very barebone basic peaganation system if you want to you know have this inside your website. I do want to point out that there is no CSS in this video here because you can see my little demo here
at the beginning uh did look a little bit different because there is CSS here. Uh but because my subscribers specifically told me not to include CSS because it takes too long. Um it is something that is going to be included
inside the lesson material for the supporters of this channel here. Uh which will be linked inside the description. So if you want to have my uh personal notes here with you know all the CSS and you know comments for every
line of code that explains what exactly things do I'll have all my personal files here inside the description for all the people who want to support the channel. So with that said I hope you enjoyed this little lesson here and I'll
enjoyed this little lesson here and I'll see you guys in the next one.
