<?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',
  ));

  // a collection of our records
  $recordSets = array(
    'marketplace' => $marketplaceRecords,
    'coupon'      => $marketplace_couponsRecords,
    'classified'  => $marketplace_classifiedsRecords,
  );
  
  // index item records in recordSets by their titles
  $rowsByTitle = array();
  foreach ( $recordSets as $recordSetName => &$records ) {
    foreach ( $records as &$record ) {
      // skip processing records without a title
      if ( strlen($record['title']) === 0 ) { continue; }
      
      // lookup an existing row by this record's title or create a new one
      if ( !array_key_exists($record['title'], $rowsByTitle) ) {
        $rowsByTitle[ $record['title'] ] = array();
        $rowsByTitle[ $record['title'] ][ 'title' ] =& $record['title'];
        $rowsByTitle[ $record['title'] ][ '_link' ] =& $record['_link'];
        $rowsByTitle[ $record['title'] ][ 'cats' ] = array();
      }
      $row =& $rowsByTitle[ $record['title'] ];
      
      // add this record's cats to the row's existing cats
      if ( $record['cats'] !== "" ) {
        $recordCats = explode("\t", trim($record['cats'], "\t"));
        $row['cats'] = array_unique(array_merge( $row['cats'], $recordCats ));
      }
      
      // create row-to-record reference keyed on recordSetName
      $row[ $recordSetName ] =& $record;
    }
  }
 
  // index categoryRecords by name
  $categoryRecordsByTitle = array();
  foreach ($categoryRecords as &$category) {
    $category['rows'] = array();
    $categoryRecordsByTitle[ $category['title'] ] =& $category;
  }
  
  // insert rows into categories
  foreach ( $rowsByTitle as &$row ) {
    foreach ( $row['cats'] as &$categoryName ) {
      $categoryRecordsByTitle[ $categoryName ][ 'rows' ][] =& $row;
    }
  }
  
  // strip categories with no rows
  function testCategoryHasRows($category) {
    return (count($category['rows']) > 0);
  }
  $nonEmptyCategories = array_filter($categoryRecordsByTitle, 'testCategoryHasRows');

?>

<?php foreach ($nonEmptyCategories as &$category): ?>
  Category: <?php echo $category['title'] ?><br />
  <?php foreach ($category['rows'] as &$row): ?>
    <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 $row['_link']; ?>"><span class="links"><b><?php echo $row['title'] ?></b></span></td>
        <td width="37" height="23"><?php if (array_key_exists('marketplace', $row) && $row['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 (array_key_exists('classified', $row) && $row['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 (array_key_exists('coupon', $row) && $row['coupon']['featured_coupon']): ?><img src="check.jpg" width="34" height="23" /><?php endif ?></td>
      </tr>
    </table>
  <?php endforeach ?>
<?php endforeach ?>
