Pull in 1 item from two sections on same page

5 posts by 2 authors in: Forums > CMS Builder
Last Post: September 23, 2013   (RSS)

Hi all ...

I have two sections in CMS builder both containing uploaded image files (home images & ipad home images).

I am trying to load a random one from each section into the same page ... is this possible?

Here's the step 1 load records code I've been trying to get working:

<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php
/* STEP 1: LOAD RECORDS - Copy this PHP code block near the TOP of your page */

// load viewer library
$libraryPath = 'cmsAdmin/lib/viewer_functions.php';
$dirsToCheck = array('/home/sites/XXXXXXXXXXXX/public_html/','','../','../../','../../../');
foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }

// load records from 'home_images'
list($home_imagesRecords, $home_imagesMetaData) = getRecords(array(
'tableName' => 'home_images',
'limit' => '1',
'orderBy' => 'RAND()',
'loadUploads' => true,
'allowSearch' => false,
));

// load records from 'ipad_home_images'
list($ipad_home_imagesRecords, $ipad_home_imagesMetaData) = getRecords(array(
'tableName' => 'ipad_home_images',
'limit' => '1',
'orderBy' => 'RAND()',
'loadUploads' => true,
'allowSearch' => false,
));

?>

By Jason - September 20, 2013

Hi,

Are your "home_images" and "ipad_home_images" sections single, or multi-record sections?  the "orderBy" => 'RAND()' option only randomizes records from multi-record sections, and doesn't randomize the order of the images inside an upload field.  If you want to get a single random image from either of these, you can do this using the PHP shuffle() function to randomize the upload array.  In this example, it assumes that you have at least 1 upload per record and that the upload field in both sections is called "images"

<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php
/* STEP 1: LOAD RECORDS - Copy this PHP code block near the TOP of your page */

// load viewer library
$libraryPath = 'cmsAdmin/lib/viewer_functions.php';
$dirsToCheck = array('/home/sites/XXXXXXXXXXXX/public_html/','','../','../../','../../../');
foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }

// load records from 'home_images'
list($home_imagesRecords, $home_imagesMetaData) = getRecords(array(
  'tableName'   => 'home_images',
  'limit'       => '1',
  'orderBy'     => 'RAND()',
  'loadUploads' => true,
  'allowSearch' => false,
));

$homeImagesRecord = $home_imagesRecords[0];

// load records from 'ipad_home_images'
list($ipad_home_imagesRecords, $ipad_home_imagesMetaData) = getRecords(array(
  'tableName'   => 'ipad_home_images',
  'limit'       => '1',
  'orderBy'     => 'RAND()',
  'loadUploads' => true,
  'allowSearch' => false,
));

$ipadHomeImagesRecord = $ipad_home_imagesRecords[0];

// radomize the image arrays
shuffle($homeImagesRecord['images']);
shuffle($ipadHomeImagesRecord['images']);

// get the first image from the randomized arrays
$homeImage = $homeImageRecord['images'][0];
$ipadImage = $ipadHomeImagesRecord['images'][0];

?>

Give this a try and let me know if you run into any issues.

Thanks!

---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Hi Jason ... thanks for the feedback.

Both sections are multi-record sections with a number of single uploads.

I think the code you suggested was based on a single record sections with numbers uploads within the upload field?

Hi Jason ...

Yep sorted.

Original code was fine ... I'd just placed the placeholder tags slightly out of the right place. I have the ipad specific images loading into a media query specifically for portrait tablet users.