Jump to content

Need help with a query. Thanks!


john_duncan

Recommended Posts

Hello guys, hope you can help me with this one.  My PHP is limited but I am learning.  I have a slight conundrum to solve.

 

Take a look at the following code...  It is a dropdown box that lists a number of categories attained from the "industry" table.  These are "construction, building, surveying" etc.. Every industry is give and ID number from 1 to 5.  This code, lists the names of the industries and then finds the corresponding ID and uses this to append "trade_"(ID) which corresponds to an HTML page for that industry.  This populates the dropdown box with the industry names. When clicked on, it returns the ID number of the industry and inserts this into the address bar link.

 

OK, what I would like to do is take this ID number and use it to for a query on our "products" table and look for all the products that contain this number in the "industries" column within that table, and display.. (instead of the "trade_"(id) as below) all the products that contain that number (i.e. industry).  Something like index.php?page=results&industries=1.

 

so for example.  The rotating laser is in industries "1,2,3". Lets say 1 is construction.  So I would like to display all products in the products table that have a 1 in the industries column.

 

How would I append the code to include a separate query using the ID number from this?  Something like $query="select this.value from products"; or similar.

 

   <select name="industries" class="dropDown2" id="industries" onChange="javascript:document.location='index.php?page=trade_'+this.value">
                        <option selected>Trade...</option>
                        <?

					$query = "select * from industries" ;
					$result=mysql_query($query);
					while($record = mysql_fetch_array($result)){
						if($record["status"] == "true"){
							echo "\n<option value='".$record["id"]."'>    ".$record["industry"]."</option>";
						}
					}
				$query = "select * from industries" ;
					$result=mysql_query($query);
				?>
                      </select> 

 

Hope I've made sense!  Thanks

Link to comment
https://forums.phpfreaks.com/topic/228698-need-help-with-a-query-thanks/
Share on other sites

First up, lets tidy up some of your code:

$query = "SELECT id, industry FROM industries WHERE industry.status = 'true'" ;
$result=mysql_query($query) or die ('Fatal Error retrieving Industry list : '.mysql_error());

while($record = mysql_fetch_array($result)){

echo "\n<option value='".$record["id"]."'>    ".$record["industry"]."</option>

}

The following will get your result set that you want, I never use javascript, so can't advise you how to call this, but it does what you want your looking for.

$query2 = "SELECT id, name FROM products WHERE products.industries = ".$_GET['page'] ;
$result2 = mysql_query($query2) or die ('Fatal Error retreving industry product list : '.mysql_error());
while($record2 = mysql_fetch_assoc($result2){

//your list code here

}

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.