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 trialJonathan Grieve
Treehouse Moderator 91,253 Points[SOLVED] Strange behaviour with catalog.php
I had it working with catalog.php where the search pagination carried on as it should retriving the rest of the items. Now all it does is as "thethe" in the query string as a non matching search.
I'll have a look at this again later but I wondered if someone might have an answer as to why?
http://www.jonniegrieve.co.uk/jg-lab.co.uk/medlib-sql/catalog.php?s=the%20the+&pg=2
It's do do with the limit results and pagination sections... thanks
<?php
include("inc/functions.php");
$pageTitle = "Full Catalog";
$section = null;
$search = null;
/*Start Pagination*/
$items_per_page = 8;
if (isset($_GET["cat"])) {
if ($_GET["cat"] == "books") {
$pageTitle = "Books";
$section = "books";
} else if ($_GET["cat"] == "movies") {
$pageTitle = "Movies";
$section = "movies";
} else if ($_GET["cat"] == "music") {
$pageTitle = "Music";
$section = "music";
}
}
if(isset($_GET["s"])) {
$search = filter_input(INPUT_GET, "s", FILTER_SANITIZE_STRING);
}
if(isset($_GET["pg"])) {
$current_page = filter_input(INPUT_GET, "pg", FILTER_SANITIZE_NUMBER_INT);
}
if(empty($current_page)) {
$current_page = 1;
}
$total_items = get_catalog_count($section, $search);
$total_pages = 1;
$offset = 0;
if($total_items > 0) {
$total_pages = ceil($total_items / $items_per_page);
//limit results in redirect
$limit_results = "";
/*if (!empty($section)) {
$limit_results = "cat=" . $section . "&";
}*/
// redirect too-large page numbers to the last page
if ($current_page > $total_pages) {
header("location:catalog.php?"
. $limit_results
. "pg=".$total_pages);
}
// redirect too-small page numbers to the first page
if ($current_page < 1) {
header("location:catalog.php?"
. $limit_results
. "pg=1");
}
//determine the offset (number of items to skip) for the current page
//for example: on page 3 with 8 item per page, the offset would be 16
$offset = ($current_page - 1) * $items_per_page;
$pagination = "<div class=\"pagination\">";
$pagination .= "Pages: ";
for ($i = 1;$i <= $total_pages;$i++) {
if ($i == $current_page) {
$pagination .= " <span>$i</span>";
} else {
$pagination .= " <a href='catalog.php?";
if (!empty($search)) {
//paginate across all the items in search term
$pagination .= "s=" . $search .urlencode(htmlspecialchars($search))."&";
} else/**/ if (!empty($section)) {
$pagination .= "cat=".$section."&";
}
$pagination .= "pg=$i'>$i</a>";
}
}
$pagination .= "</div>";
}
$catalog = full_catalog_array(); //include catalog array
if(!empty($search)) {
//set search equal to search_catalog_array(items per page, search, offset)
$catalog = search_catalog_array($search, $items_per_page, $offset);
} else if(empty($section)) {
$catalog = full_catalog_array($items_per_page, $offset);
} else {
$catalog = category_catalog_array($section, $items_per_page, $offset);
}
include("inc/header.php"); ?>
<div class="section catalog page">
<div class="wrapper">
<h1><?php
if( $search != null) {
echo "Search Results for \""
. htmlspecialchars($search)
."\"";
} else {
if($section != null) {
echo "<a href=\"catalog.php\">Full Catalog</a> > ";
}
echo $pageTitle;
}?> </h1>
<?php
if ($total_items < 1) {
echo "<p>Sorry, No items were found matching that search term.</p>";
echo "<p>Search Again or "
. "<a href=\"catalog.php\">Browse the Full Catalog</a></p>";
} else {
echo $pagination; ?>
<ul class="items">
<?php
foreach ($catalog as $item) {
echo get_item_html($item);
}
?>
</ul>
<?php echo $pagination;
} ?>
</div>
</div>
<?php include("inc/footer.php"); ?>
1 Answer
Jonathan Grieve
Treehouse Moderator 91,253 PointsSo yes, the problem was somewhere on my if statement
<?php
for ($i = 1;$i <= $total_pages;$i++) {
if ($i == $current_page) {
$pagination .= " <span>$i</span>";
} else {
$pagination .= " <a href='catalog.php?";
if (!empty($search)) {
$pagination .= "s=" . urlencode(htmlspecialchars($search)) . "&";
} else if (!empty($section)) {
$pagination .= "cat=".$section."&";
}
$pagination .= "pg=$i'>$i</a>";
}
}
?>
It seems I actually added the $search variable twice which meant it was showing up twice on the screen. So this was causing the non matching search.
I also had a problem with the limit results part as I wasn't returning the search s=
but the full catalog cat=
. Here's the correct code.
<?php
if (!empty($search)) {
$limit_results = "s=" . urlencode(htmlspecialchars($search)) . "&";
}
?>