K, first off I'm brand new to PHP and databases. So I
hope my question isn't stupid or unfitting to the
list.
Welcome! Only those that don't ask are unfitting
I am creating a "search and results" page of, right
now 3 fields with more to add.
Perhaps not necessary to my question, I have created a
working SQL statement which involved one main
transactional table and a number of joins to "static"
tables where the id codes inserted into the main table
are referenced. All is fine.
I thought I could use AND / OR statements, but both
don't allow for any number of fields unusued or force
a return if the first condition is met. I have a
variable in the SQL statment , like LocationCity =
'resset1', LocationState = 'resset2', etc.
Just sure how to get the code to see if it was passed
a value or not. Also to treat each field
independently, so if the user leaves the first 2
unchanged , and only enters the 3rd it will search for
that particular condition.
Try this, which is assuming that you are sending variables through POST
// Start the query off right
$query = "Select * FROM table_name WHERE (";
Foreach($_POST as $key->$value) {
if($value != '') {
// Add the query part
$query_frag .= " $key = '$value' AND ";
}
}
// Now you have something that looks like
// $query_frag = " first_var = 'Value1' AND second_var = 'Value2' AND
third_var = 'Value3' AND "
// Get rid of that last AND, plus close out your parentheses
$query_frag_trimmed = rtrim($query_frag,"AND ") . ')';
// Now $query_frag_trimmed = " first_var = 'Value1' AND second_var =
'Value2' AND third_var = 'Value3'"
// If you want to limit your query, tack on a limit clause on the end
$offset = 0;
$rows = 50;
$query_frag_trimmed .= " LIMIT $offset,$rows";
// Now put it all together
$completed_query = $query . $query_frag_trimmed;
// You've got:
// $completed_query = "Select * FROM table_name WHERE ( first_var =
'Value1' AND second_var = 'Value2' AND third_var = 'Value3') LIMIT
0,50";
// And send that to mysql
Also I should mention, that when the Search form comes
up, all the drop down menus say "Please Select", that
label actually has a value, which I assume is common.
Regardless on "submit" that value is going.
I was looking at the following code but the !='' tells
me that it's looking for an empty string. So if my
dead "Please Select" value is sent , do I need to put
the value in there, i.e. !='XXX' ?
<?php
if (isset($HTTP_GET_VARS['states']) && $HTTP_GET_VARS['states']!=''){
if (!isset($isNotFirst) OR $isNotFirst==false){
$isNotfirst = true;
$KT_search = '1=1 AND ';
}else{
$KT_search .=' AND ';
}
$KT_search .='LocationState = \''.$HTTP_GET_VARS['states'].'\' '; } ?>
Well I hope I don't get flamed since I'm obviously
clueless and seeking some advice when perhaps I should
be finding it on my own.
Thank you ,
Stuart
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php