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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.