Display Values from List Field Type in a Multi Record

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

By RGC - February 6, 2013 - edited: February 6, 2013

I am looking for code to display the values from a list field (field name - county) which is a field within a multi record (table name - towns)

If I use the following code the value displays repeatedly for every mutli-record that is associated with it. I only want to display the county once.

          <?php
              // load records from 'towns'
              list($townsRecords, $townsMetaData) = getRecords(array(
                'tableName'   => 'towns',
                'loadUploads' => true,
                'allowSearch' => false,
              ));
            
            ?>
          <?php foreach ($townsRecords as $record): ?>
          <li><?php echo $record['county'] ?></li>
          <?php endforeach ?>

Thanks for you assistance!

Rod

By RGC - February 6, 2013

Thanks Steve I have taken your code and re-written as follows:

<?php $towns_grouping = array_filter(array_pluck($townsRecords, 'county')); ?>
<?php foreach ($towns_grouping as $towns_group_name): ?>
  <p><strong><?php echo $towns_group_name; ?></strong></p>

<?php endforeach ?>

The county values are displaying, but repeatedly for each multi record associated with the county list value. What I am looking to do is just display the 8 values for the list type county once.

Rod

By Steve99 - February 6, 2013

You need that other chunk of code in there. Just swap out "FIELD" with your field name.

  <?php $towns_grouping = array_filter(array_pluck($townsRecords, 'county')); ?>
  <?php foreach ($towns_grouping as $towns_group_name): ?> 
  <p><strong><?php echo $towns_group_name; ?></strong></p>

  <ul>
   <?php foreach ($townsRecords as $record): ?>
   <?php if ($record['county:label'] == $towns_group_name) : ?>    
     <li><?php echo $record['FIELD'] ?></li>
   <?php endif ?>
   <?php endforeach ?>
  </ul>

  <?php endforeach ?>

Let me know how that works out for you.

Output should be:

Town Group Name 1
- correlating records

Town Group Name 2
- correlating records

Town Group Name 3
- correlating records