a-ok Posted September 1, 2015 Share Posted September 1, 2015 Hi folks, I have some tags setup, using the Page field options, which is really flexible and works great. However, I am curious to know if it's possible to view multiple tags at any one time? Is this possible? For example, I know to create a tag page (for example, www.domain.com/tags/events), you do the following: <?php $tags = $pages->find('parent=/projects/, tags=' . $page . ', sort=sort'); ?> Is it possible for a query to exist so you can view multiple tags? www.domain.com/tags/events&films&outdoor Any thoughts? Good, bad? Cheers! R Link to comment Share on other sites More sharing options...
LostKobrakai Posted September 1, 2015 Share Posted September 1, 2015 You can use GET vars to do so: <?php // URL: www.domain.com/tags/?tags=events,films,outdoor // // Alt. URL with different template than tags overview: // www.domain.com/tags/multiple/?tags=events,films,outdoor $tags = $sanitizer->selectorValue($input->get->tags); $taggedPages = $pages->find("…, tags=$tags"); 1 Link to comment Share on other sites More sharing options...
a-ok Posted October 12, 2015 Author Share Posted October 12, 2015 Thanks again for your solution. I'm wondering if I can further enhance this more? See my screenshot: http://d.pr/i/18c1N Is it possible to create a submit so if the user selected architecture from the 'Show me' list and 'Education' from the 'For' list (which are two separate Page Fields) it would send them to a URL showing items with both sets applied? Is there any documentation available on something like this? Is it common? Thanks in advance. Link to comment Share on other sites More sharing options...
LostKobrakai Posted October 12, 2015 Share Posted October 12, 2015 <?php // URL: www.domain.com/tags/?showme=events,films,outdoor&for=education // // Alt. URL with different template than tags overview: // www.domain.com/tags/multiple/?tags=events,films,outdoor $showme = $sanitizer->selectorValue($input->get->showme); $for = $sanitizer->selectorValue($input->get->for); $taggedPages = $pages->find("…, showme=$showme, for=$for"); 2 Link to comment Share on other sites More sharing options...
a-ok Posted October 12, 2015 Author Share Posted October 12, 2015 Thanks for this help. I can work out how I would return this as a page (so have this in my template) but I'm unsure how it would work having this as a filter with a submit button? Would it be as simple as front end JS to use the values to create a URL? Or would there need to be server side code? If someone selects the two filters and presses 'Go' how would it know which URL to go to? Using JS I am assuming? Link to comment Share on other sites More sharing options...
elabx Posted October 12, 2015 Share Posted October 12, 2015 It could be done with or without JS, input fields in HTML that are part of a form object will be send as the values in the URL specified in the action attribute, which can be the same page from when you are sending the data. So how do you handle this vars in the server if there is data in the POST variables? In the same template used by your page with the form, you can have something like this: if($input->post){ echo $input->post->fieldName; //Etc } I learned a lot about forms in PW from this post from Soma. Link to comment Share on other sites More sharing options...
LostKobrakai Posted October 13, 2015 Share Posted October 13, 2015 I think the easier way would be using Javascript, but as elabx pointed out you could also use e.g. checkboxes behind those links, that get send to the server. 1 Link to comment Share on other sites More sharing options...
a-ok Posted October 14, 2015 Author Share Posted October 14, 2015 Thanks @LostKobrakai. I have come up with the following: <?php $sectors = $sanitizer->selectorValue($input->get->project_sectors); ?> <?php $tags = $sanitizer->selectorValue($input->get->project_tags); ?> <?php $results = $pages->find('parent=/project-library/, sort=sort, project_sectors=$sectors, project_tags=$tags'); ?> <?php foreach ($results as $result) : ?> <h1><?php echo $result->title; ?></h1> <?php endforeach; ?> However, I'm unsure what the $input should be changed to? The URL, for example, could be: www.domain.com/project-library-results/?project_sectors=architecture&project_tags=education So, am I right in thinking that based on what the user selects, it will build a URL using JS, then on submit, take you to this page and the $input will find the relevant items from the URL and build the result based on this? Link to comment Share on other sites More sharing options...
LostKobrakai Posted October 14, 2015 Share Posted October 14, 2015 It makes sure, that harmful parts that users may put as get variables are sanitized. E.g. '…?project_tags="; echo $superuser->pass;' Link to comment Share on other sites More sharing options...
a-ok Posted October 14, 2015 Author Share Posted October 14, 2015 Ah yes, okay. So essentially this is very similar to the search setup on PW. But instead the action in the form would be a custom URL based on what they have selected, rather than a specific template. Link to comment Share on other sites More sharing options...
a-ok Posted October 14, 2015 Author Share Posted October 14, 2015 Removed to clearer version below: Link to comment Share on other sites More sharing options...
a-ok Posted October 14, 2015 Author Share Posted October 14, 2015 This is my full setup. The idea is that the input value will be updated per click via JS, so they click 'Landscape Design' it'll update the value to 'landscape design' etc. <form class="project-library-filter" action="<?php echo $pages->get('template=project-library-results')->url; ?>" method="get"> <input type="text" name="project_sectors" value="architecture,urban-design" /> <button type="submit" name="submit">Go</button> </form> On my template page for the results the following error is shown: Error: Exception: Unknown Selector operator: '=$' -- was your selector value properly escaped? (in /Users/rich/Sites/Freelance/levitt-bernstein/wire/core/Selectors.php line 281) <?php $sectors = $sanitizer->selectorValue($input->get->project_sectors); ?> <?php $tags = $sanitizer->selectorValue($input->get->project_tags); ?> <?php $results = $pages->find('parent=/project-library/, sort=sort, project_sectors=$sectors, project_tags=$tags'); ?> <?php foreach ($results as $result) : ?> <h1><?php echo $result->title; ?></h1> <?php endforeach; ?> Any thoughts? Link to comment Share on other sites More sharing options...
LostKobrakai Posted October 14, 2015 Share Posted October 14, 2015 You need to use double quotes to be able to use inlined variables. Link to comment Share on other sites More sharing options...
a-ok Posted October 14, 2015 Author Share Posted October 14, 2015 I see. Thanks so much for the help. Link to comment Share on other sites More sharing options...
a-ok Posted October 14, 2015 Author Share Posted October 14, 2015 I meant to ask; if the user selects 'All', is there a value I can use for my project_sectors and/or project_tags that return them all? <?php $results = $pages->find("parent=/project-library/, sort=sort, project_sectors=$sectors, project_tags=$tags"); ?> Link to comment Share on other sites More sharing options...
LostKobrakai Posted October 14, 2015 Share Posted October 14, 2015 Not for selectors per se, but you can use this: <?php // Get url data $sectors = $sanitizer->selectorValue($input->get->project_sectors); $tags = $sanitizer->selectorValue($input->get->project_tags); // Build Selector $selector = "parent=/project-library/, sort=sort"; if($sectors != "all") $selector .= ", project_sectors=$sectors"; if($tags != "all") $selector .= ", project_tags=$tags"; // Find pages $results = $pages->find($selector); ?> <?php foreach ($results as $result) : ?> <h1><?php echo $result->title; ?></h1> <?php endforeach; ?> Link to comment Share on other sites More sharing options...
a-ok Posted October 14, 2015 Author Share Posted October 14, 2015 Thanks, man. It's good to know this going forward. Do you know why I get %2C in my URL once passed with the commas? Link to comment Share on other sites More sharing options...
a-ok Posted October 14, 2015 Author Share Posted October 14, 2015 And actually the default search does the same. So is %2C the correct thing for it to show in the URL? It's failing to bring back an item that has two project_sectors... Link to comment Share on other sites More sharing options...
a-ok Posted October 14, 2015 Author Share Posted October 14, 2015 I don't think multiple tags in the URL works? Link to comment Share on other sites More sharing options...
a-ok Posted October 14, 2015 Author Share Posted October 14, 2015 Anyone else with any idea why commas don't work in the URL? For example, this URL returns no results: http://localhost/Freelance/levitt-bernstein/project-library-results/?project_sectors=architecture,urban-design&project_tags=all&submit= but if I do each of the project_sectors separately: http://localhost/Freelance/levitt-bernstein/project-library-results/?project_sectors=architecture&project_tags=all&submit= http://localhost/Freelance/levitt-bernstein/project-library-results/?project_sectors=urban-design&project_tags=all&submit= They both return the same result... If I output $sectors with just one it results as architecture but if I do it when I have two it wraps it in quotes "architecture,urban-design" which I don't know is of any help? Furthermore, I think we have to use | rather than a comma to separate our values, so instead of $sectors outputting "architecture,urban-design" it would output architecture|urban-design, yes? However, | gets removed when it is sanitised so I'd have to remove that? Link to comment Share on other sites More sharing options...
LostKobrakai Posted October 14, 2015 Share Posted October 14, 2015 I'm still not sure why I had these commas in my examples. Most likely you need to explode the value you get from the url by the devider (comma or whatever) and loop over all options separately to sanitize them. 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