SamC Posted April 1, 2017 Share Posted April 1, 2017 So I've been working with categories: ...and advancing upon this as a way to get to learn URL segments. So mysite.com/cars/ outputs all cars: ...with the code being: // car-index.php <?php namespace ProcessWire; ?> <?php if($input->urlSegment1) { $segment1 = $sanitizer->pageName($input->urlSegment1); echo $segment1; } ?> <?php if($input->urlSegment2) { $segment2 = $sanitizer->pageName($input->urlSegment2); echo $segment2; } ?> <h1>Index of cars</h1> <?php $cars = $pages->find("template=car-entry," . $segment1 . "=" . $segment2); foreach ($cars as $car): ?> <h2><?php echo $car->title; ?></h2> <p>MAKE: <a href="/cars/make/<?php echo $car->make->name; ?>"><?php echo $car->make->title; ?></a></p> <p>MODEL: <a href="/cars/model/<?php echo $car->model->name; ?>"><?php echo $car->model->title; ?></a></p> <p>YEAR: <a href="/cars/year/<?php echo $car->year->name; ?>"><?php echo $car->year->title; ?></a></p> <p>COLOUR: <a href="/cars/colour/<?php echo $car->colour->name; ?>"><?php echo $car->colour->title; ?></a></p> <p>SEATS: <a href="/cars/seats/<?php echo $car->seats->name; ?>"><?php echo $car->seats->title; ?></a></p> <p>TYPE: <a href="/cars/type/<?php echo $car->type->name; ?>"><?php echo $car->type->title; ?></a></p> <?php endforeach; ?> The natural tree structure would be: site.com/make/mercedes/ site.com/model/e-class/ site.com/year/2005/ site.com/colour/red/ site.com/seats/5/ site.com/type/mpv/ ... but I got this working ok without needing URL segments. Just saying so you are aware that I am doing this approach on purpose even though it's not necessary. The code above shows that I added a /cars/ into the anchor tags, thus: site.com/cars/make/mercedes/ site.com/cars/model/e-class/ site.com/cars/year/2005/ site.com/cars/colour/red/ site.com/cars/seats/5/ site.com/cars/type/mpv/ ...I enabled URL segments in the car-index.php template and gave it a shot. So the links with text work: AT: site.com/cars/make/mercedes/ print_r($cars); // outputs ProcessWire\PageArray Object ( [hooks] => Array ( [PageArray::render] => MarkupPageArray->renderPageArray() in MarkupPageArray.module [PageArray::renderPager] => MarkupPageArray->renderPager() in MarkupPageArray.module ) [count] => 1 [items] => Array ( [0] => /cars/old-banger/ ) [total] => 1 [start] => 0 [limit] => 0 [selectors] => template=car-entry, make=mercedes, status<1024 ) ...but the years and seats don't? AT: site.com/cars/year/2005/ print_r($cars) // outputs ProcessWire\PageArray Object ( [hooks] => Array ( [PageArray::render] => MarkupPageArray->renderPageArray() in MarkupPageArray.module [PageArray::renderPager] => MarkupPageArray->renderPager() in MarkupPageArray.module ) [count] => 0 [total] => 0 [start] => 0 [limit] => 0 [selectors] => template=car-entry, year=2005, status<1024 ) Have I got a typo or something? I thought it was something to do with: $segment2 = $sanitizer->pageName($input->urlSegment2); ...but even just using: $segment2 = $input->urlSegment2; ...doesn't work either. Any ideas? Thanks. --EDIT-- adding another image to show how it's setup in admin. Link to comment Share on other sites More sharing options...
Macrura Posted April 1, 2017 Share Posted April 1, 2017 numbers do work fine in url segments; there must be something going on with your selector, and either some type mismatch, or other issue with the selector; do you have tracy debugger installed? you can run a series of tests on your selector to make sure it is returning results, and also dump/log the segments or sanitized segments to see what is going on Link to comment Share on other sites More sharing options...
SamC Posted April 1, 2017 Author Share Posted April 1, 2017 I don't have tracy debugger installed, I'll look into that. It's weird though because even manually making the selectors: Good condition small car MAKE: Ford MODEL: Ka YEAR: 2008 COLOUR: Black SEATS: 5 TYPE: Other // no results $cars = $pages->find("template=car-entry, seats=5"); // no results $cars = $pages->find("template=car-entry, year=2008"); // returns car above $cars = $pages->find("template=car-entry, make=ford"); // returns car above $cars = $pages->find("template=car-entry, model=ka"); // returns car above $cars = $pages->find("template=car-entry, colour=black"); I can't see the difference between the selectors, other than the first two are numbers. Link to comment Share on other sites More sharing options...
adrian Posted April 1, 2017 Share Posted April 1, 2017 Try seats.name=5 and year.name=2008 With page reference fields you are supposed to specify the subfield from the page that has been selected. I never actually realized that some work without specifying the subfield, but I just tested an can confirm what you are seeing. Not sure if this is really a bug or not, but I would definitely recommend always specifying the subfield (either name, title, or any other field from the selected page). 2 Link to comment Share on other sites More sharing options...
SamC Posted April 1, 2017 Author Share Posted April 1, 2017 58 minutes ago, adrian said: Try seats.name=5 and year.name=2008 With page reference fields you are supposed to specify the subfield from the page that has been selected. I never actually realized that some work without specifying the subfield, but I just tested an can confirm what you are seeing. Not sure if this is really a bug or not, but I would definitely recommend always specifying the subfield (either name, title, or any other field from the selected page). As usual @adrian you're spot on. Thanks now I can move on. Link to comment Share on other sites More sharing options...
SamC Posted April 1, 2017 Author Share Posted April 1, 2017 <?php namespace ProcessWire; ?> <?php if($input->urlSegment1) { $segment1 = $sanitizer->pageName($input->urlSegment1); $segment1 .= ".name="; } ?> <?php if($input->urlSegment2) { $segment2 = $sanitizer->pageName($input->urlSegment2); } ?> <h1>Index of cars</h1> <?php $cars = $pages->find("template=car-entry," . $segment1 . $segment2); print_r($cars); foreach ($cars as $car): ?> <h2><?php echo $car->title; ?></h2> <p>MAKE: <a href="/cars/make/<?php echo $car->make->name; ?>"><?php echo $car->make->title; ?></a></p> <p>MODEL: <a href="/cars/model/<?php echo $car->model->name; ?>"><?php echo $car->model->title; ?></a></p> <p>YEAR: <a href="/cars/year/<?php echo $car->year->name; ?>"><?php echo $car->year->title; ?></a></p> <p>COLOUR: <a href="/cars/colour/<?php echo $car->colour->name; ?>"><?php echo $car->colour->title; ?></a></p> <p>SEATS: <a href="/cars/seats/<?php echo $car->seats->name; ?>"><?php echo $car->seats->title; ?></a></p> <p>TYPE: <a href="/cars/type/<?php echo $car->type->name; ?>"><?php echo $car->type->title; ?></a></p> <?php endforeach; ?> AT: /cars/year/2005 AT: /cars/seats/5 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