<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php
  /* STEP 1: LOAD RECORDS - Copy this PHP code block near the TOP of your page */
  require_once "C:/wamp/www/cmsbuilder_1_34_build1/cmsAdmin/lib/viewer_functions.php";

  // fetch selected tile_images record
  list($tile_imagesRecords, $tile_imagesMetaData) = getRecords(array(
    'tableName'   => 'tile_images',
    'where'       => whereRecordNumberInUrl(1),
    'limit'       => '1',
  ));
  $tile_imagesRecord = @$tile_imagesRecords[0]; // get first record
  
  // show error message if no matching record is found
  if (!$tile_imagesRecord) {
    print "Record not found!";
    exit;
  }
  
  // compile a list of tile nums to fetch
  $tile_fields = array('floor_tile', 'floor_grout', 'shower_wall');
  global $tile_imagesRecord;
  function collectRecordValues($field) {
    global $tile_imagesRecord;
    return $tile_imagesRecord[$field] ? $tile_imagesRecord[$field] : 0;
  }
  $tile_nums = join(', ', array_map('collectRecordValues', $tile_fields));
  
  // fetch referenced tiles and index by num
  list($tileRecords, $tileMetaData) = getRecords(array(
    'tableName'   => 'tile',
    'where'       => "num IN ($tile_nums)",
  ));
  function collectNum($record) { return $record['num']; } 
  $tileRecordsByNum = array_combine(array_map('collectNum', $tileRecords), $tileRecords); 
  
  // inject tile records into tile_images record's fields
  foreach ( $tile_fields as $field ) {
    $tile_imagesRecord[$field] =& $tileRecordsByNum[ $tile_imagesRecord[$field] ];
  }

?>

<h1>tile_images - Detail Page Viewer</h1>
  Record Number: <?php echo $tile_imagesRecord['num'] ?><br/>
  Title: <?php echo $tile_imagesRecord['title'] ?><br/>
  <?php if ($tile_imagesRecord['floor_tile']): ?>
    Floor Tile: <?php echo $tile_imagesRecord['floor_tile']['tile_sku'] ?><br/>
  <?php endif; ?>
  <?php if ($tile_imagesRecord['floor_grout']): ?>
    Floor Grout: <?php echo $tile_imagesRecord['floor_grout']['tile_sku'] ?><br/>
  <?php endif; ?>
  <?php if ($tile_imagesRecord['shower_wall']): ?>
    Shower Wall: <?php echo $tile_imagesRecord['shower_wall']['tile_sku'] ?><br/>
  <?php endif; ?>
  <hr/>
<?php if (!$tile_imagesRecord): ?>
  No record found!<br/><br/>
<?php endif ?>
