display associated record field for each record on list page

9 posts by 4 authors in: Forums > CMS Builder
Last Post: January 27, 2021   (RSS)

By Deborah - December 4, 2020

Hi, Everyone. I've searched the forum extensively, but haven't found anything for this scenario and hope someone can guide me to a solution.

----------------------------------
TABLES:

// list of moorings
list($mooringsRecords, $mooringsMetaData) = getRecords(array(
'tableName' => 'moorings',
'allowSearch' => false,
));

// list of inspections records
list($inspectionsRecords, $inspectionsMetaData) = getRecords(array(
'tableName' => 'inspections',
'allowSearch' => false,
));

----------------------------------
DESIRED HTML OUTPUT on the Moorings List Viewer page:

<?php foreach ($mooringsRecords as $record): ?>
(the usual records for $mooringsRecords)

+ most recent ['inspection_date'] field from $inspectionsRecords that matches this moorings record number

<?php endforeach ?>

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

The $inspections ["mooring_number"] field editor setup:

Field Options - Display as pulldown
Get options from database (advanced)
Section Tablename = moorings
Field for option values = num
Field for option labels = num

Each mooring can have multiple ["inspection_date"]s, but we only want to display the most recent date for each mooring.

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

Hope I explained that well enough. Thanks for any help you can give!


~ Deborah

By Toledoh - December 4, 2020

Hi Deborah.

Someone else will be able to give more expert / elegant guidance, but maybe something along the lines of this inserted within the loop?

<?php
	
  // load record from 'inspections'
  list($inspectionsRecords, $inspectionsMetaData) = getRecords(array(
    'tableName'   => 'inspections',
    'where'       => "`num` = ".$record['num'],
    'loadUploads' => true,
    'allowSearch' => false,
    'limit'       => '1',
  ));
  $inspectionsRecord = @$inspectionsRecords[0]; // get first record
  if (!$inspectionsRecord) { dieWith404("Record not found!"); } // show error message if no record found
  
  echo htmlencode($inspectionsRecord['title']);

?>
Cheers,

Tim (toledoh.com.au)

By Toledoh - December 5, 2020

Great.  

My only concern is that if you have a lot of moorings, and you do a getRocords for each, it might ge quite a load.  Maybe theres a leftJoin or a mysqlselect kind of version that would be better?

Cheers,

Tim (toledoh.com.au)

By Deborah - December 6, 2020

Tim, that thought occurred to me, too.

I'm wondering if there is a solution such as you suggested. Maybe someone will have that answer.

~ Deborah

Hi Deborah,

Don't know if it will help, but take a look at this post.

https://www.interactivetools.com/forum/forum-posts.php?postNum=2232753

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By Deborah - December 7, 2020

Hi, Jerry.

That was an interesting post - thank you!

With my limited PHP skills, I wasn't able to adjust the code to work for my example that does not need to reference uploads. Good to know uploads can be included, though.

Thanks again.
Deborah 

Good morning Deborah,

You're welcome.

I wish I could help more, but my skills in this area are limited as well.

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By Jenna - January 27, 2021

Hi all,

Just taking a look at this old post and noticed that there could still be unanswered questions. Do you still require assistance?

We'd be happy to help, please let us know.

Jenna Cooke - PHP Programmer
interactivetools.com