skip to Main Content

I am trying to build an SQL search from input field "postcode" with multiple zipcode.

My input field is working like that: user add different zipcode (french),
the value of the input (name = postcode) is: value="35200, 53333, 23400"
the URL (post) after submit is: &postcode=35200%2C53333%2C23400

Now the query PHP part (working with only one postcode added):

$postcode = (!empty($array['postcode']) && !is_array($array['postcode']) AND $array['postcode'] != $language['value_postcode']) ? $bdd->quote($array['postcode']) : 0;
$condition .= (!empty($postcode)) ? " AND s.postcode = $postcode" : "";

This is working with ONLY ONE ZIPCODE …

If I add an other zip code, the request is not working.
What to do to change the request working with multiple zipcode?

2

Answers


  1. I assume you have already fetched the multiselect input as

    value="44000, 75000, 85000"
    

    So now, you only need to convert the string value $postcode into array as

    $postcode_arr = explode(",", $postcode);
    

    This way all your multiple values are accessible in $postcode_arr array for next requirement.

    Reference link:
    https://www.php.net/manual/en/function.explode.php

    Otherwise, you have not added the question properly. So, I suggest you to add full code having database query generated from your form/data collector.
    Then only, one can debug for any logical issue there may exist and depending that we can help you fix the issue.

    Login or Signup to reply.
  2. As you are already having comma separated values value="35200, 53333, 23400"

    you can directly give a try with IN operator

    $condition .= "AND s.postcode IN ($postcode)";
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search