Skip to content
Advertisement

Prevent duplication in a PHP filter

So I have an another problem with my filtering system. I am getting my filtered results when I choose an option from the first dropdown I have. But I also get below the filtered results a block where I see my options from the second dropdown (duplicated) and below that I have again ALL of my results I have in the database. How can I prevent this?

This is my Filter now

Here is my index.php

JavaScript

And this is my ajaxdata.php:

JavaScript

What am I doing wrong so this shows everything when I am filtering

Advertisement

Answer

For the first problem (returning an unfiltered set of results as well as the filtered set), it’s simply that you are running two versions of the SELECT * FROM partner... query, and outputting the results from both. The first version is filtered, but the second version isn’t. You just need to remove the second version – it’s not clear why that’s there to begin with.

For the second problem (outputting the dropdown options after the results), the issue is that you’re just outputting one big stream of HTML. Then you’ve written $('#subcategory').html(html); $('#partner').html(html); which attempts to put the same single stream of HTML into both parts of your page! The first command likely doesn’t entirely work correctly because you’re trying to put divs inside a select. And the second one prints everything that was output by the PHP – it has no way of telling which bit you wanted to print there, and which bit was intended for the dropdown. You gave it no way to differentiate.

It would make more sense to return a JSON object from the PHP, with two properties – one property containing the HTML for the search result, and one containing the HTML for the dropdown. Then the Javascript can easily read the correct bit into each part of your page separately.

Something like this should work I think (although obviously it’s not easy for me to test it):

JavaScript:

JavaScript

PHP:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement