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

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/

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.