6 posts by 2 authors in: Forums > CMS Builder
Last Post: October 3, 2012   (RSS)

By InHouse - August 27, 2012

Hi,

We have a list view on a page which relies upon the URL parameters to show a single record on page load. We'd also like to have Next & Prev buttons to allow users to click through the list or related page categories.

We need to find a way to make these Next & Prev buttons use the record title field in the URL paramater rather than the normal '....php&page=2' format. So we'd like to use a table of categories such as:

RED
BLUE
GREEN

... so that when on "mypage.php?cat=BLUE", then the Prev button would link to "mypage.php?cat=RED" and the Next button to "mypage.php?cat=GREEN". I think this is just an adjustment to the normal use of the MetaData recordset but I'm not familiar enough with it to know what to do.

Many thanks,
J.

Re: [InHouse] Using MetaData Recordset

By Jason - August 28, 2012

Hi,

From the example you describe, you'll need to create your own next and previous link variables yourself.

Take a look at this example:

<?php
$categoriesArray = array('RED', 'BLUE', 'GREEN');

$currentCat = @$_REQUEST['cat'];
$nextLink = "";
$prevLink = "";


foreach ($categoriesArray as $index => $category) {

if ($category == $currentCat) {

if ($index == count($categoriesArray) - 1) {
$nextLink = "?cat=".$categoriesArray[0];
}
else {
$nextLink = "?cat=".$categoriesArray[$index + 1];
}

if ($index == 0) {
$prevLink = "?cat=".$categoriesArray[count($categoriesArray) - 1];
}
else {
$prevLink = "?cat=".$categoriesArray[$index - 1];
}

break;
}

}

?>


This example uses a hardcoded array of category options, but it could just as easily use a dynamic list from the database. This example is also circular, so when you reach the end of the list (Green), the next link will go the the start of the list (red). When you're at the start of the list, the previous link will go to the last option.

At the end of this code (assuming $_REQUEST['cat'] has a value that is in our array of options), you'll have $nextLink and $prevLink that you can output where ever you need.

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] Using MetaData Recordset

By InHouse - August 28, 2012

Many thanks Jason! I'll take a poke at this later today and report back on how I get on.
Cheers,
J.

Re: [Jason] Using MetaData Recordset

By InHouse - October 3, 2012

Hi Jason,

Almost there! The only issue with the above is that the array index is not being inremented or decremented in the nextLink and prevLink variables.

A few tests show the current content of $category is a complete 'array' rather than the current position in the category list.

It's probably a misunderstanding at my end once I brought the dynamic list in from the database.

Sample:

<?php
list($categoriesArray, $categoriesArrayMetaData) = getRecords(array(
'tableName' => 'collections',
'loadUploads' => '0',
'allowSearch' => '0',
'where' => '`active` != 0',
));

$currentCat = @$_REQUEST['collections'];
$nextLink = "";
$prevLink = "";

foreach ($categoriesArray as $index => $category) {

if ($category == $currentCat) {

if ($index == count($categoriesArray) - 1) {
$nextLink = "?cat=".$categoriesArray[0];
}
else {
$nextLink = "?cat=".$categoriesArray[$index + 1];
}

if ($index == 0) {
$prevLink = "?cat=".$categoriesArray[count($categoriesArray) - 1];
}
else {
$prevLink = "?cat=".$categoriesArray[$index - 1];
}

break;
}

}
echo "Current Cat: ".$currentCat.'<br />'; // Echos: Barcelona (correct)
echo "Category: ". $category . '<br />'; // Echos: array
?>

<br />
<a href="<?php echo $prevLink; ?>">Prev</a> | <a href="<?php echo $nextLink; ?>">Next</a> <br />

Re: [InHouse] Using MetaData Recordset

By Jason - October 3, 2012

Hi,

It looks like the issue is in your foreach loop. In your code, $category is an record in the record set. What we need is to just have an array of values.

For example, if you're using a field called "title" in your collections section, you can set things up like this:

<?php
list($categoriesArray, $categoriesArrayMetaData) = getRecords(array(
'tableName' => 'collections',
'loadUploads' => '0',
'allowSearch' => '0',
'where' => '`active` != 0',
));


$categories = array_pluck($categoriesArray, 'title');
$currentCat = @$_REQUEST['collections'];
$nextLink = "";
$prevLink = "";

foreach ($categories as $index => $category) {

if ($category == $currentCat) {

if ($index == count($categoriesArray) - 1) {
$nextLink = "?cat=".$categoriesArray[0];
}
else {
$nextLink = "?cat=".$categoriesArray[$index + 1];
}

if ($index == 0) {
$prevLink = "?cat=".$categoriesArray[count($categoriesArray) - 1];
}
else {
$prevLink = "?cat=".$categoriesArray[$index - 1];
}

break;
}

}
echo "Current Cat: ".$currentCat.'<br />'; // Echos: Barcelona (correct)
echo "Category: ". $category . '<br />'; // Echos: array
?>

<br />
<a href="<?php echo $prevLink; ?>">Prev</a> | <a href="<?php echo $nextLink; ?>">Next</a> <br />


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/