Jump to content

View multiple tags at any one time, rather than just one...


a-ok
 Share

Recommended Posts

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

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");
  • Like 1
Link to comment
Share on other sites

  • 1 month later...

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


<?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");

  • Like 2
Link to comment
Share on other sites

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

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

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

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

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

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

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

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
 Share

×
×
  • Create New...