Jump to content

pass multiple url variables using OR operator


mishasoni

Recommended Posts

Hi -

 

Could someone enlighten me as to how to mass multiple url variables using an OR operator?  For example, I want to return results where $var1=X OR $var1=Y, but am not sure what the url string should look like.

 

www.mydomain.com/page.php?var1=...

 

Any help would be greatly appreciated!

I will clarify what I am trying to do...

 

I'm trying to create a drop down menu that returns only records where the 'NameLast' field begins with a given letter, A, B ,C ...etc, so that users can browse by last name.  I would like to group UVW and XYZ together in the menu options since there are very few records beginning with these letter, but am unsure how to go about doing this (I should disclose that I am a PHP newbie).  See code for the menu below.

 

//Contents for menu
var menu1=new Array()
menu1[0]='<a href="http://www.agci.org/programs/scientist_workshops_%3E_past_participants/participant_results.php?firstletter=A">A</a>'
menu1[1]='<a href="http://www.agci.org/programs/scientist_workshops_%3E_past_participants/participant_results.php?firstletter=B">B</a>'
menu1[2]='<a href="http://www.agci.org/programs/scientist_workshops_%3E_past_participants/participant_results.php?firstletter=C">C</a>'
menu1[3]='<a href="http://www.agci.org/programs/scientist_workshops_%3E_past_participants/participant_results.php?firstletter=D">D</a>'
menu1[4]='<a href="http://www.agci.org/programs/scientist_workshops_%3E_past_participants/participant_results.php?firstletter=E">E</a>'
menu1[5]='<a href="http://www.agci.org/programs/scientist_workshops_%3E_past_participants/participant_results.php?firstletter=F">F</a>'
menu1[6]='<a href="http://www.agci.org/programs/scientist_workshops_%3E_past_participants/participant_results.php?firstletter=G">G</a>'
menu1[7]='<a href="http://www.agci.org/programs/scientist_workshops_%3E_past_participants/participant_results.php?firstletter=H">H</a>'
menu1[8]='<a href="http://www.agci.org/programs/scientist_workshops_%3E_past_participants/participant_results.php?firstletter=I">I</a>'
menu1[9]='<a href="http://www.agci.org/programs/scientist_workshops_%3E_past_participants/participant_results.php?firstletter=J">J</a>'
menu1[10]='<a href="http://www.agci.org/programs/scientist_workshops_%3E_past_participants/participant_results.php?firstletter=K">K</a>'
menu1[11]='<a href="http://www.agci.org/programs/scientist_workshops_%3E_past_participants/participant_results.php?firstletter=L">L</a>'
menu1[12]='<a href="http://www.agci.org/programs/scientist_workshops_%3E_past_participants/participant_results.php?firstletter=M">M</a>'
menu1[13]='<a href="http://www.agci.org/programs/scientist_workshops_%3E_past_participants/participant_results.php?firstletter=N">N</a>'
menu1[14]='<a href="http://www.agci.org/programs/scientist_workshops_%3E_past_participants/participant_results.php?firstletter=O">O</a>'
menu1[15]='<a href="http://www.agci.org/programs/scientist_workshops_%3E_past_participants/participant_results.php?firstletter=P">P</a>'
menu1[16]='<a href="http://www.agci.org/programs/scientist_workshops_%3E_past_participants/participant_results.php?firstletter=Q">Q</a>'
menu1[17]='<a href="http://www.agci.org/programs/scientist_workshops_%3E_past_participants/participant_results.php?firstletter=R">R</a>'
menu1[18]='<a href="http://www.agci.org/programs/scientist_workshops_%3E_past_participants/participant_results.php?firstletter=S">S</a>'
menu1[19]='<a href="http://www.agci.org/programs/scientist_workshops_%3E_past_participants/participant_results.php?firstletter=T">T</a>'
menu1[20]='<a href="http://www.agci.org/programs/scientist_workshops_%3E_past_participants/participant_results.php?.....</a>' // should return all records beginning with U, V or W
menu1[21]='<a href="http://www.agci.org/programs/scientist_workshops_%3E_past_participants/participant_results.php?.....</a>' // should return all records beginning with X, Y, or Z

 

You'll use the following url instead

http://www.agci.org/programs/scientist_workshops_%3E_past_participants/participant_results.php?firstletter=UVW

 

Then in participant_results.php you'll check to see if $_GET['firstletter'] equals to UVW or XYZ if its does you'll grab all names that begin with U, V, W or X, Y, Z at the same time.

OK, now I need a bit of handholding on the code for the results page.  So far I have:

 

$query_rsParticipants = "SELECT * FROM Contacts WHERE ContactType = 'Scientist' AND ContactID_pk <> 0 ORDER BY NameLast ASC";
$firstletter = $_GET['firstletter']	;	
if (isset($firstletter) && $firstletter = UVW) {
  $query_rsParticipants = "SELECT * FROM Contacts WHERE ContactType = 'Scientist' AND ContactID_pk <> 0 AND NameLast LIKE 'U%' OR NameLast LIKE 'V%' OR NameLast LIKE 'W%' ORDER BY NameLast ASC";
}
elseif (isset($firstletter) && $firstletter = XYZ) {
  $query_rsParticipants = "SELECT * FROM Contacts WHERE ContactType = 'Scientist' AND ContactID_pk <> 0 AND NameLast LIKE 'X%' OR NameLast LIKE 'Y%' OR NameLast LIKE 'Z%' ORDER BY NameLast ASC";
  }
else {
$query_rsParticipants = "SELECT * FROM Contacts WHERE ContactType = 'Scientist' AND ContactID_pk <> 0 AND NameLast LIKE '$firstletter%' ORDER BY NameLast ASC";
} 

 

...but this doesn't work – it returns results for UVW regardless of what $firstletter is set to.  Any help? Is there a better way of accomplishing this?

 

Thanks.

When comparing variables use == not =

$search = null;

if(isset($_GET['firstletter']))
{
    $firstletter = $_GET['firstletter'];

    if($firstletter == 'UVW')
    {
        $search = " AND NameLast LIKE 'U%' OR NameLast LIKE 'U%' OR NameLast LIKE 'V%' OR NameLast LIKE 'W%'";
    }
    elseif($firstletter == 'XYZ')
    {
        $search = " AND NameLast LIKE 'U%' OR NameLast LIKE 'X%' OR NameLast LIKE 'Y%' OR NameLast LIKE 'Z%'";
    }
    elseif(in_array(strtolower($firstletter), range('a','z')))
    {
        $search = " AND NameLast LIKE '$firstletter%'";
    }
}

$query_rsParticipants = "SELECT * FROM Contacts WHERE ContactType = 'Scientist' AND ContactID_pk <> 0". $search ." ORDER BY NameLast ASC";

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.