Jump to content

Repeaters - outputting (and adding up) user created fields


Galaxy
 Share

Recommended Posts

How do I target and output the data entered in repeater fields?

I am using PW for a directory type website that shows how many grants organisations have recieved in donations from a community trust.

Each time a recipient applies for a new grant we enter it via a repeater field called "grants" by selecting +add item - this creates a new set of fields that have date and amount approved. Potentially there could be half a dozen grants per recipient.

Looking at an example - we have a field within a repeater (called "amount_approved) :

<input id="Inputfield_amount_approved_repeater2038" class="FieldtypeInteger" type="text" size="10" value="500" name="amount_approved_repeater2038">
 

and the next field is a date input (called "cheque_date")

<input id="Inputfield_cheque_date_repeater2038" class="InputfieldDatetimeDatepicker InputfieldDatetimeDatepicker3 hasDatepicker" type="text" data-ampm="1" data-ts="1360062000000" data-timeformat="" data-dateformat="yy-mm-dd" value="2013-02-06" size="25" name="cheque_date_repeater2038">
 

When we create a new set by using +add new item under a repeater set, the same field in the next new repeater  is

<input id="Inputfield_amount_approved_repeater2039" class="FieldtypeInteger" type="text" size="10" value="600" name="amount_approved_repeater2039">

and the date field in the same repeater

<input id="Inputfield_cheque_date_repeater2039" class="InputfieldDatetimeDatepicker InputfieldDatetimeDatepicker3 hasDatepicker" type="text" data-ampm="1" data-ts="1361962800000" data-timeformat="" data-dateformat="yy-mm-dd" value="2013-02-28" size="25" name="cheque_date_repeater2039">
 

In this case PW added the unique "repeater2039" and "repeater2038" to the ID and the name of the fields.

(see attached screenshot of how the repeaters/fields look in admin edit mode)

So now to display these via the template - how can I output these variables to look something like this:

Date granted: 06 Feb 2013

Amount:  $500

 

Date granted: 28 Feb 2013

Amount: $600

Also, I want to grab the latest date a grant was made and the total of all grants and display them:

Last grant made: 28 Feb 2013

Total of all grants: $1100

Thanks for any help on this.

post-1337-0-22549400-1370547624_thumb.jp

Link to comment
Share on other sites

Galaxy,

 

These should get you started...nothing really special about accessing repeaters :)

 

Accessing repeatable fields from the API is super-easy. They are fully searchable/selectable with the API just as easily as with any other fields

The individual items contained in a repeater field are technically pages in ProcessWire. The value of a repeater field is a PageArray. As a result, you may interact with them exactly like you would any other group of pages in ProcessWire.

 

 

http://processwire.com/api/fieldtypes/repeaters/

http://processwire.com/talk/topic/958-repeatable-fields/

  • Like 1
Link to comment
Share on other sites

Hi Galaxy,

Have you read the docs?

http://processwire.com/api/fieldtypes/repeaters/

It's pretty straightforward:

$total = 0;
foreach ($page->grants as $grant) {
  echo "Date granted: {$grant->cheque_date}";
  echo "Amount ${$grant->amount_approved}";
  $total += $grant->amount_approved;
}

To get the last date, there are several solutions. One of them:

$page->grants->sort('-cheque_date')->first()->cheque_date;

Edit: kongondo is online all the time and faster than me :)

  • Like 2
Link to comment
Share on other sites

hey thanks

but for

 <?php
        $total = 0;
        foreach ($page->grants as $grant) {
          echo "<p>Date granted: {$grant->cheque_date}</p>";
          echo "<p>Amount ${$grant->amount_approved}</p>";
          $total += $grant->amount_approved;
        }
     ?>
 

It outputs no amounts and the date format eg.


Date granted: 1360062000

Amount

Date granted: 1361962800

Amount

   

Link to comment
Share on other sites

please excuse my ignorance but this isn't working for me. There is a field called "amount_approved" (see original attached screen capture)

