Jump to content
pppws

sorting repeater fields

Recommended Posts

hey there,

 

i have the following structure for a repeater field:

moments, 10x per page (unfortunatly a lot of pages, roughly 400)

-- yearofmoment

-- monthofmoment

-- textofmoment

 

i'm trying to find all repeater fields of certain year and sort them by month. e.g. get all repeater fields of year 1969, first repeater to be displayed: repeaters with monthofmoment = january.

i already got this:

 

<?php $langname = "es"; //temp: only get spanish profiles?>
<?php $persons = $pages->find("template=persons, lang=$langname, moments.yearofmoment=1969") ?>

<?php foreach ($persons as $p): ?>
    <?php foreach ($p->moments as $moments): ?>
        <?php if ($moments->yearofmoment == 1969): ?>
            <ul>
                <li><?= $p->givenname ?>,<?= $moments->month ?> <?= $moments->yearofmoment ?></li>
            </ul>
        <?php endif; ?>
    <?php endforeach; ?>
<?php endforeach; ?>

but somehow i can't figure out how to sort the repeater fields by month. the code above gives me this result: (julio should be on top, luis the last )

Screenshot%202017-11-29%2018.24.27.png?d

 

Share this post


Link to post
Share on other sites

this actually changes the order, but not in the way i need it. 

 

Screenshot%202017-11-30%2000.40.17.png?d

 

 

Edited by pppws
added image.

Share this post


Link to post
Share on other sites

no, i want all of the repeaters sorted by month. not only their parent pages.

the screenshot shows all pages wich include a repeater field with the year 1969. the names are the names of the pages. the second number is the month (1 = january, 4 = april and so on) but i don't need the pages to be sorted, but the repeater fields. so the list should be:

julio, 1

selva, 4

gloria, 5

gloria, 5

gloria, 6

miguel, 6

sandra, 7

catalina, 8

luis, 10

hilda, 11

 

 

Share this post


Link to post
Share on other sites

Try to replace this:

<?php foreach ($p->moments as $moments): ?>

with this:

<?php foreach ($p->moments->sort('month') as $moments): ?>

 

Share this post


Link to post
Share on other sites

Building on what PWaddict said, you could optimize the code just a little bit more, like this:

<?php foreach ($p->moments->find('yearofmoment=1969,sort=month') as $moments): ?>

You can then remove the conditional you have:

<?php if ($moments->yearofmoment == 1969): ?>
...
<?php endif; ?>

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...