Jump to content

Multiple Page PageArray and Search Queries


ptjedi
 Share

Recommended Posts

Greetings,

I wonder if anyone can help me understand how to overcome this issue. I am building a simples catalog with 3 taxonomy: category (determined by hierarchical page), type and area (both are selected using PageArray dropdowns, meaning that the Options are "pages" which have attributes themselves.

Now, I want to create a search-like template that picks up the URL arguments, but I don't know how to set this correctly. I picked up the search.php example, and added category restriction, but now I want to also restrict by 'area'.

$matches = $pages->find("title|body|sidebar*=$q, parent=/catalogo/produtos/, arealimit=50, id!=1");

The area attribute has 4 items, all of them have title, description and a field called 'alias'. I would like to search by alias, and return all products that have that 'area' selected. The URL should look like this:

domain.com/catalog-search/?q=carro&area=RH

Now, I want to pickup the 'area alias' typed in the URL (which I already have), check to which 'area' this alias belongs, and use that area to filter my results. Since there can be multiple areas selected, how can that "search query" be built?

Thank you very much for any help.

  • Like 1
Link to comment
Share on other sites

$area_alias = $sanitizer->text($input->get->area);

$area_page = $pages->get("alias=$area_alias");

$matches = $pages->find("title|body|sidebar*=$q, parent=/catalogo/produtos/, arealimit=50, id!=1, area=$area_page");

I think this is what you want, but I'm not sure. Tell me if I misunderstood everything.

EDIT: See Ryan's corrections to my code some posts below

  • Like 2
Link to comment
Share on other sites

Diogo to the rescue. Once again :)

It works and I now understand how it's done. My problem is that I wasn't sure how to call this in the find query. The answer is that I need to match page objects and not titles, for example.

Muito obrigado!

Link to comment
Share on other sites

When possible, use IDs (integers) for referring to pages in forms. Then your sanitization is as simple as this:

$area_alias = (int) $input->get->area; 

There's a problem with this code segment:

$area_alias = $sanitizer->text($input->get->area);

Someone could inject more selectors into the string by specifying it in the URL for area_alias. The text() sanitizer isn't meant to sanitize strings for use in a selector. Instead, you'd want to use the selectorValue sanitizer:

$area_alias = $sanitizer->selectorValue($input->get->area); 

But like mentioned at the beginning, I prefer to use integers (IDs) for anything referencing pages, since it makes the sanitization simple and bulletproof.

  • Like 2
Link to comment
Share on other sites

Thanks to Ryan as well.

I have another question though. If I have an URL like this:

pesquisa-de-catalogo/?area=HL&type=passboxes

and if I want to specify ANY area and leave the restraint only to the type, this returns zero results:

pesquisa-de-catalogo/?area=&type=passboxes

What's the better way to achieve that feature?

Link to comment
Share on other sites

Just solved it. Was rather simple:

if($area_alias){ $area_page_string = "catalog_product_area=$area_page,";}else{$area_page_string="";}
if($tipo_name){ $type_page_string = "catalog_product_type=$type_page,";}else{$type_page_string="";}

$matches = $pages->find("parent=/catalogo/produtos/,".$area_page_string.$type_page_string." limit=50, id!=1"); 

Thanks!

  • Like 1
Link to comment
Share on other sites

Thats's very similar to what I usually do. I usually just have a $selector variable and then keep appending to that one:

$selector = "parent=catalogo/productos/, limit=50, id>1";
if($area_alias) $selector .= ", catalog_product_area=$area_page";
if($tipo_name) $selector .= ", catalog_product_type=$type_page";
$matches = $pages->find($selector); 

Not that this is preferable to what you are doing, just an alternate way of the same thing.

  • Like 1
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...