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,
interactivetools.com
By hiroko - June 20, 2018
Hi Leo,
Thanks for the reply and sorry for the late response.
I couldn't figure out how to get the related records from a multi value field for a while, but looking through the forum, I am finally getting the result I need.
I tried some options, and this post seemed to fit:
In my case the code became like this
if(is_array(@$detailRecord['this_artist:values'])){
$arrayCounter = count($detailRecord['this_artist:values']);
foreach($detailRecord['this_artist:values'] as $key => $filterItem){
$searchString .= "this_artist LIKE '%\t$filterItem\t%'";
if(($key+1) != $arrayCounter){
$searchString .= " OR ";
}
}
}
this worked except the viewer shows all records when there is nothing selected on the multi value.
So, I used a non-existing num to avoid it. ( I am sure there is a better solution to this )
$searchString = '';
if(!@$detailRecord['this_artist']){
$searchString .= "this_artist LIKE '%\t0\t%'";
}
if(is_array(@$detailRecord['this_artist:values'])){
$arrayCounter = count($detailRecord['this_artist:values']);
foreach($detailRecord['this_artist:values'] as $key => $filterItem){
$searchString .= "this_artist LIKE '%\t$filterItem\t%'";
if(($key+1) != $arrayCounter){
$searchString .= " OR ";
}
}
}
Will this cause any problem?
By leo - June 20, 2018
Hi Hiroko,
That looks good to me!
interactivetools.com