Jump to content
brandy

Sum up fields of pages

Recommended Posts

Hi!

I have different pages, where I can check an option via checkbox.
Now I want to count the pages, where the checkbox is checked.

How is this possible?
Thanks a lot!
 

Share this post


Link to post
Share on other sites

One way to do it:

<?php namespace ProcessWire;

$checkedPagesCount = $pages->count("name_of_checkbox_field=1");

You might want to refine the selector, e.g. limit to a parent or template(s). 

untested.

Edit

I see @lokomotivan beat me to it 😁.

  • Like 1

Share this post


Link to post
Share on other sites

Thanks a lot. That looks very good, but I missed one thing - the field is on the grandchild respectively on the great-grandchild of the pages.

The child is an detailed overview of numerous grandchildren, and the page itself shows all children, where I want to have the checked fields counted.
Here the structure:

-> page (should show a sum of the checked fields)
-> -> child
-> -> ->  grandchild (contains field, which is checked or not)
-> -> -> grandchild (contains field, which is checked or not)
-> -> -> grandchild (contains field, which is checked or not)

I already have a loop on the top page, to show the field of the child:

<?php foreach($page->children() as $child) {

Thanks!

Share this post


Link to post
Share on other sites

Like kongondo mantioned, you can refine the selector, for example you can include grandchild template:
 

$pages->count("template=grandchild_template_name, name_of_checkbox_field=1");


 

Share this post


Link to post
Share on other sites

Hi!

Thanks a lot - it´s working now. But not 100%.

This is my code:
 

		<?php foreach($page->children() as $child) {
			echo "<div class='column column-block'>
						<div class='row'>
							<div class='large-12 small-12 columns'>
								<div style='border: 1px solid black; padding: 0px !important;'>
									<div style='width: 100%; background-image: url({$child->projekt_main_image->url}); background-size: cover; background-position:center center; height: 400px;'></div>
									<div style='text-align:center; background-color: black; color: white; padding: 10px 10px;'>
											<h4><strong><i>{$child->title}</i></strong></h4>
									</div>
									<div class='row' style='margin-top: 20px; margin-bottom: 20px;'>
										<div class='large-4 small-4 columns' style='padding: 10px; border-right: 1px solid black; text-align: center;'><div style='text-transform:uppercase;'>Tops gesamt</div><div style='font-size: 3.5rem;'><strong>{$child->count('template=tops')}</strong></div></div>
										<div class='large-4 small-4 columns' style='padding: 10px; border-right: 1px solid black; text-align: center;'><div style='text-transform:uppercase;'>Tops zu mieten</div><div style='font-size: 3.5rem;'><strong>{$child->count('template=tops, projekt_top_vermietet=0')}</strong></div></div>
										<div class='large-4 small-4 columns' style='padding: 10px; text-align: center;'><div style='text-transform:uppercase;'>Tops zu kaufen</div><div style='font-size: 3.5rem;'><strong>{$child->count('template=tops, projekt_top_verkauft=0')}</strong></div></div>
									</div>
									<div class='row'>
										<div class='large-8 small-centered columns' style='text-align: center;'><a href='{$child->url}' class='button expanded rounded' style='background-color: black;'>Alle Informationen</a></div>
									</div>
								</div>
							</div>
						</div>
					</div>";
			} ?>

$child->count('template=tops') works and the right number is shown, but {$child->count('template=tops, projekt_top_vermietet=0')} and {$child->count('template=tops, projekt_top_verkauft=0')} are showing the same number as the first count, although the fields 'projekt_top_vermietet' and 'projekt_top_verkauft' have different states in the different pages.

Thanks a lot!

 

 

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...