Jump to content

Filter results via multiple dropdowns


3fingers
 Share

Recommended Posts

Hello all,

I'm learning more on Processwire and php in general, and today I'm struggling over this idea:

Having multiple <select> inputs to be able to query from database different criterias and echo them on the page.

Let's say

  • I got some registered users
  • Each of them have a gender, an age, and a color of hair. // ...so i want 3 <select> inputs
  • I'd like to query for example : SELECT * from users WHERE user_gender="male" AND user_age="25" AND user_hair="black"
  • The query above is the result of the combination of the 3 <select> inputs

I've looked at this tutorial made by Jeffrey Way (part1 and part2) where he explains (expecially in part1) how to query a database through a single <select> input.

Does anybody here have some insights to make me get closer to this?

Thanks!

Link to comment
Share on other sites

That's an "easy" task in Pw because you don't have to write SQL but find your stuff with selectors.

Basically you just throw your data that was sent with the selects in a Pw selector to get your pages:

// In template (HTML)
<form method="GET" action="<?= $page->url ?>">
<select name="gender">
//.. options
</select>

<select name="color_hair">
//.. options
</select>

<select name="age">
//.. options
</select>

</form>

// In template, above output

if (count($input->get)) {
  // Get the criteria and sanitize them
  $gender = $sanitizer->selectorValue($input->get->gender);
  $color_hair = $sanitizer->selectorValue($input->get->color_hair);
  $age = $sanitizer->selectorValue($input->get->age);
  // Search pages
  $p = $pages->find("template=user, gender=$gender, color_hair=$color_hair, age=$age");
}

This is a simple example. If you have some select fields optional, you'd need simple logic to check if the data was sent.

And just add it to the selector if the user submitted the criteria.

  • Like 2
Link to comment
Share on other sites

Thanks guys, you're both right!

@Wanze: I completely understand your code and I find it really simple, but I'm still not capable to write it down by myself with ease....practicing is the answer :)

@Macrura : I'm going to check it right now.

Next assignment to me (but probably too complicated at the moment) : How to implement a private messaging system to a processire site...hard topic.

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...