Jump to content

Query Generation


crazyguy30

Recommended Posts

Hi guys,

 

I'm having abit of a problem generating a mysql query based on selected dropdown boxes.

 

This is how i populate my options in the select form:

 

<?php
function getResultSet($field) {
$result = mysql_query("SELECT DISTINCT ". $field ." FROM whales ORDER BY ". $field);
if ($result) {
	while($row = mysql_fetch_array($result)) {
		$fieldValue = $row[$field];
		if($fieldValue != "" || $fieldValue != null) {
  				echo "<option value=\"". $fieldValue ."\">". $fieldValue ."</option>";
		}
	}
}
}
?>

........


<select name="location">
<option value="0" selected>All</option>
<?php
getResultSet("Location");
?>
</select>

 

So if "All" is selected it returns 0 (this can be changed if need be). Otherwise if a value is selected it add the value to the form.

 

This is the javascript i want to generate the string to pass to the php function (alreade implemented with ajax). Just need to generate string query.

 

 

<script>
function generateQuery() {
var date = document.filtration.date.options[document.filtration.date.selectedIndex].value;
var location = document.filtration.location.options[document.filtration.location.selectedIndex].value;
var species = document.filtration.species.options[document.filtration.species.selectedIndex].value;
var pod = document.filtration.pod.options[document.filtration.pod.selectedIndex].value;
var individuals = document.filtration.individuals.options[document.filtration.individuals.selectedIndex].value;
var cred = document.filtration.cred.options[document.filtration.cred.selectedIndex].value;	

var query = "SELECT * FROM whales";

if((date == 0) && (location == 0) && (species == 0) && (pod == 0) && (individuals == 0) && (cred == 0)) {

alert(query);

}
        else {
             //Somehow loop through and generate a query
        }
}
</script>

 

Any help or ideas would be greatly appreciated.

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/104772-query-generation/
Share on other sites

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.