Forum Moderators: open
ie
field 1 "all"
field 2 "county"
field 3 "district"
I can set the filters to each value but when i enter "all" i get no results.
I hope this makes sence.
This is being used in a property search web page and the user may want to search by any or all of the filters.
Thanks for your time.
Roger
$fmdistrict_main_search = "-1";
if (isset($_POST['district'])) {
$fmdistrict_main_search = (get_magic_quotes_gpc())? $_POST['district'] : addslashes($_POST['district']);
}
$fmlocation_main_search = "-1";
if (isset($_POST['location'])) {
$fmlocation_main_search = (get_magic_quotes_gpc())? $_POST['location'] : addslashes($_POST['location']);
}
mysql_select_db($database_abruzzo, $abruzzo);
$query_main_search = sprintf("SELECT location, district FROM property WHERE location = '%s' AND district = '%s'", $fmlocation_main_search,$fmdistrict_main_search);
$main_search = mysql_query($query_main_search, $abruzzo) or die(mysql_error());
$row_main_search = mysql_fetch_assoc($main_search);
$totalRows_main_search = mysql_num_rows($main_search);
I hope this is what you want.
Roger
<p><?php echo $_POST['location'];?></p>
<p><?php echo $_POST['district'];?></p>
when i send "all" (no value) i get no result, when i select from one of the drop down menus i get the corresponding result.
Is this what you mean, if not could you tell me how to echo the result you want.
Thanks again, sorry for my limited knowledge your help is very appreciated.
Try this which is just what you posted before with just the echo of the query so we can see it. When it echos the value to the screen copy and paste it back (just the echo'd value) so we can take a look. Run it twice and grab the output from both. One when you say it works and one where you say it doesn't work:
$fmdistrict_main_search = "-1";
if (isset($_POST['district'])) {
$fmdistrict_main_search = (get_magic_quotes_gpc())? $_POST['district'] : addslashes($_POST['district']);
}
$fmlocation_main_search = "-1";
if (isset($_POST['location'])) {
$fmlocation_main_search = (get_magic_quotes_gpc())? $_POST['location'] : addslashes($_POST['location']);
}
mysql_select_db($database_abruzzo, $abruzzo);
$query_main_search = sprintf("SELECT location, district FROM property WHERE location = '%s' AND district = '%s'", $fmlocation_main_search,$fmdistrict_main_search);
echo "What do we have here?:: '".$query_main_search"'<br>";
$main_search = mysql_query($query_main_search, $abruzzo) or die(mysql_error());
$row_main_search = mysql_fetch_assoc($main_search);
$totalRows_main_search = mysql_num_rows($main_search);
JAG
'SELECT location, district FROM property'
The empty strings '' in the select is what is causing the problem. Nothing in the table matches the empty string so the query is doing just what you asked it to do. By removing the where clause you will get everything.
You mentioned null in a field1. If you are using that as a value to indicate ALL then use the following query:
'SELECT location, district FROM property where field1 is null'
Notice I used 'is' and not '=' when checking for null.
JAG
$fmdistrict_main_search = "-1";
if (isset($_POST['district'])) {
$fmdistrict_main_search = (get_magic_quotes_gpc())? $_POST['district'] : addslashes($_POST['district']);
}
$fmlocation_main_search = "-1";
if (isset($_POST['location'])) {
$fmlocation_main_search = (get_magic_quotes_gpc())? $_POST['location'] : addslashes($_POST['location']);
}
mysql_select_db($database_abruzzo, $abruzzo);
$query_main_search = sprintf("SELECT location, district FROM property WHERE location is null", $fmlocation_main_search,$fmdistrict_main_search);
echo "What do we have here?:: '".$query_main_search."'<br>";
$main_search = mysql_query($query_main_search, $abruzzo) or die(mysql_error());
$row_main_search = mysql_fetch_assoc($main_search);
$totalRows_main_search = mysql_num_rows($main_search);
Now if i select all on either of the 2 menus i get nothing and if i select coasal or chieti on either of the menus I get nothing!
Roger
I hope this makes it a bit clearer as to my end result.
Roger
So there are a couple ways to do it. You could string all the locations together with or statements like:
...WHERE (location = 'Coastal' OR location = 'Inland' OR location = 'Underwater') AND district = 'Chieti'
But that could get long. You could also do something like:
...WHERE location <> '' AND district = 'Chieti'
Both ways would give you ALL the locations in the district 'Chieti' with the second option above assuming there are no empty entries in the table.
Unfortunately you will have to build the query string with the possible combinations in it. So to get any location and any district your code could put together a where clause like:
...WHERE location <> '' AND district <> ''
That would give you any location and any district. Just keep adding the other options you have in the drop down and you should get what you want.
JAG
this is the new record.
mysql_select_db($database_abruzzo, $abruzzo);
$query_main_search = "SELECT location, district FROM property WHERE location <> '' AND district = 'Chieti'";
$main_search = mysql_query($query_main_search, $abruzzo) or die(mysql_error());
$row_main_search = mysql_fetch_assoc($main_search);
$totalRows_main_search = mysql_num_rows($main_search);
I can give you the link to my testing server if that might help?
Roger
OK ,now i always get the same result, so if i now change the menus to show location "hills" and district to "Pascara" I still get the "chieti" and "coastal" coming up in the list when they shouldn't!
Assuming the database is OK then it has to be the way the query is put together in the php script. Check the query and make absolutely sure that the location and district (and everything else) are correct.
I'm guessing that you're not putting the condition in the php code to handle the queries. If you are not then you would get the same results back.
JAG
What could be wrong with the database that may cause this issue?
When you ask about conditions are you talking about variables i need to set in the query?
This is the query i have in at the moment.
$query_main_search = sprintf("SELECT location, district, FROM property WHERE location <> '' AND district = '%s'",
As i am so new to this I think i may be out of my depth. do you have any facilaties to sort this for me for a fee? If not are you happy to continue to help me with this till i get a resolution, I will be donating as you have put so much effort in.
do you have any facilaties to sort this for me for a fee?
Nah...the nice thing about this forum is we all just like to help :-)
Here is a little change to your code that should get you in the right direction. I code a little different than you do so I did it the way I would do it. Feel free to change.
$main_search = "select location, district from property where ";
if (isset($_POST['district'])) {
// If someone didn't choose 'all' then do the if
if($_POST['district'] <> 'ALL'){
$fmdistrict_main_search = "district = '".mysql_real_escape_string($_POST['district'])."'";
}else{
// Someone chose 'all' so do the else
$fmdistrict_main_search = "district <> ''";
}
}
$main_search = $main_search.$fmdistrict_main_search;
//So now add the location info
if (isset($_POST['location'])) {
// There is a location so add the 'and' to the condition
$main_search = $main_search." and ";
if($_POST['location'] <> "ALL"){
$fmlocation_main_search = "location = '".mysql_real_escape_string($_POST['location']."'";
}else{
$fmlocation_main_search = "location <> ''"
}
}
$main_search = $main_search.$fmlocation_main_search;
Now just send the $main_search query to the db and all should be fine. I didn't try the code so you may have to fix any typos.
JAG
I do however have a question which is unrelated to this issue but still a PHP code issue. If you are happy to help me again i would realy appreciate it.
Thanks again
Roger