Jump to content

Remove/merge duplicate results


taotoo
 Share

Recommended Posts

I have three types of content, with two page reference fields linking them.

                          work

                    /                  \

work_exhibition      work_artist

           /                                   \

exhibition                            artist


On the Exhibition template, I want to show all artists that appear in an exhibition, and beneath each artist all works that are both by them and in the exhibition.

I have the following code:

<?php foreach ($pages->find("template=work, work_exhibition=$page->path") as $workIds):?>

	<?php foreach ($pages->find("template=artist, title={$workIds->work_artist->title}") as $artist):?>

		<strong><?= $artist->title; ?></strong><br>

		<?= $workIds->title; ?><br><br>

	<?php endforeach; ?>

<?php endforeach; ?>

Which is giving me (e.g.):

Artist 1
Work

Artist 1
Work

Artist 2
Work

I'd like it to instead show:

Artist 1
Work
Work

Artist 2
Work

I've tried using "->unique()" but wasn't sure where to put it. Or maybe I need to go about this some other way. Any pointers appreciated.

Link to comment
Share on other sites

I read your post a few times but wasn't able to grasp exactly how you have this set up.

In case it helps, here is how I would tend to approach this...

Work template: has Page Reference field for Artist

Exhibition template: has Page Reference field for Works

Then in your Exhibition template file:

$artist = '';
foreach($page->works->sort('artist') as $work) {
    if($work->artist->title !== $artist) {
        $artist = $work->artist->title;
        echo "<h3>$artist</h3>";
    }
    echo "<p>$work->title</p>";
}

 

  • Thanks 1
Link to comment
Share on other sites

In that case:

$artist = '';
foreach($pages->find("template=work, work_exhibition=$page, sort=work_artist.title") as $work) {
    if($work->work_artist->title !== $artist) {
        $artist = $work->work_artist->title;
        echo "<h3>$artist</h3>";
    }
    echo "<p>$work->title</p>";
}

 

  • Thanks 1
Link to comment
Share on other sites

9 minutes ago, taotoo said:

I'll now go through your code and attempt to understand what it's doing

// Initialise artist name variable
$artist = '';
// Find "work" pages that have this exhibition selected in their "work_exhibition" field
// and sort them according to the title (i.e. name) of the artist
// Iterate over those works...
foreach($pages->find("template=work, work_exhibition=$page, sort=work_artist.title") as $work) {
    // If the name of this work's artist is different to $artist
    if($work->work_artist->title !== $artist) {
        // Then store the artist's name in $artist
        $artist = $work->work_artist->title;
        // And output a heading
        echo "<h3>$artist</h3>";
    }
    // Output the title of the work
    echo "<p>$work->title</p>";
}

 

  • Like 3
  • Thanks 1
Link to comment
Share on other sites

10 hours ago, Robin S said:

// Initialise artist name variable
$artist = '';
// Find "work" pages that have this exhibition selected in their "work_exhibition" field
// and sort them according to the title (i.e. name) of the artist
// Iterate over those works...
foreach($pages->find("template=work, work_exhibition=$page, sort=work_artist.title") as $work) {
    // If the name of this work's artist is different to $artist
    if($work->work_artist->title !== $artist) {
        // Then store the artist's name in $artist
        $artist = $work->work_artist->title;
        // And output a heading
        echo "<h3>$artist</h3>";
    }
    // Output the title of the work
    echo "<p>$work->title</p>";
}

 

I've been staring at this for a while and finally figured out why it works - very clever!

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...