<?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_33_build1/cmsAdmin/lib/viewer_functions.php";

  list($categoryRecords, $categoryMetaData) = getRecords(array(
    'tableName'   => 'category',
  ));

  list($marketplaceRecords, $marketplaceMetaData) = getRecords(array(
    'tableName'   => 'marketplace',
  ));

  list($marketplace_couponsRecords, $marketplace_couponsMetaData) = getRecords(array(
    'tableName'   => 'marketplace_coupons',
  ));

  list($marketplace_classifiedsRecords, $marketplace_classifiedsMetaData) = getRecords(array(
    'tableName'   => 'marketplace_classifieds',
  ));

  // index marketplaceRecords by title
  $marketplaceRecordsByTitle = array();
  foreach ($marketplaceRecords as $marketplace) {
    $marketplaceRecordsByTitle[ $marketplace['title'] ] = $marketplace;
  }
  
  // join data from marketplace_couponsRecords
  foreach ($marketplace_couponsRecords as $marketplace_coupon) {
    if ( isset($marketplaceRecordsByTitle[ $marketplace_coupon['title'] ]) ) {
      $marketplaceRecordsByTitle[ $marketplace_coupon['title'] ]['coupon'] = $marketplace_coupon;
    }
  }

  // join data from marketplace_classifiedsRecords
  foreach ($marketplace_classifiedsRecords as $marketplace_classified) {
    if ( isset($marketplaceRecordsByTitle[ $marketplace_classified['title'] ]) ) {
      $marketplaceRecordsByTitle[ $marketplace_classified['title'] ]['classified'] = $marketplace_classified;
    }
  }
  
  // index categoryRecords by name
  $categoryRecordsByTitle = array();
  foreach ($categoryRecords as $category) {
    $category['marketplaces'] = array();
    $categoryRecordsByTitle[ $category['title'] ] = $category;
  }
  foreach ($marketplaceRecordsByTitle as $marketplace) {
    foreach (explode("\t", trim($marketplace['cats'], "\t")) as $catName) {
      if ( !$catName ) { continue; }
      $categoryRecordsByTitle[$catName]['marketplaces'][] = $marketplace;
    }
  }
  function testCategoryHasMarketplaces($category) {
    return (count($category['marketplaces']) > 0);
  }
  $nonEmptyCategories = array_filter($categoryRecordsByTitle, 'testCategoryHasMarketplaces');

?>

<?php foreach ($nonEmptyCategories as $category): ?>
  Category: <?php echo $category['title'] ?><br />
  <?php foreach ($category['marketplaces'] as $marketplace): ?>
    <table width="543" border="0" cellspacing="0" cellpadding="0" height="31">
      <tr height="23">
        <td width="19" height="23"></td>
        <td width="242" height="23"><a href="<?php echo $marketplace['_link'] ?>"><span class="links"><b><?php echo $marketplace['title'] ?></b></span></td>
        <td width="37" height="23"><?php if ($marketplace['featured_ad']): ?><img src="check.jpg" width="34" height="23" /><?php endif ?></td>
        <td width="102" height="23"></td>
        
        <td width="34" height="23"><?php if (isset($marketplace['classified']) && $marketplace['classified']['featured_classified']): ?><img src="check.jpg" width="34" height="23" /><?php endif ?></td>
        <td width="71" height="23"></td>
        <td width="38" height="23"><?php if (isset($marketplace['coupon']) && $marketplace['coupon']['featured_coupon']): ?><img src="check.jpg" width="34" height="23" /><?php endif ?></td>
      </tr>
    </table>
  <?php endforeach ?>
<?php endforeach ?>
