Using MetaData Recordset

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: [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/

Re: [Jason] Using MetaData Recordset

By InHouse - October 3, 2012

Thanks Jason. That puts the right array element into $currentCat and $category but now the next & prev links get the original array dumped to them. For those keeping score at home, the full solution is below:

$categories = array_pluck($categoriesArray, 'collection');
$currentCat = @$_REQUEST['collections'];
$nextLink = "";
$prevLink = "";
foreach ($categories as $index => $category) {

if ($category == $currentCat) {

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

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

break;
}

}


Works as billed. Thank you Jason. You're the bestest.

J.