Display linked records

5 posts by 2 authors in: Forums > CMS Builder
Last Post: May 27, 2014   (RSS)

By gregThomas - May 19, 2014

Hi Tim,

Unfortunately the getRecords left join option only allows you to make one call to a particular section, plus it doesn't return _link or upload data. In this case the best way to do it would be to make 3 separate getRecord calls for each business.

I can give you some example code if you require, is this for a list page or for a detail page?

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By Toledoh - May 19, 2014

Thanks Greg - it's for a list page...  

Cheers,

Tim (toledoh.com.au)

By gregThomas - May 27, 2014

Hi Tim, 

Something like this should work, in this example I've used blog posts and blog categories, but the you should be able to swap it so that it uses your adverts and businesses instead. 

First you need to retrieve all of the blog categories (businesses) and sort them by num value:

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

  $blog_categories = array_groupBy($blog_categories, 'num', false);

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

Now when you loop through the blog records (adverts). In this example I get the appropriate blog category record (business) for each blog record from it's category (business_1,business_2,business_3) field, and set it to the blogCategory variable.


  foreach($blogs as $blog){

    //Get the appropriate blog category for blog records category field
    $blogCategory = @$blog_categories[$blog['category']];

    //Dislay an image from the associated blog category.
    if($image = @$blogCategory['image'][0] ){
      echo "<img src='{$image['urlPath']}' alt='Yes' />\r\n";
    }
  }

So with your example you would need to do something like this:


  foreach($adverts as $advert){

    //Get the appropriate businwaa records for each advert
    $business1 = @$businesses[$advert['business_1']];
    $business2 = @$businesses[$advert['business_2']];
    $business3 = @$businesses[$advert['business_3']];


    //display data here....

  }

Let me know if you have any questions.

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By Toledoh - May 27, 2014

Got it! Thanks mate

Cheers,

Tim (toledoh.com.au)