I have the below GET function that is based on regular WordPress custom field names.
When ticked, it sorts all posts that have that custom field value set to 1.
It currently works. But, I happen to have two custom fields named: 'free' and 'twofree'
When I tick 'free', it also includes 'twofree' and vica-versa. It does not seem to be case sensitive. Is there any work around to this?
<?php
/* my code starts from here*/
if( isset( $_GET['show'] ) && !empty ( $_GET['show'] ) ){
if( $_GET['show'] == 1 )
$meta_field = 'free';
else if( $_GET['show'] == 4 )
$meta_field = 'sale';
else if( $_GET['show'] == 2 )
$meta_field = 'genuine';
else if ( $_GET['show'] == 'onfire' )
$meta_field = 'onfire';
else if( $_GET['show'] == 5 )
$meta_field = 'twofree';
else if( $_GET['show'] == 3 )
$meta_field = 'onfire';
if( $_GET['show'] == 'sale' )
query_posts('cat='.$cat.'&meta_key='.$meta_field.'&meta_value > 0');
else
query_posts('cat='.$cat.'&meta_key='.$meta_field.'&meta_value>=1');
}
/* my code ends from here*/
?>
EDIT: I have found the problem and it lied in the part
query_posts('cat='.$cat.'&meta_key='.$meta_field.'&meta_value>=1');
I changed it to
query_posts('cat='.$cat.'&meta_key='.$meta_field.'&meta_value=1');
Use identity operator === when you want to match exact values, instead of == which checks similar values based on strings/integers.
More one this subject, check out this linkor this answer below
Using the `==` operator (*Equality*)
true == 1; //true, because 'true' is converted to 1 and then compared
"2" == 2 //true, because 2 is converted to "2" and then compared
Using the `===` operator (*Identity*)
true === 1 //false
"2" === 2 // false
This is because the **equality operator == does type coercion**...meaning that the interpreter implicitly tries to convert the values and then does the comparing.
On the other hand, the **identity operator === does not do type coercion**, and so thus it does not convert the values of the values when comparing
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 am trying to INSERT a new row into my db via php7.
Mysqli is throwing the error
Column 'newsletter' cannot be null
The newsletter column in the database is a tinyint(1)
Here is the code:
public function Add_User(array $data) {
if( !isset($data['name']) || !isset($data['email']) || !isset($data['newsletter']) )
$optin = ( $data['newsletter'] === 'on' ) ? 1 : 0;
$country_code = $this->Get_Country_Code();
if( !$q = $this->_db->prepare("INSERT INTO `users` (`name`,`email`,`country_code`,`newsletter`) VALUES (?,?,?,?)") ) { printf('error on prepare'); }
if( !$q->bind_param('sssi', $data['name'], $data['email'], $country_code, $optin)) { printf('error on bind'); }
if( !$q->execute() ) { printf('error on execute'); }
printf('Insert error %s', $this->_db->error);
if( $this->_db->affected_rows == 0 ) {
// There was a problem with the insert
printf('Insert error %s', $this->_db->error);
}
$q->close();
}
I also tried adding the boolean value as a string, but it still didn't work.
Thanks in advance.
Fixed!
The issue was that I was not providing all column values ( I want to update those later, as they are not available on the html form submit ).
I needed to assign a default value for those columns which will not be filled on the initial INSERT.
The error message however was not helpful.
Shouldn't you set $optin to true/false instead of 1/0?
if( !isset($data['name']) || !isset($data['email']) || !isset($data['newsletter']) )
$optin = ( $data['newsletter'] === 'on' ) ? 1 : 0;
Basically if the criteria for this if are not true so you don't "enter" your if your $optin variable will be null/undefined.
If you want to avoid that there are 2 ways.
1) change the accepted value in your newsletter field in the db to accept null
2)
$optin=0;
if( !isset($data['name']) || !isset($data['email']) || !isset($data['newsletter']) ){
if($data['newsletter'] === 'on' ){
$optin=1;
}
}
$country_code = $this->Get_Country_Code();
Define your variable with the "negative" value you want to be set if your criteria are not matched and change it inside your if.
Its' about a form to enter results of a soccer tournament.
The form got the already inputed data from the db and writes it into the value argument of the html form. If the value in the db NULL so in the html i got
value=""`
It's important that games with no inputs doesn't make a change in the db so i filter it before i do the query. But now could it happen that a game ends 0 : 0 But the db won't safe that. How can i say the system its not empty/NULL it is 0?
if(!empty($_POST[$tore_heim] OR !empty($_POST[$tore_gast]))){
$spiele_save = "UPDATE spiele SET tore_heim = '".$_POST[$tore_heim]."', tore_gast = '".$_POST[$tore_gast]."' WHERE id_spiele = ".$spiele_id[$i]."";
$spiele_save = mysqli_query($con, $spiele_save);};
};
Thank you deceze
if(isset($_POST[$tore_heim]) && strlen($_POST[$tore_heim]) > 0
OR
isset($_POST[$tore_gast]) && strlen($_POST[$tore_gast]) > 0)
The Problem is solved!
Check if value is '0': !empty($_POST[$tore_gast])) || $_POST[$tore_gast] === '0'
look at this example.
function isEmpty( $variable ){
return (
!isset($_POST[$variable]) ||
( empty($_POST[$variable]) && $_POST[$variable] !== "0" )
);
}
if ( !isEmpty($tore_heim) ){
// run your code here...
}
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;
hello i have a search engine for my site. i have two selection fields. i would like to give an example:
input_a: 3
input_b: 5
so the queries should look like:
if ( ($input_a == true) && ($input_b == false) ){
$filter_orders[] = " `col` LIKE '%$input_a%' ";
}
if ( ($input_a == false) && ($input_b == true) ){
$filter_orders[] = " `col` LIKE '%$input_b%' ";
}
if ( ($input_a == true) && ($input_b == true) ){
$filter_orders[] = " `col`= `col`>='%$input_a%' AND `col` = `col`<='%$input_b%' ";
now the problem is, that i dont know, if the last query is incorrect or not. the logic behind that will be that in case of my example the range between 3 and 5 should be found.
so 1,2 [3,4,5] 6,7,8...
if there is someone who could help me out i really would appreciate.
thanks a lot.
That should be
$filter_orders[] = " `col`>='%$input_a%' AND `col`<='%$input_b%' ";
EDIT:
You probably want the entire statement to look like this:
if ( (!empty($input_a) && is_int($input_a)) && (empty($input_b) || !is_int($input_b)) {
$filter_orders[] = " `col`='%$input_a%' ";
}
else if ( (empty($input_b) || !is_int($input_a)) && (!empty($input_b) && is_int($input_b)) ) {
$filter_orders[] = " `col`='%$input_b%' ";
}
else if ( !empty($input_a) && !empty($input_b) && is_int($input_a) && is_int($input_b) ) {
$filter_orders[] = " `col`>='%$input_a%' AND `col`<='%$input_b%' ";
}
Inserting the else if instead of just if helps makes sure other ifs arent executed if an earlier if is found toe be true.
Using the = instead of LIKE wille make sure that 3 == 3 and not 3 and 31 or 32
NO, sadly that is everything but correct. It should be something like this.
$filter_orders[] = " `col`>='%$input_a%' AND `col`<='%$input_b%' ";