brandy Posted November 17, 2020 Posted November 17, 2020 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!
lokomotivan Posted November 17, 2020 Posted November 17, 2020 $pages->count("checkbox_field_name=1"); ? 1
kongondo Posted November 17, 2020 Posted November 17, 2020 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 ?. 1
brandy Posted November 17, 2020 Author Posted November 17, 2020 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!
lokomotivan Posted November 17, 2020 Posted November 17, 2020 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");
netcarver Posted November 17, 2020 Posted November 17, 2020 You might also be able to use ESRCH's PagesSum module to do this - but I never tried it on checkbox fields before.https://github.com/netcarver/PW-PagesSum 1
brandy Posted November 22, 2020 Author Posted November 22, 2020 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!
brandy Posted December 14, 2020 Author Posted December 14, 2020 Ok, it works now with this 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>{$pages->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>{$pages->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>{$pages->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>"; } ?> Now the fields are summed up, which are supposed to be summed up. This means he sums up the fields from all pages under "Projects". Home - Projects - - Project 1 - - - TOP 1 - - - TOP 2 - - - ... - - Project 2 - - - TOP 1 - - - TOP 2 - - - ... - - Project 3 - - - TOP 1 - - - TOP 2 - - - ... How can I differ between the different pages under "Projects" by its parent? I tried it with "has_parent=$child", as the template is set in "Projects"! Thanks a lot! 1
brandy Posted December 14, 2020 Author Posted December 14, 2020 An id of a parent works, like this: parent_id=1021 But I do need it in between the loop dynamically? How do I get this done?
brandy Posted December 16, 2020 Author Posted December 16, 2020 Now I changed {$pages->count('template=tops')} to {$child->count('template=tops')} It works better, but the question, that I want count grandchildren-pages with the template "tops" is still open! Can you help me?
brandy Posted December 21, 2020 Author Posted December 21, 2020 Can nobody help me in this case? Thanks a lot - I think, I just need a little hint!
brandy Posted December 21, 2020 Author Posted December 21, 2020 Although the template of the grandchildren and the field is given, the sum isn´t right: {$child->count('template=tops, projekt_top_vermietet=1')}
diogo Posted December 21, 2020 Posted December 21, 2020 Try: $pages->count("has_parent=$child, template=tops, projekt_top_vermietet=1") notice that I changed the single quotes to double quotes, so the variable inside them can be evaluated. 2
brandy Posted December 22, 2020 Author Posted December 22, 2020 Oh diogo you´re the man! Thanks a lot - I have tried already with has_parent, and I knew there is only a small mistake by me. Thanks a lot! 1
brandy Posted March 8, 2021 Author Posted March 8, 2021 Can I change the tag $pages->count("has_parent=$child, template=tops, projekt_top_vermietet=1") so that the template is "tops" OR "tops_2"? Short: I want to count the grandchildren with two different templates. Thanks a lot!
brandy Posted March 8, 2021 Author Posted March 8, 2021 Or the easier way: How do I check if the grandchild has at least a children? It should like something like this: if($page->$child->$child->hasChildren()) { ... } I know the syntax above is totally wrong - how do I can reproduce this right?
Jan Romero Posted March 8, 2021 Posted March 8, 2021 25 minutes ago, brandy said: How do I check if the grandchild has at least a children? You can do that with hasChildren() just like you proposed, but you have to get the grandchild first. If you want to get the first child of $page and then the first child of that, and then check if that has children, you can do $page->child()->child()->hasChildren(). You can also put selector strings between each of the parentheses to only get specific children. 3 hours ago, brandy said: Can I change the tag $pages->count("has_parent=$child, template=tops, projekt_top_vermietet=1") so that the template is "tops" OR "tops_2"? Short: I want to count the grandchildren with two different templates. Yeah, I’m pretty sure this should work: $pages->count("has_parent=$child, template=tops|tops2, projekt_top_vermietet=1") If you already know there aren’t any other templates under $child, you can just remove the template selector entirely. 2
brandy Posted March 9, 2021 Author Posted March 9, 2021 Thanks a lot, Jan Romero! I thought it would be a simple way, but I didn´t know that would be that simple! Thanks a lot!
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now