I have this PHP statement:
if (($row['rest'] != "") or ($row['rest'] != "Select An Option")) {
$rest = "<b>Rest Stops:</b> {$row['rest']},";
}
else {
$rest = "";
}
which is not evaluating properly and I can't figure out why. What I want the statement to do is if the field 'rest' is blank or "Select An Option" then the variable $rest should evaluate to "Rest Stops:" followed by the data. My data is "Select An Option" and I get "Rest Stops: Select An Option" as the output. I did some testing of this statement and I figured out PHP is assigning the variable $row['rest'] as not equal to "" instead of evaluating the 'or' statement. What would be the correct syntax?
What I want the statement to do is if the field 'rest' is blank or Select An Option than the variable $rest should evaluate to Rest Stops: followed by the data.
Your logic is incorrect. You need to check if they are equal to, so use == instead of !=.
if ($row['rest'] == "" || $row['rest'] == "Select An Option") {
^--------------------^ ^--------------------------------^
if field 'rest' blank if field is 'Select An Option'
This can be be improved by using empty() to perform the "is empty" check:
if (empty($row['test']) || $row['rest'] == "Select An Option") {
"If the field 'rest' is blank ...." - if that's true, then your logic is backwards:
if ($row['rest'] == '') || ($row['rest'] == 'Select an option') {
$rest = 'rest stops';
} else {
$rest = '';
}
Note the use of == for equality, rather than != for inequality.
Related
My if statements are not working despite the log file confirming that the values meet the conditions required.
As you will see below I have attempted to use both boolean and numerical values (as I have read that there are a few quirks with boolean statements in PHP.)
$lift = isset($p["lift"]) ? $p["lift"] : 0;
$parking = isset($p["parking"]) ? $p["parking"] : false;
// LIFT
if ( $lift === 1 && $home ) {
$query .= " AND `lift` == $lift";
}
// PARKING
if ( $parking === 1 && $home ) {
$query .= " AND `parking` != '';";
}
$log_file = "../../queries.log";
$error_message = "query: '$query' \n\n lift: ".$lift."\n home: ".$home."\n";
error_log($error_message, 3, $log_file);
I have tried both double and triple equal operators without success. I have tried both boolean and numerical values. The log statement prints the following:
'SELECT id, ref_crm, `type`, prov_name, prov_id, muni_name, muni_id, barrio, price_latest, photo,sqm,bed,bath,lift,parking,`year`,descr,
x(pt) as lat, y(pt) as lng, ref_cat FROM outlet WHERE prov_id = '06' AND `type` = 'Piso' AND price_latest >= 0 AND price_latest <= 500000 AND sqm >= 0 AND sqm <= 200'
lift: 1
home: true
As you can see, the string statements are not being attached to the query despite the two conditions both being met.
I have also tried removing the variables I've created ($lift and $home) and simply used $p["lift"] and $p["parking"] without success. The only way I am able to make this work is to specifically state $lift === 1 and $home === true (double or triple equal operators) above the conditions. This despite the log confirming that these variables already have those values set! I have also tried double and triple equal operators with $home and $p["home"]
Try echoing something out within your if statements.
Also please note:
https://www.php.net/manual/en/language.operators.comparison.php
Solution:
if (!empty($home) && $lift == 1) {
echo 'Lift works';
} else {
echo 'Lift is not 1';
}
if (!empty($home) && $parking == 1) {
echo 'Parking works';
} else {
echo 'Error: home parking is not 1';
}
I'm relatively new to PHP and I need some help on a search query.
I have a few drop down 'select' and a 'checkbox group' which will filter the search from database using (...WHERE somethingA = 'somethingA' && somethingB = 'somethingB' etc)
That's all working great but the problem comes when I want to make it so that some search fields DONT have to be used, so if 'SomethingA' is either disabled or value='none' then it will only return WHERE somethingB = 'SomethingB'.
I have tried using OR instead of AND but that returns both values if they are true and not really filtering it properly.
my initial solution was to have if..else statements to define the query,
for example:
$query = "SELECT * FROM table";
$results = $con->query("$query $where $QueryA $QueryB $QueryC");
if($_GET['SomethingA'] == "none" && $_GET['SomethingB'] == "none" && $_GET['SomethingC'] == "none"){
$where = ""
$QueryA = ""
$QueryB = ""
$QueryC = "ORDER by ID" //if all search field is 'none' then get all results
}elseif($_GET['SomethingB'] == "none" && $_GET['SomethingC'] == "none"){
$where = "WHERE"
$QueryA = "SomethingA = '{SomethingA}'" //only use A filter one field
$QueryB = ""
$QueryC = ""
}elseif($_GET['SomethingA'] == "none" && $_GET['SomethingC'] == "none"){
$where = "WHERE"
$QueryA = ""
$QueryB = "SomethingB = '{SomethingB}'" //only use B filter one field
$QueryC = ""
.....
it works but you can already see the problem as if i wanted to cross matrix all conditions it becomes very lengthy and confusing.
So my question is whether there is a much better way of doing this, for instance, make value='none' return all results?
been looking around and attacking it from many angles but cant find a solution..
maybe javascript could help but im not the best with it.
thanks in advance
The question is not too clear but look into this. It should help.
$query="SELECT * FROM table WHERE";
$query_link = " AND ";
$isASet=false;
$isBSet=false;
$isCSet=false;
if(strcmp($_GET['SomethingA'],"none") != 0){
$query.=" column = {$_GET['SomethingA']}";
//set this to true for later if statements
$isASet=true;
}
if(strcmp($_GET['SomethingB'],"none") != 0){
//check if A has been set, if yes include an AND
if($isASet){
$query.=$query_link;
}
//include this one as usual
$query.=" column = {$_GET['SomethingB']}";
$isBSet=true;
}
if(strcmp($_GET['SomethingC'],"none") != 0){
//check if A or B has been set, if yes include an AND
if($isASet || $isBSet){
$query.=$query_link;
}
//include this as usual
$query.=" column = {$_GET['SomethingC']}";
}
//run query and collect result
$result = $connection->query($query);
i have a simple if else statement and wondered if theres a better way of writing the code than I have listed below, the $price_data and $api_price are normally set but in some cases could be null / empty values, so if there is a empty value I need to revert back to $old_price
// now double the price compare api price and current price use highest value
if ($price_data >= $api_price) {
$price_double = $price_data;
// If we encounter a null or empty value for $price_data use old price data
if ($price_double == null){
$price_double = $old_price;
}
} else {
$price_double = $api_price;
// If we encounter a null or empty value for $price_data use old price data
if ($price_double == null){
$price_double = $old_price;
}
}
$useprice = ($price_double * 2);
I think this will do what you want:
if ((floatval($price_data) === 0) || (floatval($api_price) === 0)):
$price_double = $old_price;
else:
$price_double = ($price_data >= $api_price)?$price_data:$api_price;
endif;
I have an HTML with GET to a php file. The HTML has dropdowns one of which is called Combobox8. What I want is that when a user selects ALL in dropdown the relative variable in php is set to all, if not it is set to the selection in the dropdown:
$shopfloor = if ($_GET['Combobox8'] <> "all")
{ ($shopfloorvalue = $_GET['Combobox8'])}
$shopfloorvalue='';
However I am getting an error.
What is wrong pls?
Thanks
If think you need some changes in your statement.
if($_GET['Combobox8'] != "all"){
$shopfloorvalue = $_GET['Combobox8'];
} else {
$shopfloorvalue='';
}
First of all you don't need to bind the if statement to a variable. If you want easy access to it bind it to a function instead!
function shoppingFloor(){
if($_GET['Combobox8'] != "all"){
$shopfloorvalue = $_GET['Combobox8']
} else {
$shopfloorvalue='';
}
return $shopfloorvalue;
}
then you can call it using shoppingFloor().
Second you should try != (is not) instead of <>.
If all you want is to simply bind the floor to $shopfloorvalue you can even try the short version of the if else.
$shopfloorvalue = ($_GET['Combobox8'] != "all") ? $_GET['Combobox8'] : '';
^ ^ ^
Your IF Statement | if true what to do? | Else what? |
Give it a try!
$shopfloor = ($_GET['Combobox8'] != "all") ? $_GET['Combobox8']:'';
change your code from
$shopfloor = if ($_GET['Combobox8'] <> "all")
{ ($shopfloorvalue = $_GET['Combobox8'])}
$shopfloorvalue='';
To
$shopfloor = ($_GET['Combobox8'] <> "all")?$_GET['Combobox8']:'';
$shopfloor = if ($_GET['Combobox8'] != "all")
{ ($shopfloorvalue = $_GET['Combobox8'])}
$shopfloorvalue='';
$sql = 'SELECT * FROM `phpbb_profile_fields_data`';
$result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result)) {
if ($row['pf_kp_em_no_bonethr'] == '1') {
echo " Was 1";
} else if ($row['pf_kp_em_no_bonethr'] == '2') {
echo "Was 2";
} else {
echo "Was Neither 1 or 2";
}
}
$db->sql_freeresult($result);
I am curios, In my example I am checking the field for either a value of 1 or 2 but how do I check it for a value of NULL. Would it be any of the following three:
if ($row['pf_kp_em_no_bonethr'] == '')
if ($row['pf_kp_em_no_bonethr'] == '-1')
if ($row['pf_kp_em_no_bonethr'] == 'NULL')
Normally I would just try it out but I am not at home and wont be for the foreseeable future it has been bugging me. I am pretty sure it's not the second but I have seen -1 used for a null value in other languages. So can someone verify how I would indeed check for a NULL value please.
if ($row['pf_kp_em_no_bonethr'] === NULL)
Something like this should work.
if (is_null($row['pf_kp_em_no_bonethr'])) {
echo "Is NULL";
}
MySQL will return NULL values to PHP as actual PHP NULL. In this situation, what you need is:
// Notice lack of quotes around NULL
// And use === to distinguish type properly between integer 0 and NULL
if ($row['pf_kp_em_no_bonethr'] === NULL)
However, it would be more appropriate to check it in the query if NULL values are what you need to work with in PHP.
$sql = 'SELECT * FROM `phpbb_profile_fields_data` WHERE pf_kp_em_no_bonethr IS NULL';
Or to find all three values:
$sql = 'SELECT * FROM `phpbb_profile_fields_data`
WHERE pf_kp_em_no_bonethr IS NULL
OR pf_kp_em_no_bonethr IN (1,2)
';
I'd recommend to be very carfull with this one: I have seen
<?php
$field=$row['fieldname'];
if ($field===null) {
//Do something
}
?>
fail intermittently, especially on windows. This is why I prefer
SELECT
IFNULL(fieldname,'some_safe_value') AS fieldname
...
FROM
...
and the resulting trivial null-check.
Use is_null or === NULL.
if(is_null($row['pf_kp_em_no_bonethr'])){
}
or
if($row['pf_kp_em_no_bonethr'] === NULL){
}