Viewing an image from a related table

6 posts by 2 authors in: Forums > CMS Builder
Last Post: February 17, 2015   (RSS)

By northernpenguin - February 15, 2015 - edited: February 15, 2015

I have 2 multi-value tables.  One lists the personnel, while the other lists the positions (2nd field is an image).  How do I display the personnel information for each person plus the position image?

Right now, I'm stumped!

--
northernpenguin
Northern Penguin Technologies

"Any sufficiently advanced technology
is indistinguishable from magic."
........Arthur C. Clarke

By claire - February 16, 2015

Hey there - it depends on what the relationship between the two tables is. Can you give me any more information?

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

Claire Ryan
interactivetools.com

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

By claire - February 16, 2015

Yes, it does! What you'll have to do is use a second getRecords function to get the RANKS table, and then create a new array of ranks with the num of each rank record as the index to the record. Then if you're outputting the personnel records, you can use the indexed array to get the uploaded photo.

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

Claire Ryan
interactivetools.com

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

By northernpenguin - February 16, 2015 - edited: February 16, 2015

Claire:  I got everything except the "..create a new array of ranks....".  Not sure how to do that.

Ragi

P.S.  Is this what you mean?

<?php
  // load records from 'staff'
  list($staffRecords, $staffMetaData) = getRecords(array(
    'tableName'   => 'staff',
    'loadUploads' => true,
    'allowSearch' => false,
    'debugSql'    => false,      // optional, display SQL query, defaults to no
  ));

  // load records from 'ranks'
  list($ranksRecords, $ranksMetaData) = getRecords(array(
    'tableName'   => 'ranks',
    'loadUploads' => true,
    'allowSearch' => false,
  ));
  
  $rankStructure = getRecords(array(
    'tableName'   => 'ranks',
    'orderBy'   => 'num',
));
?>

--
northernpenguin
Northern Penguin Technologies

"Any sufficiently advanced technology
is indistinguishable from magic."
........Arthur C. Clarke

By claire - February 17, 2015

No no, there's a particular way of setting up an array so that each record is easily accessible. So you start with this:

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

The $ranksRecords come out as an array indexed in the order that they're picked from the database. So it looks like this:

array(
  0 => record 1,
  1 => record 2,
  2 => record 3...

etc. What you want to do is re-sort the array so that it's indexed to the num of the ranks records, and you do it like this:

$sortedRanks = array();
foreach ($ranksRecords as $rank) {
  $sortedRanks[$rank['num']] = $rank;
}

Now when you're looping through the $staffRecords, you can access the right rank record to display the picture by selecting the right index in the $sortedRanks, like this:

$rank = $sortedRanks[$record['rankNum']];

Change 'rankNum' to the right variable name, of course. This is a convenient piece of logic to use when you have a set of records that can be applied to multiple others.

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

Claire Ryan
interactivetools.com

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