Filter records from a variable - related articles

2 posts by 2 authors in: Forums > CMS Builder
Last Post: September 18, 2014   (RSS)

By Jesus - September 10, 2014

Hi,

Will try to explain this the easiest possible way :)

Here's what I'm trying to do.

I've a honeymoon table organized by categories, and I already sort my records where I can choose from. Once I select the proper honeymoon I'm displaying the Honeymoon details. Until here all fine.

Each honeymoon has a category field and now, I'm trying to get 4 related (same category) records to display at the end of my page. I don't know how can I create the filter using the category field I've on my details page.

So far here's what I've on my header section:

  // load viewer library
  $libraryPath = 'cms/lib/viewer_functions.php';
  $dirsToCheck = array('/chroot/home/paquetes/paqueteslunamieleros.com.mx/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 record from 'paquetes_lunamieleros'
  list($paquetes_lunamielerosRecords, $paquetes_lunamielerosMetaData) = getRecords(array(
    'tableName'   => 'paquetes_lunamieleros',
    'where'       => whereRecordNumberInUrl(0),
    'loadUploads' => true,
    'allowSearch' => false,
    'limit'       => '1',
  ));
  $paquetes_lunamielerosRecord = @$paquetes_lunamielerosRecords[0]; // get first record
  if (!$paquetes_lunamielerosRecord) { dieWith404("No hay registros!"); } // show error message if no record found
  
  // load records from 'paquetes_lunamieleros'
  list($paquetes_lunamielerosALLRecords, $paquetes_lunamielerosALLMetaData) = getRecords(array(
    'tableName'   => 'paquetes_lunamieleros',
    'loadUploads' => true,
    'allowSearch' => true,
    'perPage'     => '4', 
  ));

Here. $paquetes_lunamielerosRecords works to grab the info I use on the record I'm displaying and $paquetes_lunamielerosALLRecords will work to be able to display the related records.

I've on the details record I'm displaying the category field so I don't know how can I display up tp 4 related records (same category) using the proper category value on my current page (for my current record).

Thanks in advance for helping me out.

Jesus

By claire - September 18, 2014

Hey there

Best thing to do is use some SQL trickery to get four random records.

So you've got $paquetes_lunamielerosRecord, which contains the category you want to get four related records from. I'm not sure how your schema is set up, but here's how I'd get those four records:

// load records from 'paquetes_lunamieleros'
  list($paquetes_lunamielerosALLRecords, $paquetes_lunamielerosALLMetaData) = getRecords(array(
    'tableName'   => 'paquetes_lunamieleros',
    'loadUploads' => true,
    'allowSearch' => true,
    'where'       => 'category = '.$paquetes_lunamielerosRecord['category'],
    'limit'       => '4',
    'orderBy'     => 'RAND()'
  ));

The RAND() option should choose four random records from the right category.

Does this help?

--------------------

Claire Ryan
interactivetools.com

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