2

I'm using a URL query string to filter MySQL search results. When a user clicks on one of the links the query along with another variable is passed to the application, which then builds and executes a SQL query to the database.

The filtering works - the user can filter by genre, platform and can also sort at the same time, but the problem is that every time they click on a link it adds another variable at the end of $query.

For example if the user types in example and selects as filter, the genre action, the query looks like this:

keywords=example&genre=action

But lets say they then click on the adventure genre, the query then looks like this:

keywords=example&genre=action&genre=adventure

Is there a way to get the query to replace a variable if it isalready set?

$query = mysql_real_escape_string(htmlentities(trim($_SERVER[QUERY_STRING]))); 

<div id="filter_nav">
        <ul  id="nav_form">
            <li><h3 id="h3">Genre: &nbsp;</h3>
            <li><a href="search.php?'.$query.'&genre=Fighting">Fighting</a></li>
            <li><a href="search.php?'.$query.'&genre=Role-Playing">Role-Playing</a></li>
            <li><a href="search.php?'.$query.'&genre=Action">Action</a></li>
        </ul>
        <ul  id="nav_form">
            <li><h3 id="h3">Platform: &nbsp;</h3>
            <li><a href="search.php?'.$query.'&platform=Playstation 3">PS3</a></li>
            <li><a href="search.php?'.$query.'&platform=xbox 360">Xbox 360</a></li>
            <li><a href="search.php?'.$query.'&platform=Gamecube">Gamecube</a></li>
        </ul>
        </div>
        ';
        echo '
        <ul  id="sorting_form">
            <li><h3 id="h3">SORT BY: &nbsp;</h3>
            <li><a href="search.php?'.$query.'&order=title">Title</a></li>
            <li><a href="search.php?'.$query.'&order=release_date">Date</a></li>
            <li><a href="search.php?'.$query.'&order=rating">Rating</a></li>

        </ul>
        ';

function search_results($keywords){
$returned_results = array();
$where = "";

$keywords = preg_split('/[\s]+/', $keywords);
$total_keywords = count($keywords);

foreach($keywords as $key=>$keyword){
    $where .= "title LIKE '%$keyword%'";
    if($key != ($total_keywords - 1)){
        $where .= " AND ";
    }   
}
if (isset($_GET['platform']) && !empty($_GET['platform'])){
    $platform = mysql_real_escape_string(htmlentities(trim($_GET['platform']))); 
        $where .= " AND platform='$platform'";
    }

if (isset($_GET['genre']) && !empty($_GET['genre'])){
    $genre = mysql_real_escape_string(htmlentities(trim($_GET['genre']))); 
                $where .= " AND genre='$genre'";
}

if (isset($_GET['order']) && !empty($_GET['order'])){
    $order = mysql_real_escape_string(htmlentities(trim($_GET['order'])));
    $where .= " ORDER BY $order DESC";
    }

$results ="SELECT * FROM games WHERE $where ";
3
  • 2
    you prevent that by properly buildiong the new URL. Commented May 5, 2012 at 16:36
  • possible duplicate of keeping url parameters during pagination Commented May 5, 2012 at 16:38
  • Thanks i'll check out the post. I've been searching for a while i guess i was using wrong keywords. Commented May 5, 2012 at 16:50

2 Answers 2

2

Using your code it can be done de-constructing the query string using parse_url and rebuilding it using http_build_query for every link.

However, personally I would just go for a form with 3 select boxes where the previously selected values are pre-selected.

You could put all your selection options in one multi-dimensional array and do a double loop.

Example:

<?php
$options = array(
  "genre" => array("Fighting", "Role-Playing", ...),
  ...
);
foreach $options as $key => $value)
{
?>
<select name="<?php echo $key; ?>">
<?php 
  foreach ($value as $item)
  {
    // echo option for item and mark it selected if necessary
  }
?>
</select>
<?php
}
?>
Sign up to request clarification or add additional context in comments.

1 Comment

Interesting, i'll try it out and get back to you. Thanks.
-1

Modify every <a> tag like: <a href="search.php?'.$query.'&genre=Fighting">Fighting</a> to <a href="search.php?&genre=Fighting">Fighting</a>

I.e remove '.$query.' part.

2 Comments

Wouldn't that make you loose all the other parameters?
is there a way to get the query to replace the variable if its already set?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.