Category Pagination

7 posts by 2 authors in: Forums > CMS Builder
Last Post: June 22, 2011   (RSS)

By Perchpole - June 20, 2011

Hello, All

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

Hi Perchpole,

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

Hi, Jason -

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

Hi Perch,

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: [Perchpole] Category Pagination

By Jason - June 22, 2011

Hi Perch,

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

Brilliant!

That's just about perfect.

Thanks very much for your help!

:0)

Perch