Page 1 of 1

Help using multiple drop down menus to refine a MySQL search

Posted: Thu Jul 07, 2005 9:59 am
by clangro
I've decided to put this in the Code forum rather than database because I know how SQL works (how to write a statement to do what I want), I just don't know how to get it to work well with PHP.

Here's the deal. I'm a PHP newbie. Well, I'm a coding newbie, period. I'm the technical support/network administrator in my company, but I've been given the task of creating a very basic search page for people to browse our products. What they want are drop down boxes for people to search by. Right now, we have 3: region, equipment type and company. All I've gotten to work so far is to search just by region, not the others.

I need to do two things:

I need to refine my code so that an "ALL" option is included for each drop down menu. Let's say you want everything in the south region. You select south, then all and all and it will give you all products that are just in the south.

I also need to be able to refine the search to include searching by 1 of any field or 2 fields. Meaning if you just want cable equipment type, it gives just that. Or if you want cable equipment type in the south region, it gives just that.

Here is the code I have and here is a test page on which it goes:

http://acscable.com/test/test.php

Code: Select all

<html>
	<head>  
		<title>PHP Test</title>
	</head>
	<body>
		<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="GET">
		<select name="region">
			<option value="S">South</option>
			<option value=&quote;N&quote;&gt;North&lt;/option&gt;
			&lt;option value=&quote;C&quote;&gt;Central&lt;/option&gt;
			&lt;option value=&quote;W&quote;&gt;West&lt;/option&gt;
		&lt;/select&gt;
		&lt;?
			$username=&quote;ABC&quote;;
			$password=&quote;123&quote;;
			$database=&quote;acscable_tmobile&quote;;
			mysql_connect(localhost,$username,$password);
			@mysql_select_db($database) or die( &quote;Unable to select database&quote;);

			if ($_GET&#1111;&quote;region&quote;] &lt;&gt; &quote;&quote;)
			{
				$query=&quote;select * from tmobile where region = '&quote; . $_GET&#1111;&quote;region&quote;] . &quote;'&quote;;
				$result=mysql_query($query);
				$num=mysql_numrows($result);
				mysql_close();
				$i=0;
				while ($i &lt; $num) 
					{
					$region=mysql_result($result,$i,&quote;region&quote;);
					$manu_part_num=mysql_result($result,$i,&quote;manu_part_num&quote;);
					$acs_part_num=mysql_result($result,$i,&quote;acs_part_num&quote;);
					$equipment_type=mysql_result($result,$i,&quote;equipment_type&quote;);
					$project=mysql_result($result,$i,&quote;project&quote;);
					$description=mysql_result($result,$i,&quote;description&quote;);
					echo &quote;&lt;br&gt;&lt;br&gt;&lt;b&gt;Region:&lt;/b&gt; $region &lt;br&gt;&lt;b&gt;Manufacturer Part #:&lt;/b&gt; $manu_part_num&lt;br&gt;&lt;b&gt;ACS Part #:&lt;/b&gt; $acs_part_num&lt;br&gt;&lt;b&gt;Equipment Type&lt;/b&gt;: $equipment_type&lt;br&gt;&lt;b&gt;Project:&lt;/b&gt; $project&lt;br&gt;&lt;b&gt;Description:&lt;/b&gt; $description&lt;br&gt;&lt;hr&gt;&lt;br&gt;&quote;;    
					$i++;
					}
			}
		?&gt;
		&lt;select name=&quote;Equipment Type&quote;&gt;
			&lt;option value=&quote;SB&quote;&gt;Nokia SB Equipment&lt;/option&gt;
			&lt;option value=&quote;HYBL&quote;&gt;HyperLink Blue&lt;/option&gt;
			&lt;option value=&quote;C&quote;&gt;Cabling&lt;/option&gt;
		&lt;/select&gt;
		&lt;?
			if ($_GET&#1111;&quote;equipment_type&quote;] &lt;&gt; &quote;&quote;)
				{
				$query=&quote;select * from tmobile where equipment_type = '&quote; . $_GET&#1111;&quote;equipment_type&quote;] . &quote;'&quote;;
				$result=mysql_query($query);
				$num=mysql_numrows($result);
				mysql_close();
				$i=0;
				while ($i &lt; $num) 
					{
					$region=mysql_result($result,$i,&quote;region&quote;);
					$manu_part_num=mysql_result($result,$i,&quote;manu_part_num&quote;);
					$acs_part_num=mysql_result($result,$i,&quote;acs_part_num&quote;);
					$equipment_type=mysql_result($result,$i,&quote;equipment_type&quote;);
					$project=mysql_result($result,$i,&quote;project&quote;);
					$description=mysql_result($result,$i,&quote;description&quote;);
					echo &quote;&lt;br&gt;&lt;br&gt;&lt;b&gt;Region:&lt;/b&gt; $region &lt;br&gt;&lt;b&gt;Manufacturer Part #:&lt;/b&gt; $manu_part_num&lt;br&gt;&lt;b&gt;ACS Part #:&lt;/b&gt; $acs_part_num&lt;br&gt;&lt;b&gt;Equipment Type&lt;/b&gt;: $equipment_type&lt;br&gt;&lt;b&gt;Project:&lt;/b&gt; $project&lt;br&gt;&lt;b&gt;Description:&lt;/b&gt; $description&lt;br&gt;&lt;hr&gt;&lt;br&gt;&quote;;    
					$i++;
					}
				}
		?&gt;
		&lt;select name=&quote;Project&quote;&gt;
			&lt;option value=&quote;Ericson&quote;&gt;Ericson&lt;/option&gt;
			&lt;option value=&quote;Nokia&quote;&gt;Nokia&lt;/option&gt;
		&lt;/select&gt;
		&lt;INPUT TYPE=SUBMIT VALUE=&quote;Submit&quote;&gt;
		&lt;?
			if ($_GET&#1111;&quote;project&quote;] &lt;&gt; &quote;&quote;)
			{
			$query=&quote;select * from tmobile where project = '&quote; . $_GET&#1111;&quote;project&quote;] . &quote;'&quote;;
			$result=mysql_query($query);
			$num=mysql_numrows($result);
			mysql_close();
			$i=0;
			while ($i &lt; $num) 
				{
				$region=mysql_result($result,$i,&quote;region&quote;);
				$manu_part_num=mysql_result($result,$i,&quote;manu_part_num&quote;);
				$acs_part_num=mysql_result($result,$i,&quote;acs_part_num&quote;);
				$equipment_type=mysql_result($res's the deal. I'm a PHP newbie. Well, I'm a coding newbie, period. I'm the technical support/network administrator in my company, but I've been given the task of creating a very basic search page for people to browse our products. What they want are drop down boxes for people to search by. Right now, we have 3: region, equipment type and company. All I've gotten to work so far is to search just by region, not the others.

I need to do two things:

I need to refine my code so that an "ALL" option is included for each drop down menu. Let's say you want everything in the south region. You select south, then all and all and it will give you all products that are just in the south.

I also need to be able to refine the search to include searching by 1 of any field or 2 fields. Meaning if you just want cable equipment type, it gives just that. Or if you want cable equipment type in the south region, it gives just that.

Here is the code I have and here is a test page on which it goes:

http://acscable.com/test/test.php

Code: Select all

<html>
	<head>  
		<title>PHP Test</title>
	</head>
	<body>
		<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="GET">
		<select name="region">
			<option value="S">South</option>
			<option value="N">North</option>
			<option value="C">Central</option>
			<option value="W">West</option>
		</select>
		<?
			$username="ABC";
			$password="123";
			$database="acscable_tmobile";
			mysql_connect(localhost,$username,$password);
			@mysql_select_db($database) or die( "Unable to select database");

			if ($_GET["region"] <> "")
			{
				$query="select * from tmobile where region = '" . $_GET["region"] . "'";
				$result=mysql_query($query);
				$num=mysql_numrows($result);
				mysql_close();
				$i=0;
				while ($i < $num) 
					{
					$region=mysql_result($result,$i,"region");
					$manu_part_num=mysql_result($result,$i,"manu_part_num");
					$acs_part_num=mysql_result($result,$i,"acs_part_num");
					$equipment_type=mysql_result($result,$i,"equipment_type");
					$project=mysql_result($result,$i,"project");
					$description=mysql_result($result,$i,"description");
					echo "<br><br><b>Region:</b> $region <br><b>Manufacturer Part #:</b> $manu_part_num<br><b>ACS Part #:</b> $acs_part_num<br><b>Equipment Type</b>: $equipment_type<br><b>Project:</b> $project<br><b>Description:</b> $description<br><hr><br>";    
					$i++;
					}
			}
		?>
		<select name="Equipment Type">
			<option value="SB">Nokia SB Equipment</option>
			<option value="HYBL">HyperLink Blue</option>
			<option value="C">Cabling</option>
		</select>
		<?
			if ($_GET["equipment_type"] <> "")
				{
				$query="select * from tmobile where equipment_type = '" . $_GET["equipment_type"] . "'";
				$result=mysql_query($query);
				$num=mysql_numrows($result);
				mysql_close();
				$i=0;
				while ($i < $num) 
					{
					$region=mysql_result($result,$i,"region");
					$manu_part_num=mysql_result($result,$i,"manu_part_num");
					$acs_part_num=mysql_result($result,$i,"acs_part_num");
					$equipment_type=mysql_result($result,$i,"equipment_type");
					$project=mysql_result($result,$i,"project");
					$description=mysql_result($result,$i,"description");
					echo "<br><br><b>Region:</b> $region <br><b>Manufacturer Part #:</b> $manu_part_num<br><b>ACS Part #:</b> $acs_part_num<br><b>Equipment Type</b>: $equipment_type<br><b>Project:</b> $project<br><b>Description:</b> $description<br><hr><br>";    
					$i++;
					}
				}
		?>
		<select name="Project">
			<option value="Ericson">Ericson</option>
			<option value="Nokia">Nokia</option>
		</select>
		<INPUT TYPE=SUBMIT VALUE="Submit">
		<?
			if ($_GET["project"] <> "")
			{
			$query="select * from tmobile where project = '" . $_GET["project"] . "'";
			$result=mysql_query($query);
			$num=mysql_numrows($result);
			mysql_close();
			$i=0;
			while ($i < $num) 
				{
				$region=mysql_result($result,$i,"region");
				$manu_part_num=mysql_result($result,$i,"manu_part_num");
				$acs_part_num=mysql_result($result,$i,"acs_part_num");
				$equipment_type=mysql_result($result,$i,"equipment_type");
				$project=mysql_result($result,$i,"project");
				$description=mysql_result($result,$i,"description");
				echo "<br><br><b>Region:</b> $region <br><b>Manufacturer Part #:</b> $manu_part_num<br><b>ACS Part #:</b> $acs_part_num<br><b>Equipment Type</b>: $equipment_type<br><b>Project:</b> $project<br><b>Description:</b> $description<br><hr><br>";    
				$i++;
				}
			}
		?>
</body>
</html>
I'm stuck. All the PHP tutorials I see don't cover drop down menus, mainly just text inputs. And even then, I'm so new to this I still need help.

Thanks in Advance,

Chris

P.S - I created a user specifically available to the public with only select privileges, so I should be safe from exploits.

JCART | Please use

Code: Select all

tags when posting php code. Review [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/colorthe south region. You select south, then all and all and it will give you all products that are just in the south.

I also need to be able to refine the search to include searching by 1 of any field or 2 fields. Meaning if you just want cable equipment type, it gives just that. Or if you want cable equipment type in the south region, it gives just that.

Here is the code I have and here is a test page on which it goes:

http://acscable.com/test/test.php

Code: Select all

&lt;html&gt;
	&lt;head&gt;  
		&lt;title&gt;PHP Test&lt;/title&gt;
	&lt;/head&gt;
	&lt;body&gt;
		&lt;form action=&quote;&lt;?php echo $_SERVER&#1111;'PHP_SELF'];?&gt;&quote; method=&quote;GET&quote;&gt;
		&lt;select name=&quote;region&quote;&gt;
			&lt;option value=&quote;S&quote;&gt;South&lt;/option&gt;
			&lt;option value=&quote;N&quote;&gt;North&lt;/option&gt;
			&lt;option value=&quote;C&quote;&gt;Central&lt;/option&gt;
			&lt;option value=&quote;W&quote;&gt;West&lt;/option&gt;
		&lt;/select&gt;
		&lt;?
			$username=&quote;ABC&quote;;
			$password=&quote;123&quote;;
			$database=&quote;acscable_tmobile&quote;;
			mysql_connect(localhost,$username,$password);
			@mysql_select_db($database) or die( &quote;Unable to select database&quote;);

			if ($_GET&#1111;&quote;region&quote;] &lt;&gt; &quote;&quote;)
			{
				$query=&quote;select * from tmobile where region = '&quote; . $_GET&#1111;&quote;region&quote;] . &quote;'&quote;;
				$result=mysql_query($query);
				$num=mysql_numrows($result);
				mysql_close();
				$i=0;
				while ($i &lt; $num) 
					{
					$region=mysql_result($result,$i,&quote;region&quote;);
					$manu_part_num=mysql_result($result,$i,&quote;manu_part_num&quote;);
					$acs_part_num=mysql_result($result,$i,&quote;acs_part_num&quote;);
					$equipment_type=mysql_result($result,$i,&quote;equipment_type&quote;);
					$project=mysql_result($result,$i,&quote;project&quote;);
					$description=mysql_result($result,$i,&quote;description&quote;);
					echo &quote;&lt;br&gt;&lt;br&gt;&lt;b&gt;Region:&lt;/b&gt; $region &lt;br&gt;&lt;b&gt;Manufacturer Part #:&lt;/b&gt; $manu_part_num&lt;br&gt;&lt;b&gt;ACS Part #:&lt;/b&gt; $acs_part_num&lt;br&gt;&lt;b&gt;Equipment Type&lt;/b&gt;: $equipment_type&lt;br&gt;&lt;b&gt;Project:&lt;/b&gt; $project&lt;br&gt;&lt;b&gt;Description:&lt;/b&gt; $description&lt;br&gt;&lt;hr&gt;&lt;br&gt;&quote;;    
					$i++;
					}
			}
		?&gt;
		&lt;select name=&quote;Equipment Type&quote;&gt;
			&lt;option value=&quote;SB&quote;&gt;Nokia SB Equipment&lt;/option&gt;
			&lt;option value=&quote;HYBL&quote;&gt;HyperLink Blue&lt;/option&gt;
			&lt;option value=&quote;C&quote;&gt;Cabling&lt;/option&gt;
		&lt;/select&gt;
		&lt;?
			if ($_GET&#1111;&quote;equipment_type&quote;] &lt;&gt; &quote;&quote;)
				{
				$query=&quote;select * from tmobile where equipment_type = '&quote; . $_GET&#1111;&quote;equipment_type&quote;] . &quote;'&quote;;
				$result=mysql_query($query);
				$num=mysql_numrows($result);
				mysql_close();
				$i=0;
				while ($i &lt; $num) 
					{
					$region=mysql_result($result,$i,&quote;region&quote;);
					$manu_part_num=mysql_result($result,$i,&quote;manu_part_num&quote;);
					$acs_part_num=mysql_result($result,$i,&quote;acs_part_num&quote;);
					$equipment_type=mysql_result($result,$i,&quote;equipment_type&quote;);
					$project=mysql_result($result,$i,&quote;project&quote;);
					$description=mysql_result($result,$i,&quote;description&quote;);
					echo &quote;&lt;br&gt;&lt;br&gt;&lt;b&gt;Region:&lt;/b&gt; $region &lt;br&gt;&lt;b&gt;Manufacturer Part #:&lt;/b&gt; $manu_part_num&lt;br&gt;&lt;b&gt;ACS Part #:&lt;/b&gt; $acs_part_num&lt;br&gt;&lt;b&gt;Equipment Type&lt;/b&gt;: $equipment_type&lt;br&gt;&lt;b&gt;Project:&lt;/b&gt; $project&lt;br&gt;&lt;b&gt;Description:&lt;/b&gt; $description&lt;br&gt;&lt;hr&gt;&lt;br&gt;&quote;;    
					$i++;
					}
				}
		?&gt;
		&lt;select name=&quote;Project&quote;&gt;
			&lt;option value=&quote;Ericson&quote;&gt;Ericson&lt;/option&gt;
			&lt;option value=&quote;Nokia&quote;&gt;Nokia&lt;/option&gt;
		&lt;/select&gt;
		&lt;INPUT TYPE=SUBMIT VALUE=&quote;Submit&quote;&gt;
		&lt;?
			if ($_GET&#1111;&quote;project&quote;] &lt;&gt; &quote;&quote;)
			{
			$query=&quote;select * from tmobile where project = '&quote; . $_GET&#1111;&quote;project&quote;] . &quote;'&quote;;
			$result=mysql_query($query);
			$num=mysql_numrows($result);
			mysql_close();
			$i=0;
			while ($i &lt; $num) 
				{
				$region=mysql_result($result,$i,&quote;region&quote;);
				$manu_part_num=mysql_result($result,$i,&quote;manu_part_num&quote;);
				$acs_part_num=mysql_result($result,$i,&quote;acs_part_num&quote;);
				$equipment_type=mysql_result($result,$i,&quote;equipment_type&quote;);
				$project=mysql_result($result,$i,&quote;project&quote;);
				$description=mysql_result($result,$i,&quote;description&quote;);
				echo &quote;&lt;br&gt;&lt;br&gt;&lt;b&gt;Region:&lt;/b&gt; $region &lt;br&gt;&lt;b&gt;Manufacturer Part #:base because I know how SQL works (how to write a statement to do what I want), I just don't know how to get it to work well with PHP.

Here's the deal. I'm a PHP newbie. Well, I'm a coding newbie, period. I'm the technical support/network administrator in my company, but I've been given the task of creating a very basic search page for people to browse our products. What they want are drop down boxes for people to search by. Right now, we have 3: region, equipment type and company. All I've gotten to work so far is to search just by region, not the others.

I need to do two things:

I need to refine my code so that an "ALL" option is included for each drop down menu. Let's say you want everything in the south region. You select south, then all and all and it will give you all products that are just in the south.

I also need to be able to refine the search to include searching by 1 of any field or 2 fields. Meaning if you just want cable equipment type, it gives just that. Or if you want cable equipment type in the south region, it gives just that.

Here is the code I have and here is a test page on which it goes:

http://acscable.com/test/test.php

Code: Select all

<html>
	<head>  
		<title>PHP Test</title>
	</head>
	<body>
		<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="GET">
		<select name="region">
			<option value="S">South</option>
			<option value="N">North</option>
			<option value="C">Central</option>
			<option value="W">West</option>
		</select>
		<?
			$username="ABC";
			$password="123";
			$database="acscable_tmobile";
			mysql_connect(localhost,$username,$password);
			@mysql_select_db($database) or die( "Unable to select database");

			if ($_GET["region"] <> "")
			{
				$query="select * from tmobile where region = '" . $_GET["region"] . "'";
				$result=mysql_query($query);
				$num=mysql_numrows($result);
				mysql_close();
				$i=0;
				while ($i < $num) 
					{
					$region=mysql_result($result,$i,"region");
					$manu_part_num=mysql_result($result,$i,"manu_part_num");
					$acs_part_num=mysql_result($result,$i,"acs_part_num");
					$equipment_type=mysql_result($result,$i,"equipment_type");
					$project=mysql_result($result,$i,"project");
					$description=mysql_result($result,$i,"description");
					echo "<br><br><b>Region:</b> $region <br><b>Manufacturer Part #:</b> $manu_part_num<br><b>ACS Part #:</b> $acs_part_num<br><b>Equipment Type</b>: $equipment_type<br><b>Project:</b> $project<br><b>Description:</b> $description<br><hr><br>";    
					$i++;
					}
			}
		?>
		<select name="Equipment Type">
			<option value="SB">Nokia SB Equipment</option>
			<option value="HYBL">HyperLink Blue</option>
			<option value="C">Cabling</option>
		</select>
		<?
			if ($_GET["equipment_type"] <> "")
				{
				$query="select * from tmobile where equipment_type = '" . $_GET["equipment_type"] . "'";
				$result=mysql_query($query);
				$num=mysql_numrows($result);
				mysql_close();
				$i=0;
				while ($i < $num) 
					{
					$region=mysql_result($result,$i,"region");
					$manu_part_num=mysql_result($result,$i,"manu_part_num");
					$acs_part_num=mysql_result($result,$i,"acs_part_num");
					$equipment_type=mysql_result($result,$i,"equipment_type");
					$project=mysql_result($result,$i,"project");
					$description=mysql_result($result,$i,"description");
					echo "<br><br><b>Region:</b> $region <br><b>Manufacturer Part #:</b> $manu_part_num<br><b>ACS Part #:</b> $acs_part_num<br><b>Equipment Type</b>: $equipment_type<br><b>Project:</b> $project<br><b>Description:</b> $description<br><hr><br>";    
					$i++;
					}
				}
		?>
		<select name="Project">
			<option value="Ericson">Ericson</option>
			<option value="Nokia">Nokia</option>
		</select>
		<INPUT TYPE=SUBMIT VALUE="Submit">
		<?
			if ($_GET["project"] <> "")
			{
			$query="select * from tmobile where project = '" . $_GET["project"] . "'";
			$result=mysql_query($query);
			$num=mysql_numrows($result);
			mysql_close();
			$i=0;
			while ($i < $num) 
				{
				$region=mysql_result($result,$i,"region");
				$manu_part_num=mysql_result($result,$i,"manu_part_num");
				$acs_part_num=mysql_result($result,$i,"acs_part_num");
				$equipment_type=mysql_result($result,$i,"equipment_type");
				$project=mysql_result($result,$i,"project");
				$description=mysql_result($result,$i,"description");
				echo "<br><br><b>Region:</b> $region <br><b>Manufacturer Part #:</b> $manu_part_num<br><b>ACS Part #:</b> $acs_part_num<br><b>Equipment Type</b>: $equipment_type<br><b>Project:</b> $project<br><b>Description:</b> $description<br><hr><br>";    
				$i++;
				}
			}
		?>
</body>
</html>
I'm stuck. All the PHP tutorials I see don't cover drop down know how to get it to work well with PHP.

Here's the deal. I'm a PHP newbie. Well, I'm a coding newbie, period. I'm the technical support/network administrator in my company, but I've been given the task of creating a very basic search page for people to browse our products. What they want are drop down boxes for people to search by. Right now, we have 3: region, equipment type and company. All I've gotten to work so far is to search just by region, not the others.

I need to do two things:

I need to refine my code so that an "ALL" option is included for each drop down menu. Let's say you want everything in the south region. You select south, then all and all and it will give you all products that are just in the south.

I also need to be able to refine the search to include searching by 1 of any field or 2 fields. Meaning if you just want cable equipment type, it gives just that. Or if you want cable equipment type in the south region, it gives just that.

Here is the code I have and here is a test page on which it goes:

http://acscable.com/test/test.php

Code: Select all

<html>
	<head>  
		<title>PHP Test</title>
	</head>
	<body>
		<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="GET">
		<select name="region">
			<option value="S">South</option>
			<option value="N">North</option>
			<option value="C">Central</option>
			<option value="W">West</option>
		</select>
		<?
			$username="ABC";
			$password="123";
			$database="acscable_tmobile";
			mysql_connect(localhost,$username,$password);
			@mysql_select_db($database) or die( "Unable to select database");

			if ($_GET["region"] <> "")
			{
				$query="select * from tmobile where region = '" . $_GET["region"] . "'";
				$result=mysql_query($query);
				$num=mysql_numrows($result);
				mysql_close();
				$i=0;
				while ($i < $num) 
					{
					$region=mysql_result($result,$i,"region");
					$manu_part_num=mysql_result($result,$i,"manu_part_num");
					$acs_part_num=mysql_result($result,$i,"acs_part_num");
					$equipment_type=mysql_result($result,$i,"equipment_type");
					$project=mysql_result($result,$i,"project");
					$description=mysql_result($result,$i,"description");
					echo "<br><br><b>Region:</b> $region <br><b>Manufacturer Part #:</b> $manu_part_num<br><b>ACS Part #:</b> $acs_part_num<br><b>Equipment Type</b>: $equipment_type<br><b>Project:</b> $project<br><b>Description:</b> $description<br><hr><br>";    
					$i++;
					}
			}
		?>
		<select name="Equipment Type">
			<option value="SB">Nokia SB Equipment</option>
			<option value="HYBL">HyperLink Blue</option>
			<option value="C">Cabling</option>
		</select>
		<?
			if ($_GET["equipment_type"] <> "")
				{
				$query="select * from tmobile where equipment_type = '" . $_GET["equipment_type"] . "'";
				$result=mysql_query($query);
				$num=mysql_numrows($result);
				mysql_close();
				$i=0;
				while ($i < $num) 
					{
					$region=mysql_result($result,$i,"region");
					$manu_part_num=mysql_result($result,$i,"manu_part_num");
					$acs_part_num=mysql_result($result,$i,"acs_part_num");
					$equipment_type=mysql_result($result,$i,"equipment_type");
					$project=mysql_result($result,$i,"project");
					$description=mysql_result($result,$i,"description");
					echo "<br><br><b>Region:</b> $region <br><b>Manufacturer Part #:</b> $manu_part_num<br><b>ACS Part #:</b> $acs_part_num<br><b>Equipment Type</b>: $equipment_type<br><b>Project:</b> $project<br><b>Description:</b> $description<br><hr><br>";    
					$i++;
					}
				}
		?>
		<select name="Project">
			<option value="Ericson">Ericson</option>
			<option value="Nokia">Nokia</option>
		</select>
		<INPUT TYPE=SUBMIT VALUE="Submit">
		<?
			if ($_GET["project"] <> "")
			{
			$query="select * from tmobile where project = '" . $_GET["project"] . "'";
			$result=mysql_query($query);
			$num=mysql_numrows($result);
			mysql_close();
			$i=0;
			while ($i < $num) 
				{
				$region=mysql_result($result,$i,"region");
				$manu_part_num=mysql_result($result,$i,"manu_part_num");
				$acs_part_num=mysql_result($result,$i,"acs_part_num");
				$equipment_type=mysql_result($result,$i,"equipment_type");
				$project=mysql_result($result,$i,"project");
				$description=mysql_result($result,$i,"description");
				echo "<br><br><b>Region:</b> $region <br><b>Manufacturer Part #:</b> $manu_part_num<br><b>ACS Part #:</b> $acs_part_num<br><b>Equipment Type</b>: $equipment_type<br><b>Project:</b> $project<br><b>Description:</b> $description<br><hr><br>";    
				$i++;
				}
			}
		?>
</body>
</html>
I'm stuck. All the PHP tutorials I see don't cover drop down menus, mainly just text inputs. And even then, I'm so new to this I still need help.

Thanks in Advance,

Chris

P.S - I created a user specifically available to the public with only select privileges, so I should be safe from exploits.

JCART | Pleasetabase because I know how SQL works (how to write a statement to do what I want), I just don't know how to get it to work well with PHP.

Here's the deal. I'm a PHP newbie. Well, I'm a coding newbie, period. I'm the technical support/network administrator in my company, but I've been given the task of creating a very basic search page for people to browse our products. What they want are drop down boxes for people to search by. Right now, we have 3: region, equipment type and company. All I've gotten to work so far is to search just by region, not the others.

I need to do two things:

I need to refine my code so that an "ALL" option is included for each drop down menu. Let's say you want everything in the south region. You select south, then all and all and it will give you all products that are just in the south.

I also need to be able to refine the search to include searching by 1 of any field or 2 fields. Meaning if you just want cable equipment type, it gives just that. Or if you want cable equipment type in the south region, it gives just that.

Here is the code I have and here is a test page on which it goes:

http://acscable.com/test/test.php

Code: Select all

<html>
	<head>  
		<title>PHP Test</title>
	</head>
	<body>
		<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="GET">
		<select name="region">
			<option value="S">South</option>
			&lt;option value=&quote;N&quote;&gt;North&lt;/option&gt;
			&lt;option value=&quote;C&quote;&gt;Central&lt;/option&gt;
			&lt;option value=&quote;W&quote;&gt;West&lt;/option&gt;
		&lt;/select&gt;
		&lt;?
			$username=&quote;ABC&quote;;
			$password=&quote;123&quote;;
			$database=&quote;acscable_tmobile&quote;;
			mysql_connect(localhost,$username,$password);
			@mysql_select_db($database) or die( &quote;Unable to select database&quote;);

			if ($_GET&#1111;&quote;region&quote;] &lt;&gt; &quote;&quote;)
			{
				$query=&quote;select * from tmobile where region = '&quote; . $_GET&#1111;&quote;region&quote;] . &quote;'&quote;;
				$result=mysql_query($query);
				$num=mysql_numrows($result);
				mysql_close();
				$i=0;
				while ($i &lt; $num) 
					{
					$region=mysql_result($result,$i,&quote;region&quote;);
					$manu_part_num=mysql_result($result,$i,&quote;manu_part_num&quote;);
					$acs_part_num=mysql_result($result,$i,&quote;acs_part_num&quote;);
					$equipment_type=mysql_result($result,$i,&quote;equipment_type&quote;);
					$project=mysql_result($result,$i,&quote;project&quote;);
					$description=mysql_result($result,$i,&quote;description&quote;);
					echo &quote;&lt;br&gt;&lt;br&gt;&lt;b&gt;Region:&lt;/b&gt; $region &lt;br&gt;&lt;b&gt;Manufacturer Part #:&lt;/b&gt; $manu_part_num&lt;br&gt;&lt;b&gt;ACS Part #:&lt;/b&gt; $acs_part_num&lt;br&gt;&lt;b&gt;Equipment Type&lt;/b&gt;: $equipment_type&lt;br&gt;&lt;b&gt;Project:&lt;/b&gt; $project&lt;br&gt;&lt;b&gt;Description:&lt;/b&gt; $description&lt;br&gt;&lt;hr&gt;&lt;br&gt;&quote;;    
					$i++;
					}
			}
		?&gt;
		&lt;select name=&quote;Equipment Type&quote;&gt;
			&lt;option value=&quote;SB&quote;&gt;Nokia SB Equipment&lt;/option&gt;
			&lt;option value=&quote;HYBL&quote;&gt;HyperLink Blue&lt;/option&gt;
			&lt;option value=&quote;C&quote;&gt;Cabling&lt;/option&gt;
		&lt;/select&gt;
		&lt;?
			if ($_GET&#1111;&quote;equipment_type&quote;] &lt;&gt; &quote;&quote;)
				{
				$query=&quote;select * from tmobile where equipment_type = '&quote; . $_GET&#1111;&quote;equipment_type&quote;] . &quote;'&quote;;
				$result=mysql_query($query);
				$num=mysql_numrows($result);
				mysql_close();
				$i=0;
				while ($i &lt; $num) 
					{
					$region=mysql_result($result,$i,&quote;region&quote;);
					$manu_part_num=mysql_result($result,$i,&quote;manu_part_num&quote;);
					$acs_part_num=mysql_result($result,$i,&quote;acs_part_num&quote;);
					$equipment_type=mysql_result($result,$i,&quote;equipment_type&quote;);
					$project=mysql_result($result,$i,&quote;project&quote;);
					$description=mysql_result($result,$i,&quote;description&quote;);
					echo &quote;&lt;br&gt;&lt;br&gt;&lt;b&gt;Region:&lt;/b&gt; $region &lt;br&gt;&lt;b&gt;Manufacturer Part #:&lt;/b&gt; $manu_part_num&lt;br&gt;&lt;b&gt;ACS Part #:&lt;/b&gt; $acs_part_num&lt;br&gt;&lt;b&gt;Equipment Type&lt;/b&gt;: $equipment_type&lt;br&gt;&lt;b&gt;Project:&lt;/b&gt; $project&lt;br&gt;&lt;b&gt;Description:&lt;/b&gt; $description&lt;br&gt;&lt;hr&gt;&lt;br&gt;&quote;;    
					$i++;
					}
				}
		?&gt;
		&lt;select name=&quote;Project&quote;&gt;
			&lt;option value=&quote;Ericson&quote;&gt;Ericson&lt;/option&gt;
			&lt;option value=&quote;Nokia&quote;&gt;Nokia&lt;/option&gt;
		&lt;/select&gt;
		&lt;INPUT TYPE=SUBMIT VALUE=&quote;Submit&quote;&gt;
		&lt;?
			if ($_GET&#1111;&quote;project&quote;] &lt;&gt; &quote;&quote;)
			{
			$query=&quote;select * from tmobile where project = '&quote; . $_GET&#1111;&quote;project&quote;] . &quote;'&quote;;
			$result=mysql_query($query);
			$num=mysql_numrows($result);
			mysql_close();
			$i=0;
			while ($i &lt; $num) 
				{
				$region=mysql_result($result,$i,&quote;region&quote;);
				$manu_part_num=mysql_result($result,$i,&quote;manu_part_num&quote;);
				$acs_part_num=mysql_result($result,$i,&quote;acs_part_num&quote;);
				$equipment_type=mysql_result($result,$i,&quote;equipment_type&quote;);
				$project=mysql_result($result,$i,&quote;project&quote;);
				$description=mysql_result($result,$i,&quote;description&quote;);
				echo &quote;&lt;br&gt;&lt;br&gt;&lt;b&gt;Region:&lt;/b&gt; $region &lt;br&aand company. All I've gotten to work so far is to search just by region, not the others.

I need to do two things:

I need to refine my code so that an "ALL" option is included for each drop down menu. Let's say you want everything in the south region. You select south, then all and all and it will give you all products that are just in the south.

I also need to be able to refine the search to include searching by 1 of any field or 2 fields. Meaning if you just want cable equipment type, it gives just that. Or if you want cable equipment type in the south region, it gives just that.

Here is the code I have and here is a test page on which it goes:

http://acscable.com/test/test.php

Code: Select all

<html>
	<head>  
		<title>PHP Test</title>
	</head>
	&lt;body&gt;
		&lt;form action=&quote;&lt;?php echo $_SERVER&#1111;'PHP_SELF'];?&gt;&quote; method=&quote;GET&quote;&gt;
		&lt;select name=&quote;region&quote;&gt;
			&lt;option value=&quote;S&quote;&gt;South&lt;/option&gt;
			&lt;option value=&quote;N&quote;&gt;North&lt;/option&gt;
			&lt;option value=&quote;C&quote;&gt;Central&lt;/option&gt;
			&lt;option value=&quote;W&quote;&gt;West&lt;/option&gt;
		&lt;/select&gt;
		&lt;?
			$username=&quote;ABC&quote;;
			$password=&quote;123&quote;;
			$database=&quote;acscable_tmobile&quote;;
			mysql_connect(localhost,$username,$password);
			@mysql_select_db($database) or die( &quote;Unable to select database&quote;);

			if ($_GET&#1111;&quote;region&quote;] &lt;&gt; &quote;&quote;)
			{
				$query=&quote;select * from tmobile where region = '&quote; . $_GET&#1111;&quote;region&quote;] . &quote;'&quote;;
				$result=mysql_query($query);
				$num=mysql_numrows($result);
				mysql_close();
				$i=0;
				while ($i &lt; $num) 
					{
					$region=mysql_result($result,$i,&quote;region&quote;);
					$manu_part_num=mysql_result($result,$i,&quote;manu_part_num&quote;);
					$acs_part_num=mysql_result($result,$i,&quote;acs_part_num&quote;);
					$equipment_type=mysql_result($result,$i,&quote;equipment_type&quote;);
					$project=mysql_result($result,$i,&quote;project&quote;);
					$description=mysql_result($result,$i,&quote;description&quote;);
					echo &quote;&lt;br&gt;&lt;br&gt;&lt;b&gt;Region:&lt;/b&gt; $region &lt;br&gt;&lt;b&gt;Manufacturer Part #:&lt;/b&gt; $manu_part_num&lt;br&gt;&lt;b&gt;ACS Part #:&lt;/b&gt; $acs_part_num&lt;br&gt;&lt;b&gt;Equipment Type&lt;/b&gt;: $equipment_type&lt;br&gt;&lt;b&gt;Project:&lt;/b&gt; $project&lt;br&gt;&lt;b&gt;Description:&lt;/b&gt; $description&lt;br&gt;&lt;hr&gt;&lt;br&gt;&quote;;    
					$i++;
					}
			}
		?&gt;
		&lt;select name=&quote;Equipment Type&quote;&gt;
			&lt;option value=&quote;SB&quote;&gt;Nokia SB Equipment&lt;/option&gt;
			&lt;option value=&quote;HYBL&quote;&gt;HyperLink Blue&lt;/option&gt;
			&lt;option value=&quote;C&quote;&gt;Cabling&lt;/option&gt;
		&lt;/select&gt;
		&lt;?
			if ($_GET&#1111;&quote;equipment_type&quote;] &lt;&gt; &quote;&quote;)
				{
				$query=&quote;select * from tmobile where equipment_type = '&quote; . $_GET&#1111;&quote;equipment_type&quote;] . &quote;'&quote;;
				$result=mysql_query($query);
				$num=mysql_numrows($result);
				mysql_close();
				$i=0;
				while ($i &lt; $num) 
					{
					$region=mysql_result($result,$i,&quote;region&quote;);
					$manu_part_num=mysql_result($result,$i,&quote;manu_part_num&quote;);
					$acs_part_num=mysql_result($result,$i,&quote;acs_part_num&quote;);
					$equipment_type=mysql_result($result,$i,&quote;equipment_type&quote;);
					$project=mysql_result($result,$i,&quote;project&quote;);
					$description=mysql_result($result,$i,&quote;description&quote;);
					echo &quote;&lt;br&gt;&lt;br&gt;&lt;b&gt;Region:&lt;/b&gt; $region &lt;br&gt;&lt;b&gt;Manufacturer Part #:&lt;/b&gt; $manu_part_num&lt;br&gt;&lt;b&gt;ACS Part #:&lt;/b&gt; $acs_part_num&lt;br&gt;&lt;b&gt;Equipment Type&lt;/b&gt;: $equipment_type&lt;br&gt;&lt;b&gt;Project:&lt;/b&gt; $project&lt;br&gt;&lt;b&gt;Description:&lt;/b&gt; $description&lt;br&gt;&lt;hr&gt;&lt;br&gt;&quote;;    
					$i++;
					}
				}
		?&gt;
		&lt;select name=&quote;Project&quote;&gt;
			&lt;option value=&quote;Ericson&quote;&gt;Ericson&lt;/option&gt;
			&lt;option value=&quote;Nokia&quote;&gt;Nokia&lt;/option&gt;
		&lt;/select&gt;
		&lt;INPUT TYPE=SUBMIT VALUE=&quote;Submit&quote;&gt;
		&lt;?
			if ($_GET&#1111;&quote;project&quote;] &lt;&gt; &quote;&quote;)
			{
			$query=&quote;select * from tmobile where project = '&quote; . $_GET&#1111;&quote;project&quote;] . &quote;'&quote;;
			$result=mysql_query($query);
			$num=mysql_numrows($result);
			mysql_close();
			$i=0;
			while ($i &lt; $num) 
				{
				$region=mysql_result($result,$i,&quote;region&quote;);
				$manu_part_num=mysql_result($result,$i,&quote;manu_part_num&quote;);
				$acs_part_num=mysql_result($result,$i,&quote;acs_part_num&quote;);
				$equipment_type=mysql_result($result,$i,&quote;equipment_type&quote;);
				$project=mysql_result($result,$i,&quote;project&quote;);
				$description=mysql_result($result,$i,&quote;description&quote;);
				echo &quote;&lt;br&gt;&lt;br&gt;&lt;b&gt;Region:&lt;/b&gt; $region &lt;br&gt;&lt;b&gt;Manufacturer Part #:&lt;/b&gt; $manu_part_num&lt;br&gt;&lt;b&gt;ACS Part #:&lt;/b&gt; $acs_part_num&lt;br&gt;&lt;b&gt;Equipment Type&lt;/b&gt;: $equipment_type&lt;br&gt;&an, equipment type and company. All I've gotten to work so far is to search just by region, not the others.

I need to do two things:

I need to refine my code so that an "ALL" option is included for each drop down menu. Let's say you want everything in the south region. You select south, then all and all and it will give you all products that are just in the south.

I also need to be able to refine the search to include searching by 1 of any field or 2 fields. Meaning if you just want cable equipment type, it gives just that. Or if you want cable equipment type in the south region, it gives just that.

Here is the code I have and here is a test page on which it goes:

http://acscable.com/test/test.php

Code: Select all

<html>
	<head>  
		<title>PHP Test</title>
	</head>
	<body>
		<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="GET">
		<select name="region">
			<option value="S">South</option>
			<option value="N">North</option>
			<option value="C">Central</option>
			<option value="W">West</option>
		</select>
		<?
			$username="ABC";
			$password="123";
			$database="acscable_tmobile";
			mysql_connect(localhost,$username,$password);
			@mysql_select_db($database) or die( "Unable to select database");

			if ($_GET["region"] <> "")
			{
				$query="select * from tmobile where region = '" . $_GET["region"] . "'";
				$result=mysql_query($query);
				$num=mysql_numrows($result);
				mysql_close();
				$i=0;
				while ($i < $num) 
					{
					$region=mysql_result($result,$i,"region");
					$manu_part_num=mysql_result($result,$i,"manu_part_num");
					$acs_part_num=mysql_result($result,$i,"acs_part_num");
					$equipment_type=mysql_result($result,$i,"equipment_type");
					$project=mysql_result($result,$i,"project");
					$description=mysql_result($result,$i,"description");
					echo "<br><br><b>Region:</b> $region <br><b>Manufacturer Part #:</b> $manu_part_num<br><b>ACS Part #:</b> $acs_part_num<br><b>Equipment Type</b>: $equipment_type<br><b>Project:</b> $project<br><b>Description:</b> $description<br><hr><br>";    
					$i++;
					}
			}
		?>
		<select name="Equipment Type">
			<option value="SB">Nokia SB Equipment</option>
			<option value="HYBL">HyperLink Blue</option>
			<option value="C">Cabling</option>
		</select>
		<?
			if ($_GET["equipment_type"] <> "")
				{
				$query="select * from tmobile where equipment_type = '" . $_GET["equipment_type"] . "'";
				$result=mysql_query($query);
				$num=mysql_numrows($result);
				mysql_close();
				$i=0;
				while ($i < $num) 
					{
					$region=mysql_result($result,$i,"region");
					$manu_part_num=mysql_result($result,$i,"manu_part_num");
					$acs_part_num=mysql_result($result,$i,"acs_part_num");
					$equipment_type=mysql_result($result,$i,"equipment_type");
					$project=mysql_result($result,$i,"project");
					$description=mysql_result($result,$i,"description");
					echo "<br><br><b>Region:</b> $region <br><b>Manufacturer Part #:</b> $manu_part_num<br><b>ACS Part #:</b> $acs_part_num<br><b>Equipment Type</b>: $equipment_type<br><b>Project:</b> $project<br><b>Description:</b> $description<br><hr><br>";    
					$i++;
					}
				}
		?>
		<select name="Project">
			<option value="Ericson">Ericson</option>
			<option value="Nokia">Nokia</option>
		</select>
		<INPUT TYPE=SUBMIT VALUE="Submit">
		<?
			if ($_GET["project"] <> "")
			{
			$query="select * from tmobile where project = '" . $_GET["project"] . "'";
			$result=mysql_query($query);
			$num=mysql_numrows($result);
			mysql_close();
			$i=0;
			while ($i < $num) 
				{
				$region=mysql_result($result,$i,"region");
				$manu_part_num=mysql_result($result,$i,"manu_part_num");
				$acs_part_num=mysql_result($result,$i,"acs_part_num");
				$equipment_type=mysql_result($result,$i,"equipment_type");
				$project=mysql_result($result,$i,"project");
				$description=mysql_result($result,$i,"description");
				echo "<br><br><b>Region:</b> $region <br><b>Manufacturer Part #:</b> $manu_part_num<br><b>ACS Part #:</b> $acs_part_num<br><b>Equipment Type</b>: $equipment_type<br><b>Project:</b> $project<br><b>Description:</b> $description<br><hr><br>";    
				$i++;
				}
			}
		?>
</body>
</html>
I'm stuck. All the PHP tutorials I see don't cover drop down menus, mainly just text inputs. And even then, I'm so new to this I still need help.

Thanks in Advance,

Chris

P.S - I created a user specifically available to the public with only select privileges, so I should be safe from exploits.

JCART | Please use

Code: Select all

tags when posting php code. Review [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Thu Jul 07, 2005 10:51 am
by Burrito
please use PHP tags when posting php code in the forums.

PHP doesn't care whether the data comes from a text field or a dropdown menu, it just sees the values as POST data.

in other words:

Code: Select all

<input type="text" name="bob" value="my name is bob">
is exactly the same (in PHP's eyes) as:

Code: Select all

<select name="bob">
<option value="my name is bob">bob</option>
</select>
you just need to use the $_POST array to retrieve the value's you're after:

ex:

Code: Select all

echo $_POST['bob'];
//this will be the same regardless of the field type on the form page
so for your query, you'd just need to refine or broaden your search based on the values selected (or entered) on your form page.

ex:

Code: Select all

$query = "select * from mytable where ".($_POST['bob'] == "my name is bob" ? "name='bob'" : "name='joe'");
de]
<input type="text" name="bob" value="my name is bob">


is exactly the same (in PHP's eyes) as:

Code: Select all

<select name="bob">
<option value="my name is bob">bob</option>
</select>
you just need to use the $_POST array to retrieve the value's you're after:

ex:

Code: Select all

echo $_POST['bob'];
//this will be the same regardless of the field type on the form page
so for your query, you'd just need to refine or broaden your search based on the values selected (or entered) on your form page.

ex:

Code: Select all

$query = "select * from mytable where ".($_POST['bob'] == "my name is bob" ? "name='bob'" : "name='joe'");
[/php:1
echo $_POST['bob'];
//this will be the same regardless of the field type on the form page
so for your query, you'd just need to refine or broaden your search based on the values selected (or entered) on your form page.

ex:

Code: Select all

$query = "select * from mytable where ".($_POST['bob'] == "my name is bob" ? "name='bob'" : "name='joe'");

Posted: Thu Jul 07, 2005 11:39 am
by clangro
Well, I've revamped the code based on what you said, but now it doesn't work at all.

Here is the error I get:
Parse error: parse error, unexpected T_VARIABLE in /home/acscable/public_html/test/test.php on line 32
What is wrong? :(

Code: Select all

<html>
	<head>  
		<title>PHP Test</title>
	</head>
	<body>
		<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
		<select name="region">
			<option value="S">South</option>
			<option value="N">North</option>
			<option value="C">Central</option>
			<option value="W">West</option>
		</select>
		<select name="equipment_type">
			<option value="SB">Nokia SB Equipment</option>
			<option value="HYBL">HyperLink Blue</option>
			<option value="C">Cabling</option>
		</select>
		<select name="project">
			<option value="Ericson">Ericson</option>
			<option value="Nokia">Nokia</option>
		</select>
		<INPUT TYPE=SUBMIT VALUE="Submit">
		<?
			$username="ABC";
			$password="123";
			$database="acscable_tmobile";
			mysql_connect(localhost,$username,$password);
			@mysql_select_db($database) or die( "Unable to select database");

			if ($_POST["region"] <> "")
			{
				$query="select * from tmobile where region = '".$_POST["region"]."' AND equipment_type = '"._$POST["equipment_type"]."' AND project = '"._$POST["project"]."'";
				$result=mysql_query($query);
				$num=mysql_numrows($result);
				mysql_close();
				$i=0;
				while ($i < $num) 
					{
					$region=mysql_result($result,$i,"region");
					$manu_part_num=mysql_result($result,$i,"manu_part_num");
					$acs_part_num=mysql_result($result,$i,"acs_part_num");
					$equipment_type=mysql_result($result,$i,"equipment_type");
					$project=mysql_result($result,$i,"project");
					$description=mysql_result($result,$i,"description");
					echo "<br><br><b>Region:</b> $region <br><b>Manufacturer Part #:</b> $manu_part_num<br><b>ACS Part #:</b> $acs_part_num<br><b>Equipment Type</b>: $equipment_type<br><b>Project:</b> $project<br><b>Description:</b> $description<br><hr><br>";    
					$i++;
					}
			}
		?>
</body>
</html>

Posted: Thu Jul 07, 2005 11:42 am
by Burrito
change _$POST to $_POST

Posted: Thu Jul 07, 2005 11:52 am
by clangro
Burrito wrote:change _$POST to $_POST
:oops:

Thanks for the help. Code now works like a charm. That's half of my problem solved.

Now what about creating an "ALL" choice for each field? Can I simply create an ALL for each select and have it be equal to * so I'm doing

Code: Select all

select * from tmobile where region = '*' AND equipment_type = '*' AND project = 'Ericson'
?

Posted: Thu Jul 07, 2005 12:12 pm
by Burrito
wildcards in sql are % not *, but yes you can use a wildcard.

you might need to use the 'like' operator however... give it a try is the best I can tell you.