Category Pagination
7 posts by 2 authors in: Forums > CMS Builder
Last Post: June 22, 2011 (RSS)
By Perchpole - June 20, 2011
I'm trying to devise a method of pagination between category pages. I want the user to be able to click (forward and back) through a range of categories, all of which would be decendants of a common parent category.
The key (from my limited knowledge) is determining which is the next (or previous) category in the sequence. I imagine this might involve the use of the next() and prev() array funtions...
Any pointers would be most welcome.
:0)
Perchpole
Re: [Perchpole] Category Pagination
By Jason - June 20, 2011
Here is one approach you can take.
First, if you know the parent category number, you can use the "rootCategoryNum" option in getCategories to only get descendants of that category like this:
list($selectedCategories, ) = getCategories (array(
'tableName' => 'categories',
'rootCategoryNum' => $parentCategoryNum,
));
You can then manually add "page" to your next and previous links.
You can control how many elements will appear on a given page like this:
$perPage = 5;
$currentPage = intval(@$_REQUEST['page']);
if (!$currentPage || $currentPage < 0) {
$firstElement = 1;
}
$firstElement = ($currentPage -1 ) * $perPage;
$maxRecords = count ($selectedCategories);
Finally, instead of a foreach loops, we can use a for loop to get 1 element at a time from the $selectedCategories array like this:
$displayCount = 0;
for($index = $firstElement; $index < $maxRecords; $index ++) {
$displayCount ++;
if ($displayCount > $perPage) {
break;
}
$currentCategory = $selectedCategories[$index];
//display information for $currentCategory normally
}
Hope this helps get you started.
Jason Sauchuk - Project Manager
interactivetools.com
Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/
Re: [Jason] Category Pagination
By Perchpole - June 20, 2011
Thanks for a speedy response.
:0)
I can see where you're going with this but would it be possible to explore a more detailed solution? I say this simply because of the way my page links are defined site-wide.
Currently the links observe the following pattern:
www.sitename.co.uk/index.php?cat=1 (for a category page)
www.sitename.co.uk/index.php?cat=1&pag=2 (for a record page)
For the sake of consistency I would like to maintain this pattern.
With this in mind I would like to find a solution which appends a base url with the relevant $category['num'] to form the links.
Is this possible?
:0)
Perch
Re: [Perchpole] Category Pagination
By Jason - June 21, 2011
I'm not sure exactly where you want to create those links. The best thing to do here would be to develop your page as far as you can without the links, then attach your .php page and let me know which part is missing.
I should be able to give you a more concise example at that point.
Hope this helps
Jason Sauchuk - Project Manager
interactivetools.com
Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/
Re: [Jason] Category Pagination
By Perchpole - June 21, 2011
This solution is required for any particular page set-up. It's something I want to be able to nail so I can use it whenever the need arises. Let me try to explain it a little better...
Lets say there is a category called Articles which (for the sake of argument) is category number 5. To reach that category on the site you'd go to...
www.mysite.co.uk/index.php?cat=5
The Articles category has 4 sub-categories:
www.mysite.co.uk/index.php?cat=7
www.mysite.co.uk/index.php?cat=11
www.mysite.co.uk/index.php?cat=13
www.mysite.co.uk/index.php?cat=21
I'm trying to create a pagination system that would allow users to click from one sub-category to the next. There would be links at the foot of each category page (just like normal pagination links) like << prev and next >>.
So, in the example above, if I was reading category page number 11, there would be links taking me "next >>" to category page 13 or "<< prev" to cat page 7.
Does that help?
:0)
Perch
Re: [Perchpole] Category Pagination
By Jason - June 22, 2011
Okay, I think I see what you're going for. So on a page, you're displaying all of a category's sub-categories, and then using pagination to use 1 of those sub categories at a time. Is that right?
If so, we would keep keep the parent category in the URL and then swap out the category variable with each "page". Also, if we change the variable from cat to category, we can take advantage of the "selectedCategory" feature.
So, for example, our first page would be this:
www.mysite.co.uk/index.php?parentCategory=5
We would then select our categories like this:
list ($subCategories, $selectedCategory) = getCategories(array(
'tableName' => 'categories',
'rootCategoryNum' => intval(@$_REQUEST['parentCategory']),
));;
You can then determine your next/previous links like this:
$categoryNumArray = array_combine(array_keys($subCategories), array_pluck($subCategories, 'num'));
$nextLink = "";
$prevLink = "";
if (!$selectedCategory) {
if (array_key_exists(0, $categoryNumArray)) {
$nextLink = "?parentCategory=".@$_REQUEST['parentCategory']."&category=".$categoryNumArray[0];
}
}
else {
$currentKey = 0;
foreach ($categoryNumArray as $num) {
if ($num == $selectedCategory['num']) {
break;
}
$currentKey ++;
}
if (array_key_exists($currentKey - 1, $categoryNumArray)) {
$prevLink = "?parentCategory=".@$_REQUEST['parentCategory']."&category=".$categoryNumArray[$currentKey - 1];
}
if (array_key_exists($currentKey + 1, $categoryNumArray)) {
$nextLink = "?parentCategory=".@$_REQUEST['parentCategory']."&category=".$categoryNumArray[$currentKey + 1];
}
}
You can output anything you want from $selectedCategory as the current sub category.
Finally, you can output your next/prev links like this:
<?php if ($nextLink): ?>
<a href = "<?php echo $nextLink;?>">Next</a><br/>
<?php endif ?>
<?php if ($prevLink): ?>
<a href = "<?php echo $prevLink;?>">Prev</a><br/>
<?php endif ?>
Hope this helps get you started.
Jason Sauchuk - Project Manager
interactivetools.com
Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/
Re: [Jason] Category Pagination
By Perchpole - June 22, 2011
That's just about perfect.
Thanks very much for your help!
:0)
Perch