My SQL query below won't output the $srchRegion && $srchCategory string if there are $_POST values for both. Why?
if (($srchRegion!='00') || ($srchRegion!='') && ($srchCategory=='00') || ($srchCategory=='')) {
return $query .= "AND region='$srchRegion'";
} elseif (($srchRegion=='00') || ($srchRegion=='') && ($srchCategory!='00') || ($srchCategory!='')) {
return $query .= "AND category='$srchCategory'";
} elseif (($srchRegion!='00') || ($srchRegion!='') && ($srchCategory!='00') || ($srchCategory!='')) {
return $query .= "AND region='$srchRegion' AND category='$srchCategory'";
}
Is there a better way of doing this?
Instead of looking for all possible boolean values and cluttered if-else, why just keep on appending the where clause based on corresponding value populated.
if($srchRegion != '00' && $srchRegion != '')
$query .= " AND region='$srchRegion'";
if($srchCategory != '00' && $srchCategory != '')
$query .= " AND category='$srchCategory'";
return $query;
So $query acts as query builder and it keeps on adding where clause based on $_POST values being set.
Try this:
if(($srchRegion!='00' || $srchRegion!='') && ($srchCategory=='00' || $srchCategory=='')){
return $query .= "AND region='$srchRegion'";
} elseif(($srchRegion=='00' || $srchRegion=='') && ($srchCategory!='00' || $srchCategory!='')){
return $query .= "AND category='$srchCategory'";
} elseif($srchRegion!='00' || $srchRegion!='') && ($srchCategory!='00' || $srchCategory!='')){
return $query .= "AND region='$srchRegion' AND category='$srchCategory'";
}
You haven't structured the if condition right. You check two OR conditions with one AND condition, so you'll need to put the OR conditions in brackets.
Related
Am trying to write a simple PHP function that uses both && and || function in php but am only able to use the && function, i get a desired result as shown with the below code
<?php
$srt = "asc";
if($srt != "" && $srt != "asc"){
echo "close";
} else {
echo "open";
}
?>
output >> open
But when i add the || function to it i get the wrong result
<?php
$srt = "asc";
if($srt != "" && $srt != "asc" || $srt != "desc"){
echo "close";
} else {
echo "open";
}
?>
output >> close
I have also try using AND in place of && and OR in place of || since php accepts both but i still get the same result am new to php so i dont know if what am trying is allowed or not so any advise will be greatly appreciated.
Use extra parenthesis to make it an expression.
if ($srt != "" && ($srt != "asc" || $srt != "desc"))
So you compare $srt != "" AND $srt != "asc" || $srt != "desc".
Your code can be improved for readability as
if (!in_array($srt, ['', 'asc', 'desc']))
if condition 1 :
if(isset($_POST['searchOrder']) && $_POST['searchOrder']!='')
{
$userToSearch = "WHERE name='Conformed'";
}
If condition 2 :
if(strstr($status, "value1") !== false )
{
$hide .= 'style="display: none;"';
}
i need if condition 1 OR If condition 2 , i tried below :
if (((strstr($status, "value1") !== false) ||
isset($_POST['searchOrder']) && $_POST['searchOrder']!='')
{
$userToSearch = "WHERE name='Conformed'";
})
{
$hide .= 'style="display: none;"';
}
I also tried below , but both gave error....
if ((strstr($status, "value1") !== false) || isset($_POST['searchOrder']) && $_POST['searchOrder']!=''))
{
$userToSearch = "WHERE name='Conformed'";
$hide .= 'style="display: none;"';
}
&& has higher precedence than ||, so you might want to rearrange your conditions, or put parentheses around the second group. Then just write the second part in the same block, like this.
if (strstr($status, "value1") !== false || (isset($_POST['searchOrder']) && $_POST['searchOrder']!=''))
{
$userToSearch = "WHERE name='Conformed'";
$hide .= 'style="display: none;"';
}
Something like that?
if((isset($_POST['searchOrder']) && $_POST['searchOrder']!='') || strstr($status, "value1") !== false) {
$userToSearch = "WHERE name='Conformed'";
$hide .= 'style="display: none;"';
}
If you need to satisfy both conditions then do something like this :
if (((strstr($status, "value1") !== false)) && (isset($_POST['searchOrder']) && $_POST['searchOrder']!='')))
{
$userToSearch = "WHERE name='Conformed'";
$hide .= 'style="display: none;"';
}
If you want two conditions alternatively you should remember about grouping.
(condition1) || (condition2)
In your case you've got && condition as part of second case, it is not grouped so it is processing first your OR condition between condition1 and first part of condition2, than last AND which is not you wanted.
Im creating detailed product search.I verified that ,my variables are posted correctly, but the query don't finding anything. My question is:
What could be wrong in this query and what could be the best solution for detailed search in SQL?
<?php
if (
isset($_POST["productName"]) || isset($_POST["searchCategory"]) ||
isset($_POST["searchManufacturer"]) || isset($_POST["costFrom"]) ||
isset($_POST["costTo"])
){
$stmt=$user_home->runQuery("SELECT* FROM Products
WHERE (productTitle='$_POST[productName]' OR '$_POST[productName]' IS NULL)
AND (category='$_POST[searchCategory]' OR '$_POST[searchCategory]' IS NULL)
AND (manufacturer='$_POST[searchManufacturer]' OR '$_POST[searchManufacturer]' IS NULL)
");
echo $stmt->rowCount();
}
Try to proper forming WHERE statement. You should add conditions for productTitle, category, manufacturer fields only then isset proper POST fields.
Try this code:
<?php
if (
isset($_POST["productName"]) || isset($_POST["searchCategory"]) ||
isset($_POST["searchManufacturer"]) || isset($_POST["costFrom"]) ||
isset($_POST["costTo"])
){
$conditions = array();
if (isset($_POST['productName'])) {
$conditions[] = "(productTitle='".$_POST['productName']."')";
}
if (isset($_POST['category'])) {
$conditions[] = "(category='".$_POST['searchCategory']."')";
}
if (isset($_POST['searchManufacturer'])) {
$conditions[] = "(manufacturer='".$_POST['searchManufacturer']."')";
}
$where = implode(' AND ', $conditions);
if ($where) {
$where = 'WHERE '.$where;
} else {
$where = "";
}
$stmt=$user_home->runQuery("SELECT * FROM Products ". $where);
echo $stmt->rowCount();
}
I have this condition here:
if($name != '' || $name != 0 || $name != "0"){
$where .= ' AND readyBuilt.home_title = "' . $name . '"';
}
My problem with this condition if $name is equal to "0" it still adds the $where to my $where variable. how do I fix this ?
i think, you mean something else, try use && instead of ||
Well, if this statement is true:
$name is equal to "0"
Then this condition evaluates to true:
$name != ''
Therefore the entire conditional check evaluates to true.
It sounds like you want to use && instead of ||:
if($name != '' && $name != 0 && $name != "0")
That way the entire condition will evaluate to true only if all three conditions are met, instead of only one condition.
The best solution is to use :
if(!empty($name))
Just using this should solve your issue:
if($name){
//your condition
}
try this
if ( !in_array($name, array('','0',0), true ) ) {
$where .= ' AND readyBuilt.home_title = "' . $name . '"';
}
Why this condition passes even if I change the $_GET variable?
I've this code
elseif(isset($_GET['results']) && $_GET['results'] == 'reorder' &&
isset($_GET['sort_column']) && $_GET['sort_column'] != '' && isset($_GET['sort_order'])
&& $_GET['sort_order'] != '' && $_GET['sort_order'] == 'asc'
|| $_GET['sort_order'] == 'desc') { /*rest goes here*/ } else {redirect}
Link returns like this
http://localhost/system/results.php?script_id=2&results=reorder&sort_column=supplier_address&sort_order=desc
But when I change this sort_column=supplier_address to say for example sorcodsalumn=supplier_address it doesn't redirect, instead goes ahead, any idea why? But if I simply remove few letters and dont replace with something else it does redirect...
How come if am using this isset($_GET['sort_column'] and am modifying sort_column to something else still passes this condition
Basic PHP operator precedence... && evaluates before ||, so your entire statement boils down to:
(x && y && z && ....) || ($_GET['sort_order'] == 'desc')
You need to simplify that if(), add some () to enforce your own evaluation order, and then things should start working a bit better.
your AND's and OR's need to be bracketed properly.
else if (isset($_GET['results']) &&
$_GET['results'] == 'reorder' &&
isset($_GET['sort_column']) &&
$_GET['sort_column'] != '' &&
isset($_GET['sort_order']) &&
$_GET['sort_order'] != '' &&
($_GET['sort_order'] == 'asc' || $_GET['sort_order'] == 'desc'))
{
/*rest goes here*/
} else {
redirect
}
More specifically your last || needs its own brackets, as shown above.
You need to put a bracket around your || (OR) statement like this:
elseif(isset($_GET['results']) && $_GET['results'] == 'reorder' &&
isset($_GET['sort_column']) && $_GET['sort_column'] != '' && isset($_GET['sort_order'])
&& $_GET['sort_order'] != '' && ($_GET['sort_order'] == 'asc'
|| $_GET['sort_order'] == 'desc')) { /*rest goes here*/ } else {redirect}
Otherwise your statement will return true anytime sort_order is set to 'desc'.