Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
We have all the variables set for our pagination, so let’s start adding our limits. We are going to be limiting the number of products we show on a page, so we want to add limits to our catalog calls. This will tell the database to only return a limited number of items. Again, reducing the amount of data the page has to handle.
Example Code
function category_catalog_array($category, $limit = null, $offset = 0) {
include("connection.php");
$category = strtolower($category);
try {
$sql = "SELECT media_id, title, category,img
FROM Media
WHERE LOWER(category) = ?
ORDER BY
REPLACE(
REPLACE(
REPLACE(title,'The ',''),
'An ',
''
),
'A ',
''
)";
if (is_integer($limit)) {
$results = $db->prepare($sql . " LIMIT ? OFFSET ?");
$results->bindParam(1,$category,PDO::PARAM_STR);
$results->bindParam(2,$limit,PDO::PARAM_INT);
$results->bindParam(3,$offset,PDO::PARAM_INT);
} else {
$results = $db->prepare($sql);
$results->bindParam(1,$category,PDO::PARAM_STR);
}
$results->execute();
} catch (Exception $e) {
echo "Unable to retrieved results";
exit;
}
$catalog = $results->fetchAll();
return $catalog;
}
-
0:00
We have all the variables set for pagination.
-
0:03
So let's start adding our limits.
-
0:05
We're going to be limiting the number of items we show on each page.
-
0:09
So we want to add the limits to our catalog calls.
-
0:12
This will tell the database to only return a limited number of items.
-
0:17
Again, reducing the amount of data the page has to handle.
-
0:22
Let's go into our functions page.
-
0:25
In our catalog array we'll need to pass some optional arguments again.
-
0:29
We'll do this with limit = null and
-
0:33
offset = 0.
-
0:37
Remember by giving a parameter a default value
-
0:40
we do not require that the argument be passed.
-
0:44
If we were to call the function full catalog array without passing these
-
0:48
arguments, we would not have a limit and our offset would be set to 0.
-
0:53
By setting the offset default to 0, we can then pass a limit without an offset.
-
0:58
And it would start at the very first item.
-
1:01
If we don't have a limit, we should return all the items in our catalogue.
-
1:05
We'll need to add these limits to our SQL statement.
-
1:08
So let's move our current query into a SQL variable.
-
1:20
We'll then change our method to prepare and add an execute.
-
1:30
We haven't changed any functionality yet, but now we can use a conditional and
-
1:34
add our limits.
-
1:35
We could use MP again here, but let's be a little more specific.
-
1:40
We'll use a new built in function called is_, And we'll pass our limit.
-
1:47
We're going to add a limit, so let's duplicate this line.
-
1:52
We use this line in our else-statement.
-
1:58
And we'll leave our execute outside.
-
2:01
Let's go back up and at our limit with placeholders.
-
2:11
Limit tells us how many items we want to return and offset tells us where to start.
-
2:29
We'll use our PDO::PARAM_INT to filter for an integer.
-
2:50
And the same thing for our offset.
-
2:53
We need to make these changes to our category catalog array function as well.
-
2:57
We'll start by adding our optional parameters, limit and offset.
-
3:07
Again, we'll move our SQL statement out of our prepare.
-
3:26
And then we can add to it.
-
3:27
Let's copy the if statement from our full catalog and paste it here.
-
3:42
We need to change our bind params a little because this statement already
-
3:45
has a parameter.
-
3:51
We'll copy this line for 1.
-
3:55
And then we'll change these to 2 and 3.
-
3:58
We close out our else before the execute, so both of these will get executed.
-
4:02
Great, our functions are now set up.
-
4:05
So we need to pass in the limit and the offset.
-
4:07
Let's go back to the catalog.php page.
-
4:10
Let's scroll down to where we're sending our catalog variables.
-
4:16
We'll pass in the items per page which is our limit, and our offset.
-
4:23
Will also add this to our category catalog array.
-
4:38
Now, let's go back to our browser and refresh our full catalog page.
-
4:45
Perfect.
-
4:47
Well except for
-
4:48
the fact that we can't see any more than eight items yet, let's fix that.
You need to sign up for Treehouse in order to download course files.
Sign up