vendredi 20 septembre 2019

PHP Simple If Statement Failing

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 querks 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!

Aucun commentaire:

Enregistrer un commentaire