Also, I am unsure as how to format the date (I'm going to get some PHP lessons...)

Link to comment
Share on other sites

In order to start building websites with ProcessWire you don't have to know much PHP, just basics. Check out this thread.

To format your date do this (you may want to change format - check out the link posted above):

$myDate = date("Y-m-d H:i", $grant->cheque_date);
echo "<p>Date granted: $myDate</p>";

You can check the field name in setup/fields to make sure they are ok - field names are not visible on your screenshot, only their titles.

Also you can install this module to help you navigate from inputfields to corresponding fields quickly http://modules.processwire.com/modules/helper-field-links/

  • Like 1
Link to comment
Share on other sites

There was a typo here (you don't need that second dollar sign), so it should be:

echo "Amount {$grant->amount_approved}";

As for the date, I think you need this instead:

$myDate = $grant->getUnformatted("cheque_date");
$myDate = date("Y-m-d H:i", $myDate);
echo "<p>Date granted: $myDate</p>";
  • Like 1
Link to comment
Share on other sites

No need to format the date with PHP. You can set the output format in ProcessWire - check out the field settings of your date fields.

Ye, this does confuse at times...PW has both input and output formats for date...

  • Like 1
Link to comment
Share on other sites

Hey slkwrm


 

There was a typo here (you don't need that second dollar sign), so it should be...

It wasn't really a typo - I need a $ symbol in front of the amount output. It works if I put a space after the first $ as in

echo "<h2>Amount $ {$grant->amount_approved}</h2>";
 

but if the space is removed it doesn't work. What I had to do was this:

echo "<h2>Amount $";
echo "{$grant->amount_approved}</h2>";
 

thanks for your input dude

Link to comment
Share on other sites

Also, after listing all grants I want to grab the latest date a grant was made and the total of all grants and display them in an info box eg:

 

Last grant made: 28 Feb 2013 (this grabs the most recent date from all grants)

Total of all grants: $1100 (adds up all grants made)

 

Any ideas?

Link to comment
Share on other sites

(Note: I'd misguidedly edited this after getting it to work but Wanze and Diogo had answered - see following posts)

Thanks Wanze

This is what I've got that seems to output as required

    

<?php
        $total = 0;
        foreach ($page->grants as $grant) {
          echo "<h2>{$grant->cheque_date} ";
          echo "\${$grant->amount_approved}</h2>";
          $total += $grant->amount_approved;
        }
        $page->grants->sort('-cheque_date')->first()->cheque_date;
          echo "<p>Date of last grant: {$grant->cheque_date}</p> ";
        echo "<p>Total grants: {$total}</p> ";
     ?>
 

 thanks for your help dude

Link to comment
Share on other sites

Inside the foreach, you are summing up the amounts. You forgot a + in the following snippet above:

$total += $grant->amount_approved;
// Is the same as
// $total = $total + $grant->amount_approved;

After the loop, you can echo the $total variable where you want to appear it in your template.

Same holds for the latest date:

$latestDate = $page->grants->sort('-cheque_date')->first()->cheque_date;
echo $latestDate;
  • Like 2
Link to comment
Share on other sites

Little explanation of Wanze's code. Hope it helps:

// total starts with zero
$total = 0;

// loop through all grants and store each of them in the $grant variable for their part of the loop
foreach ($page->grants as $grant) {

  // echo this particular grant cheque_date
  echo "Date granted: {$grant->cheque_date}";

  // echo this particular grant amount approved
  echo "Amount ${$grant->amount_approved}"; 
  
  // add this particular grant amount approved to the total that we have in this moment (in the end of the loop the total will be the sum of all amounts)
  $total += $grant->amount_approved;

}

What you don't have in your code is "+=" that adds the amount of this grant to the total. In your code the total is always the same as the previous amount and in the end it will be equal to the last amount.

--

$page->grants->sort('-cheque_date')->first()->cheque_date;

For what I understand this returns the value the most recent cheque date.

you can echo it directly like this:

echo $page->grants->sort('-cheque_date')->first()->cheque_date;

or store it in a variable

$last_cheque_date = $page->grants->sort('-cheque_date')->first()->cheque_date;

...and use it later

echo "the most recent cheque date is {$last_cheque_date}.";

or

// just a random example...

if ($last_cheque_date < 165436524) {
    // do something
}
  • Like 3
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...