Skip duplicate records

4 posts by 2 authors in: Forums > CMS Builder
Last Post: June 20, 2018   (RSS)

By hiroko - June 7, 2018

Hi,

I have an exhibition detail viewer page where the participating artists are listed. In this page, I have a list of related exhibition but I am having a problem with duplicate records listed when the artists are participating in the same exhibitions.

This is the listing part of the detail page.

<!--Related exhibitions-->
        <div class="l-column l-row u-paddingTop--half">
            <?php $fieldValues = explode("\t",trim($detailRecord['this_artist'],"\t")); ?>
            <?php foreach($fieldValues as $artistNum): ?>
            <?php foreach ($eventRecords as $record): ?>
            <?php $fieldValues2 = explode("\t",trim($record['this_artist'],"\t")); ?>
            <?php foreach($fieldValues2 as $artistNum2): ?>
            <?php if ($artistNum2 == $artistNum && ($artistNum > 0)): ?>
            <div class="EntryPanel l-flexAuto-4column">
                <div class="EntryPanel--vertical">
                    <a href="/en/<?php echo $record['permalink'] ?>/" title="<?php echo $record['title'] ?>">
                        <div class="EntryPanel__sub EntryPanel__sub--medium imgLiquidFill imgLiquid">
                            <?php foreach ($record['image'] as $index => $upload): ?>
                            <?php if ($index >= 1) { continue; } // limit uploads shown ?>
                            <img src="<?php echo htmlencode($upload['thumbUrlPath2']) ?>" alt="<?php echo $record['num'] ?><?php echo $record['title'] ?>"/>
                            <?php endforeach ?>
                        </div>
                    </a>
                    <div class="EntryPanel__main">
                        <a href="/en/<?php echo $record['permalink'] ?>/" title="<?php echo $record['title'] ?>">
                            <h3 class="EntryPanel__head">
                                <?php echo $record['title'] ?>
                            </h3>
                        </a>
                    </div>
                </div>
            </div>
            <?php endif ?>
            <?php endforeach ?>
            <?php endforeach ?>
            <?php endforeach ?>
            <!--/Related Exhibitions-->

Is there a way to skip the same records?

This is the example of the page. The list is at the bottom page.

Thank you in advance.

Hiroko

By leo - June 7, 2018

Hi Hiroko,

If you want to get related artist/event records from an event record, you don't need massive nested foreach loops to do that. You can get the records you need by using mysql_select or getRecords with where statements when you are looping through artist nums.

Regarding the duplicate record issue, you can try creating an array of record nums first and add new num when the num doesn't already exist in the array, and then get all the records once in the end. Here is an example showing the concept:

...

$eventNumArray = [];

...
// When you figured the event that needs to be displayed
if(!in_array($eventRecord['num'], $eventNumArray)){
  $eventNumArray[] = $eventRecord['num'];
}

...
$displayEventRecords = mysql_select('events', 'num IN (' . implode(',', $eventNumArray) . ')');

foreach($displayEventRecords as $record){
 // echo html to show the related event record
}

Let me know if you have any questions!

Thanks,

Leo - PHP Programmer (in training)
interactivetools.com

By leo - June 20, 2018

Hi Hiroko,

That looks good to me!

Leo - PHP Programmer (in training)
interactivetools.com