Viewing an image from a related table

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

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/

Claire:  Right now, one table just lists a set of Air Force ranks and the image (RANKS).  The main table has the personnel information, including name, position, email, rank.  The rank field in this table is a list linked to the RANKS table.  The only field it will let me display is the text rank field, not the uploaded image.

Does that help?

Ragi

--
northernpenguin
Northern Penguin Technologies

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

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/