leftJoin (with images and multiple "joins" and multi-selects)

5 posts by 2 authors in: Forums > CMS Builder
Last Post: February 16, 2016   (RSS)

By Toledoh - February 14, 2016

Hi Guys.

A few questions surrounding leftJoin....

Images

I've got a section "news" which has a field "author", which is a list of records from the "authors" table.

Normally, I would use the following:

// load records from 'news'
list($newsRecords, $newsMetaData) = getRecords(array(
'tableName' => 'news',
'loadUploads' => true,
'allowSearch' => false,
'leftJoin'    => array('author' => 'authors'),

));

 - - - 

<?php echo htmlencode($newsRecords['authors.title']) ?>

This is great for most fields, but how do I go about getting the authors upload field. I was hoping that the following would work but it doesn't.

<?php foreach ($record['authors.upload'] as $index => $upload): ?>

<?php echo htmlencode($upload['urlPath']) ?>

<?php endforeach ?>

Multiple

Is it OK to do the following to get multiple related tables?

'leftJoin'    => array('author' => 'authors'),
'leftJoin'    => array('photographer' => 'photographers')

Multi-selects

And what about multi-selects?  So if "Photographer" was a multi-select, how could I select 2 photographers associated to a single newsRecord, can I still use leftJoin?

Cheers,

Tim (toledoh.com.au)

By gregThomas - February 15, 2016

Hey Tim, 

Getting Left joined Images

The getRecords left join system doesn't get upload fields for the left joined records, but you can manually fetch the uploads using the following system:

  
  // load viewer library
  $libraryPath = 'cmsb/lib/viewer_functions.php';
  $dirsToCheck = array('C:/wamp/www/','','../','../../','../../../');
  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 'blog'
  list($blogRecords, $blogMetaData) = getRecords(array(
    'tableName'   => 'blog',
    'loadUploads' => true,
    'allowSearch' => false,
    'leftJoin'    => array('blog_categories' => 'category')
  ));

  foreach($blogRecords as $key => $blog){
    $where   = "`tableName` = 'blog_categories' AND `fieldName` = 'image' AND `recordNum` = '".intval($blog['blog_categories.num'])."'";
    $uploads = mysql_select('uploads', $where);
    $blogRecords[$key]['blog_categories.image'] = $uploads;
  }

So my sections are set up so that each blog post has a category which comes from the blog_categories table, and each blog_categories has an image field called image. To add the image upload field for each blog post you have to loop through the records and use mysql_select to get the appropriate images from the upload table. Note: These fields won't have the upload directory prefix on them, so when displaying the records you'll need to add it using the $SETTINGS['uploadUrl'] variable. 

Getting Multiple Left Joins

You can create one leftJoin array item, and add multiple sections it like this:

'leftJoin'    => array('author' => 'authors', 'photographer' => 'photographers'),

Left Joining on a multi-select

Mutli-selects are more difficult, as we're returning more than one record for each item in the getRecords array. Here is how you could return those items for each item under the field category:records. In this example each blog post can have multiple categories. 

  // load records from 'blog'
  list($blogRecords, $blogMetaData) = getRecords(array(
    'tableName'   => 'blog',
    'loadUploads' => true,
    'allowSearch' => false,
  ));

  foreach($blogRecords as $key => $blog){
    $categoryNums = implode(", ", $blog['category:values']);
    if($categoryNums){
      $blogRecords[$key]['category:records'] = mysql_select('blog_categories', "`num` IN ($categoryNums)");
    }
  }

Let me know if you have any questions.

Cheers!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By gregThomas - February 15, 2016

Hey Tim, 

Getting Left joined Images

The getRecords left join system doesn't get upload fields for the left joined records, but you can manually fetch the uploads using the following system:

  
  // load viewer library
  $libraryPath = 'cmsb/lib/viewer_functions.php';
  $dirsToCheck = array('C:/wamp/www/','','../','../../','../../../');
  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 'blog'
  list($blogRecords, $blogMetaData) = getRecords(array(
    'tableName'   => 'blog',
    'loadUploads' => true,
    'allowSearch' => false,
    'leftJoin'    => array('blog_categories' => 'category')
  ));

  foreach($blogRecords as $key => $blog){
    $where   = "`tableName` = 'blog_categories' AND `fieldName` = 'image' AND `recordNum` = '".intval($blog['blog_categories.num'])."'";
    $uploads = mysql_select('uploads', $where);
    $blogRecords[$key]['blog_categories.image'] = $uploads;
  }

So my sections are set up so that each blog post has a category which comes from the blog_categories table, and each blog_categories has an image field called image. To add the image upload field for each blog post you have to loop through the records and use mysql_select to get the appropriate images from the upload table. Note: These fields won't have the upload directory prefix on them, so when displaying the records you'll need to add it using the $SETTINGS['uploadUrl'] variable. 

Getting Multiple Left Joins

You can create one leftJoin array item, and add multiple sections it like this:

'leftJoin'    => array('author' => 'authors', 'photographer' => 'photographers'),

Left Joining on a multi-select

Mutli-selects are more difficult, as we're returning more than one record for each item in the getRecords array. Here is how you could return those items for each item under the field category:records. In this example each blog post can have multiple categories. 

  // load records from 'blog'
  list($blogRecords, $blogMetaData) = getRecords(array(
    'tableName'   => 'blog',
    'loadUploads' => true,
    'allowSearch' => false,
  ));

  foreach($blogRecords as $key => $blog){
    $categoryNums = implode(", ", $blog['category:values']);
    if($categoryNums){
      $blogRecords[$key]['category:records'] = mysql_select('blog_categories', "`num` IN ($categoryNums)");
    }
  }

Let me know if you have any questions.

Cheers!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By Toledoh - February 15, 2016

This is great Greg.

With displaying the images, what do I use as the for-each loop?

<?php foreach ($blogRecords as $record): ?>

<h1><?php echo htmlencode($record['title']) ?></h1>

<?php foreach ($record['upload'] as $index => $upload): ?>
  <img src="<?php echo $SETTINGS['uploadUrl'] ?>/<?php echo htmlencode($upload['urlPath']) ?>" alt="<?php echo htmlencode($record['title']) ?>" class="width"/>
<?php endforeach ?>


<?php endforeach ?>

Cheers,

Tim (toledoh.com.au)