Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Bethany Norton
4,818 PointsPaginating a List
I have copied the code from the tutorial Paginating a List model & view and if I leave the $products = get_products_subset, then I am getting my shirts to display but it only displays 1-8 no matter what page number.
I am also getting an error saying that $positionStart & $positionEnd are undefined. change those to $start $end it doesn't make a difference in terms of the pagination.
What am I doing wrong?
View Code:
require_once("../inc/config.php");
require_once(ROOT_PATH . "inc/products.php");
if (empty($_GET["pg"])){
$current_page = 1;
} else {
$current_page = $_GET["pg"];
}
$current_page = intval($current_page);
$total_products = get_products_count();
$products_per_page = 8;
$total_pages = ceil($total_products / $products_per_page);
if ($current_page > $total_pages){
header("Location: ./?pg=" . $total_pages);
}
if ($current_page < 1) {
header("Location: ./");
}
$start = (($current_page-1) * $products_per_page) + 1;
$end = $current_page * $products_per_page;
if ($end > $total_products) {
$end = $total_products;
}
$products = get_products_subset($start,$end);
?><?php
$pageTitle = "Mike's Full Catalog of Shirts";
$section = "shirts";
include(ROOT_PATH . 'inc/header.php'); ?>
<div class="section shirts page">
<div class="wrapper">
<h1>Mike’s Full Catalog of Shirts</h1>
<ul class="products">
<?php foreach($products as $product) {
echo get_list_view_html($product);
}
?>
</ul>
</div>
</div>
<?php include(ROOT_PATH . 'inc/footer.php') ?>
and the function code:
function get_products_subset($start, $end) {
$subset = array();
$all = get_products_all();
$position = 0;
foreach ($all as $product) {
$position += 1;
if($position >= $positionStart && $position <= $positionEnd){
$subset[]=$product;
}
}
return $subset;
}
2 Answers
thomascawthorn
22,986 Points<?php
function get_products_subset($start, $end) {
$subset = array();
$all = get_products_all();
$position = 0;
foreach ($all as $product) {
$position += 1;
if($position >= $positionStart && $position <= $positionEnd){
$subset[]=$product;
}
}
return $subset;
}
Here you can see you're not using $start or $end inside the function. Instead, you're using $positionStart and $positionEnd.
From what I can see, these are not defined anywhere in your code - hence the undefined warning.
What you need to do is start off by switching out $positionStart and $positionEnd for $start and $end here:
<?php
if($position >= $positionStart && $position <= $positionEnd){
Make that change and see how it goes
Bethany Norton
4,818 PointsThis seems to have fixed it, thank you. The example had them separated as per my code above, and then when I was changing them to match before (as that was my initial hunch) it wasn't working, but I think I was missing a separate piece of code as well, which I've since fixed.
Thanks again!