0

Hi,

I need to show results with some filters on mysql query.

The filters will be taken from $_GET['filter']

There are going to be multiple combinations of filters.

Like : filter1+filter2, filter1+filter5, filter2+filter4 etc.

I think i need to create a switch() for these filters.

And based on the filters selected, run the query in sql.

I am not able to create an array for the $_GET[] variables.

Please assist.

3
Contributors
5
Replies
33
Views
4 Years
Discussion Span
Last Post by diafol
0

COuld you give us an idea of the data expected?

How about the database fields?

I am not able to create an array for the $_GET[] variables.

Yes you can. Give us more info.

0

Is the $_GET info not already automatically stored in an array? E.g. ?key=value becomes $_GET['key'] == 'value'

0

Thank you very much for your reply.

So here it is :

I have a webpage with 2 columns. All the results will be posted in the right column.

On the left column, i have the filters in <select>, <checkbox> and <radio> tabs.

Now depending on the values selected at the left side the data on the right column changes [The filters are applied to sql as WHERE].

These are the filters I am talking about.

So if nothing is selected/checked on the left side - Show all results.

if(isset($_GET['filter1'])) {

$filter1 = $_GET['filter1'];

SQL QUERY SELECT WHERE column.value = $filter1;

SHOW RESULTS.

}



if(isset($_GET['filter1']) && isset($_GET['filter2'])) {

$filter1 = $_GET['filter1'];
$filter2 = $_GET['filter2'];

SQL QUERY SELECT WHERE column1.value = $filter1 && column2.value = $filter2;

SHOW RESULTS.

}

Hope i described my expectation.

Thank you again

Edited by boney: removed excess <BR>

0

What about you loop through your $_GET keys, and if the key represents a filter, you add the filter to your filters list? For example:

<?php
if($_GET)
{
    // Create an array to store the filters that are to be used in.
    $filters = array();

    foreach($_GET as $key => $value)
    {
        if(strpos($key, 'filter') !== false)
        {
            // This key contains the word "filter". Add it to the filters array.
            $filters[] = $value;
        }
    }
}



if($filters)
{
    // Create a variable to store the WHERE clause in.
    $where_clause = '';

    // Create a counter for the foreach() loop.
    $i = 0;

    foreach($filters as $filter)
    {
        // Do whatever you want with this filter. For example, let's add it to your query:

        // We don't want to add the word "AND" to our first part of $where_clause.
        if($i === 0)
        {
            $add = null;
        }
        else
        {
            $add = ' AND ';
        }

        // Each filter is connected to its own colum, so let's use a switch to determine the
        // exact words that we need to add to our WHERE clause.
        switch($filter)
        {
            case 'filter1':
                $where_clause .= $and . ' column1.value = "filter1" ';
                break;

            case 'filter2':
                $where_clause .= $and . ' column2.value = "filter2" ';
                break;
        }

        // Add one to the foreach() counter.
        $i++;
    }
}



// Build the query.
$query = 'SELECT *
    FROM table
    WHERE ' . $where_clause;
0

Here's my take...

<?php
//CONNECTION DETAILS HERE or WITHIN the next block    

if(isset($_GET['filters']))
{
    $where = array();
    foreach($_GET['filters'] as $k=>$v)
    {
        if(!is_array($v))
        {
            $cleanv = trim(mysql_real_escape_string($v));
            if($cleanv) $where[] = "column$k.value = '$cleanv'";
        }else{
            $cleanv = array_map('mysql_real_escape_string', $v);
            $where[] = "column$k.value IN ('" . implode("','",$cleanv) . "')";
        } 
    }
    $whereClause = implode(" AND ", $where);
    //echo here - but can place into a QUERY instead
    echo $whereClause;
}
?>

<form>
<!--Optional/Required-->
<label for="word">Word:</label>
<select name="filters[1]" id="word">
    <option value="">(Choose a Word)</option>
    <option value="some">Some</option>
    <option value="thing">Thing</option>
    <option value="else">Else</option>
    <option value="in">In</option>
    <option value="here">Here</option>
</select>
<br />
<!--Optional-->
<input type="checkbox" name="filters[2][]" id="eggs" value="eggs" /><label for="eggs">Eggs</label>
<input type="checkbox" name="filters[2][]" id="bread" value="bread" /><label for="bread">Bread</label>
<input type="checkbox" name="filters[2][]" id="butter" value="butter" /><label for="butter">Butter</label>
<input type="checkbox" name="filters[2][]" id="soda" value="soda" /><label for="soda">Soda</label>
<br />
<!--Required-->
<input type="radio" name="filters[3]" id="book" value="book" checked="checked" /><label for="book">Book</label>
<input type="radio" name="filters[3]" id="magazine" value="magazine" /><label for="magazine">Magazine</label>
<input type="radio" name="filters[3]" id="kindle" value="kindle" /><label for="kindle">Kindle</label>
<input type="radio" name="filters[3]" id="newspaper" value="newspaper" /><label for="newspaper">Newspaper</label>
<br />
<input type="submit" value="Go" />
</form>

This assumes that the column for filtering has the same index as the filter in the form.
So the guts of this is only about 17 lines of code.

Edited by diafol

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.