entschleunigung Posted September 8, 2017 Share Posted September 8, 2017 hello there, this is my first post, please excuse my bad english. simple site building is not my problem. create templates, fields and pages and some foreachs in the templates and my easy sites are ready, with this stuff i'm familar. but now ...! i will try to explain what i want to do. i have a template with this fields: - name (text) - zipcode (integer) - city (text) that's need for a register with ca. 1000 records. what i want is now group this fields like this in the frontend. [ PLZ 1 (23) ] --- [ PLZ 2 (12) ] --- [ PLZ 3 (8) ] --- and so on. these should be buttons and the number in the bracket should show the count of records with starts this zipcode. click on the button shows me on the next page all records with zipcode e.g. PLZ 8 - 80331 - Name - 80337 - Name - 80337 - Name - 80539 - Name - 80539 - Name - 80638 - Name i think the logic is simple to unterstand but for me as a non-programmer is it difficult to group the fields and build the page. Auf Deutsch könnte ich das natürlich besser erklären, but has anybody a hint for me? thanks & greetings from munich Link to comment Share on other sites More sharing options...
abdus Posted September 8, 2017 Share Posted September 8, 2017 You can group pages depending on a field and output them like this. I used `country` field, instead of `zipcode` but it doesnt change anything <?php namespace ProcessWire; $persons = $pages->find('template=person'); $grouped = []; foreach ($persons as $person) { $country = $person->country; if (isset($grouped[$country])) { $grouped[$country][] = $person; } else { $grouped[$country] = [$person]; } } ?> <?php foreach ($grouped as $country => $people): ?> <h2><?= $country ?></h2> <?php foreach ($people as $p): ?> <p><?= $p->title ?></p> <?php endforeach; ?> <?php endforeach; ?> Which outputs something like this 1 Link to comment Share on other sites More sharing options...
bernhard Posted September 8, 2017 Share Posted September 8, 2017 hi and welcome deceleration quite easy: <?php // overview for($i=1; $i<??; $i++) { echo "<a href='./$i'>"; // create a link with that url segment echo "ZIP $i... [" . $pages->count("template=yourtemplate, zipcode^=$i") . "]"; echo "</a><br>"; } // details $zip = $sanitizer->int($input->urlSegment1); if($zip) { $items = $pages->find("template=yourtemplate, zipcode^=$zip"); foreach($items as $item) { echo "<a href='{$item->url}'>{$item->title}</a><br>"; } } 3 Link to comment Share on other sites More sharing options...
abdus Posted September 8, 2017 Share Posted September 8, 2017 To get an output like you want, you can use: <?php foreach ($grouped as $country => $people): ?> <h2><?= $country ?> [<?= count($people) ?>]</h2> <?php endforeach; ?> Which gives 1 Link to comment Share on other sites More sharing options...
entschleunigung Posted September 8, 2017 Author Share Posted September 8, 2017 5 minutes ago, bernhard said: hi and welcome deceleration quite easy: for($i=1; $i<??; $i++) hallo bernhard, selam abdus, thanks for now, i will try it and reply when it works or not, but this for-loop i see the first time. this loop really works with the fragezeichen or really works? THX Link to comment Share on other sites More sharing options...
abdus Posted September 8, 2017 Share Posted September 8, 2017 Ok, in my test setup `country` field may include spaces, so urlSegments is out of question. In these situations you can use query strings and send encoded parameters with urls. If user goes to a nonexistent url, it redirects back to summary list <?php namespace ProcessWire; $detailMode = false; // show people in a country? $grouped = []; if ($input->get->country) { // always sanitize user input $country = $sanitizer->selectorValue($input->get->country); $persons = $pages->find("template=person, country=$country"); $detailMode = true; if (!$persons->count) $session->redirect('./'); } else { // summary mode $persons = $pages->find('template=person'); foreach ($persons as $person) { $country = $person->country; if (isset($grouped[$country])) { $grouped[$country][] = $person; } else { $grouped[$country] = [$person]; } } } ?> <?php if($detailMode): // showing single country ?> <h1><?= $persons->first->country ?></h1> <?php foreach ($persons as $person): ?> <?= $person->title ?><br> <?php endforeach; ?> <?php else: // showing all countries ?> <?php foreach ($grouped as $country => $people): ?> <?php $encoded = "./?" . http_build_query(['country' => $country]) ?> <?= $country ?> <a href="<?= $encoded ?>">[<?= count($people) ?>]</a><br> <?php endforeach; ?> <?php endif; ?> Result looks like this One drawback of using query parameters is that the url looks like mysite.com/url?param=value, instead of mysite.com/url/value. 2 Link to comment Share on other sites More sharing options...
bernhard Posted September 8, 2017 Share Posted September 8, 2017 sorry my solution only works if you have zip-codes like 1... 2... 3... 10... and 19... would be listed under 1... you could replace the for(...) with this $zipcodes = [11, 15, 80, 99]; foreach($zipcodes as $zip) { ... } always depends what your initial data looks like (if you know the zip-code-categories or if they are dynamic). 1 question, so many answers 1 Link to comment Share on other sites More sharing options...
entschleunigung Posted September 8, 2017 Author Share Posted September 8, 2017 sagol abdus, your solution is great and your explanation too, i will save it for further & greater problems i think my problem is solved by bernhards way. it's easy enough to deal with my zipcodes 0 - 9: THX Link to comment Share on other sites More sharing options...
abdus Posted September 8, 2017 Share Posted September 8, 2017 Eyvallah, kolay gelsin Link to comment Share on other sites More sharing options...
entschleunigung Posted September 8, 2017 Author Share Posted September 8, 2017 2 minutes ago, abdus said: Eyvallah, kolay gelsin sagol, yuvarlanıp gidiyoruz iste. means for the non-turkish members: thanks, this is really a great community and i learned a lot of interesting stuff. god bless ryan. 2 Link to comment Share on other sites More sharing options...
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