0

I have a search on a site which I only want to display any results if something has been entered into one of the fields. I'm not really a php person so the best I could do was by adding this before the search:

<?php code on how to get the listings ... 

if ($search_location != "" || $search_keywords != ""  ) {

... code on how to display the listings.} ?>

As far as I understand this will only display the results if either the location or keywords are not empty. Works well so far.

My problem: there is also a dropdown to select a category ... I tried adding this like:

if ($search_location != "" || $search_keywords != "" || $search_categories != ""  ) {

But the search doesn't work if you only choose a category you have to enter in one of the other fields also, so I am assuming this was wrong.

2 Answers 2

0

I think you need AND (which is && in php):

if (($search_location != "" || $search_keywords != "") && $search_categories != ""  ) {
//                                                     ^ here

Which means:

provide (search_location or search_keywords) and search_categories

4
  • Thanks for the response. I want you to be able to fill in only one of any three. Either location, keyword or category, but the code I tried means you need to fill in at least one of location or keywords for the category one to filter. Hope this is clearer :)
    – Elizabeth
    Jul 23, 2016 at 19:01
  • If you need any of three then your original code is fine.
    – u_mulder
    Jul 23, 2016 at 19:03
  • That's what I thought, but its not working. The search is run by Ajax, do you think that is causing the problem. Oh, another thing ... probably should have mentioned, its a dynamic dropdown which depends on categories added by users.
    – Elizabeth
    Jul 23, 2016 at 19:08
  • Ok, got there in the end. I had to use !empty($search_categories) ) I don't know why !empty works and != "" didn't but it's all good so I can't complain. Thanks for the help. Liz.
    – Elizabeth
    Jul 23, 2016 at 22:20
0

Just posting the answer I got incase any one else needs it.

I had to use !empty like -

if ($search_location != "" || $search_keywords != "" || !empty($search_categories) ) {

I don't know why !empty works and != "" doesn't, still learning.

Thanks. Liz.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.