I try to create a selection page, where different query are made, depending on selected checkboxes, and textboxes.

It works for most the part but I a selection between zipcode ranges and year of birth range (between nbs_zip and nbs_zip2 and between nbs_contact_year and nbs_contact_year2).

It works when I have for example nbs_zip and nbs_zip2 filled with a zipcode and only nbs_contact_year filled (not second nbs_contact_year2).
But I can't get the query to work when I enter in both zipcode1 and 2 and year1 and 2 (second elseif).

Here is part of the code I use (it has multiple ways for the selection).

if(!empty($_POST['nbs_zip2']))
{
    $sql_query = "SELECT * "
                      . " FROM nbs_contacts JOIN nbs_events "
                      . " ON nbs_contacts.nbs_contact_id = nbs_events.nbs_events_id "
                      . " WHERE nbs_relativeto LIKE '%$nbs_relativeto%' AND nbs_contact_year LIKE '%$nbs_contact_year%' AND nbs_year LIKE '%$nbs_year%' ";
{
    $sql_query .= " AND nbs_zip BETWEEN $nbs_zip AND $nbs_zip2 ";
}
    $sql_query .= " $sqlAND"; 
}
elseif(!empty($_POST['nbs_contact_year2']))
{
    $sql_query = "SELECT * "
                      . " FROM nbs_contacts JOIN nbs_events "
                      . " ON nbs_contacts.nbs_contact_id = nbs_events.nbs_events_id "
                      . " WHERE nbs_relativeto LIKE '%$nbs_relativeto%' AND nbs_zip LIKE '%$nbs_zip%' AND nbs_year LIKE '%$nbs_year%' ";
{
    $sql_query .= " AND nbs_contact_year BETWEEN $nbs_contact_year AND $nbs_contact_year2 ";
}
    $sql_query .= " $sqlAND"; 
}
elseif(!empty($_POST['nbs_contact_year2']) && !empty($_POST['nbs_zip2']))
{
    $sql_query = "SELECT * "
                      . " FROM nbs_contacts JOIN nbs_events "
                      . " ON nbs_contacts.nbs_contact_id = nbs_events.nbs_events_id "
                      . " WHERE nbs_relativeto LIKE '%$nbs_relativeto%' AND nbs_zip LIKE '%$nbs_zip%' AND nbs_year LIKE '%$nbs_year%' ";
{
    $sql_query .= " AND nbs_contact_year BETWEEN $nbs_contact_year AND $nbs_contact_year2 ";
}
{
    $sql_query .= " AND nbs_zip BETWEEN $nbs_zip AND $nbs_zip2 ";
}
    $sql_query .= " $sqlAND"; 
}
else
{
  $sql_query = "SELECT * "
                      . " FROM nbs_contacts JOIN nbs_events "
                      . " ON nbs_contacts.nbs_contact_id = nbs_events.nbs_events_id "
                      . " WHERE nbs_relativeto LIKE '%$nbs_relativeto%' AND nbs_contact_year LIKE '%$nbs_contact_year%' AND nbs_zip LIKE '%$nbs_zip%' AND nbs_year LIKE '%$nbs_year%' $sqlAND";
}

Please need help soon!

Thanks

But I can't get the query to work when I enter in both zipcode1 and 2 and year1 and 2 (second elseif).

When that condition is true, then the "if" and the first "elseif" are also true. Since they appear before elseif(!empty($_POST['nbs_contact_year2']) && !empty($_POST['nbs_zip2'])), they will execute first. You must move it up so that it appears first (it should be you initial "if" cluase).

I don't understand what you mean.

Can you post an example of what you mean?

Forget my last post, I think I solved it.
In the following code, the thing in red was wrong:

elseif(!empty($_POST['nbs_contact_year2']) && !empty($_POST['nbs_zip2']))
{
    $sql_query = "SELECT * "
                      . " FROM nbs_contacts JOIN nbs_events "
                      . " ON nbs_contacts.nbs_contact_id = nbs_events.nbs_events_id "
                      . " WHERE nbs_relativeto LIKE '%$nbs_relativeto%' AND nbs_zip LIKE '%$nbs_zip%' AND nbs_year LIKE '%$nbs_year%' ";
{
    $sql_query .= " AND nbs_contact_year BETWEEN $nbs_contact_year AND $nbs_contact_year2 ";
}
{
    $sql_query .= " AND nbs_zip BETWEEN $nbs_zip AND $nbs_zip2 ";
}
    $sql_query .= " $sqlAND"; 
}

It was twice in the query (the nbs_zip).

I changed it to:

if(!empty($_POST['nbs_contact_year2']) && !empty($_POST['nbs_zip2']))
{
    $sql_query = "SELECT * "
                      . " FROM nbs_contacts JOIN nbs_events "
                      . " ON nbs_contacts.nbs_contact_id = nbs_events.nbs_events_id "
                      . " WHERE nbs_relativeto LIKE '%$nbs_relativeto%' AND nbs_year LIKE '%$nbs_year%' ";
{
    $sql_query .= " AND nbs_contact_year BETWEEN $nbs_contact_year AND $nbs_contact_year2 AND nbs_zip BETWEEN $nbs_zip AND $nbs_zip2 ";
}
    $sql_query .= " $sqlAND"; 
}

Thanks for your help.

I don't understand what you mean.

On line 1 of your original post you have: if(!empty($_POST['nbs_zip2']) and then on line 23 you have: elseif(!empty($_POST['nbs_contact_year2']) && !empty($_POST['nbs_zip2'])) What I was saying is that when BOTH nbs_zip2 AND nbs_contact_year2 are filled, line 23 will not execute because the condition on line 1 is true! You need to give priority to the condition on line 23. In other words, line 23 should be THE if clause, NOT an elseif clause.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.