Query String Filter from Drop-Down Menus

12 posts by 2 authors in: Forums > CMS Builder
Last Post: December 1, 2014   (RSS)

By Perchpole - November 28, 2014

Hello, All

I don't know if this one has been discussed before but I think someone must have raised it...

I'm trying to put together a filter menu for a picture gallery using 3 drop-downs. The user selects the values of choice and then hits the submit button at which point the page reloads showing only the filtered results.

I want to do this by building a query string with the values from the 3 drop downs. I have an idea I may need to use jScript to do this but I'm not sure how to put it together.

My understanding tells me I can grab the values from the drop downs using something like this...

var ddval1 = $("#dropdown1").val();
var ddval2 = $("#dropdown2").val();
var ddval3 = $("#dropdown3").val();

Then I can compile the query string with code like this...

document.location.href = './index.php' + ddval1 + ddval2 + ddval3;

But where it all goes and how the submit button comes into things has me scratching my head!
 
 If anyone has some advice I'd be most grateful.
 
 :0)
 
 Perch

By claire - December 1, 2014

Hi Perch

You don't really need to use JS to compile the query string here. It's easier to implement this as a form with three select dropdowns and a submit button.

Something like this:

<form action="" method="get">
    <select name="select1">
        <option value="v1">Value 1</option>
        <option value="v2">Value 2</option>
        <option value="v3">Value 3</option>
    </select>
    <select name="select2">
        <option value="v4">Value 4</option>
        <option value="v5">Value 5</option>
        <option value="v6">Value 6</option>
    </select>
    <select name="select3">
        <option value="v7">Value 7</option>
        <option value="v8">Value 8</option>
        <option value="v9">Value 9</option>
    </select>
    <input type="submit" value="Search" />
</form>

This will submit the variables on the end of the URL like so:

http://www.yoursite.com/thispage.php?select1=v1&select2=v4&select3=v7

This is on the same page, so really all you need to do is check to see if these variables appear in the $_REQUEST array and incorporate them into your where parameter in getRecords.

Does that make sense?

--------------------

Claire Ryan
interactivetools.com

Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

By Perchpole - December 1, 2014

Hi, Claire -

Thanks for this. Just one question, what happens if one of the values is left blank - i.e. nothing selected? Do I need to make provision for that in the code?

Perch

By claire - December 1, 2014

Hi Perch

Yes, you would. I'd do the check like this:

if(@$_REQUEST['select1'] != "") {
    ....
}

This checks if the select1 variable is present and actually filled in. Assuming it passes that, the if statement should contain the code to add select1 to the where parameter.

--------------------

Claire Ryan
interactivetools.com

Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

By Perchpole - December 1, 2014

Assuming it passes that, the if statement should contain the code to add select1 to the where parameter.

But we're not passing the value to the where parameter!

:0/

Perch

By claire - December 1, 2014

Hah, okay. If you're not sure what that should look like, copy your code in here and I can take a look.

--------------------

Claire Ryan
interactivetools.com

Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

By Perchpole - December 1, 2014

Hi, Claire -

I've made some progress - but not quite nailed it! I'm trying to pass the values to the getRecords where parameter as you suggested:

'where' => $where

I'm having difficulty constructing the clause using if statements when there are multiple possibilities, such as the following query string:

/gallery.php?user=&category=4

I could just string a load of if statements one after another to cover every eventuality - but that seems clunky. Is there a better way?

Perch

By claire - December 1, 2014

Welcome to the wonderful world of PHP logic :) you can and will tie yourself into knots over this stuff.

Paste in the whole chunk of code here, I might know of a better way to handle it.

--------------------

Claire Ryan
interactivetools.com

Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

By Perchpole - December 1, 2014

Hi, Claire -

Here's an example - based on the query string above:

/gallery.php?user=&category=4

It only uses 2 parameters. I want to use 3...!

if (@$_REQUEST['user'] && @$_REQUEST['category']) {
$userNum = @$_REQUEST['user'];
$catNum = @$_REQUEST['category'];
$where = " tableName = 'pages' &&  fieldName = 'gallery' && info4 = $catNum && info5 = $userNum ";
}
elseif (@$_REQUEST['user'] && @!$_REQUEST['category']) {
$userNum = @$_REQUEST['user'];
$where = " tableName = 'pages' &&  fieldName = 'gallery' && info5 = $userNum ";
}
elseif (@!$_REQUEST['user'] && @$_REQUEST['category']) {
$catNum = @$_REQUEST['category'];
$where = " tableName = 'pages' &&  fieldName = 'gallery' && info4 = $catNum ";
}
else
{
$where = " tableName = 'pages' &&  fieldName = 'gallery' ";    
}


There must be a simpler, cleaner way?!

:0/

Perch