PHP get string value from Assoc array and convert it to string for comparison
I am trying to create a piece of code which will dynamically set the
maximum values of a slider on my page by finding the max values returned
from my database using a PDO statement.
The problem I am having is that I cannot convert the value returned in my
assoc array to an integer value, what am I doing wrong?
Example of PDO statement
$maxHGPM = $connection->prepare("SELECT MAX(high_gpm) FROM pumps
WHERE pump_type = :pType AND pump_category = :cVal");
$maxHGPM->bindParam(':pType', $pType, PDO::PARAM_STR);
$maxHGPM->bindParam(':cVal', $cVal, PDO::PARAM_STR);
$maxHGPM->execute();
$res3 = $maxHGPM->fetch(PDO::FETCH_ASSOC);
$maxFGPM = $connection->prepare("SELECT MAX(flow_gpm) FROM pumps
WHERE pump_type = :pType AND pump_category = :cVal");
$maxFGPM->bindParam(':pType', $pType, PDO::PARAM_STR);
$maxFGPM->bindParam(':cVal', $cVal, PDO::PARAM_STR);
$maxFGPM->execute();
$res4 = $maxFGPM->fetch(PDO::FETCH_ASSOC);
Test code and integer conversion
// TEST CURRENT SET VALUES
var_dump($res1);
var_dump($res2);
var_dump($res3);
var_dump($res4);
// INTEGER CONVERSION
$psiA = (integer) array_keys($res1)[0];
var_dump($psiA);
// PERFORM LOGICAL COMPARISONS
if(array_keys($res1)[0] >= array_keys($res2)[0]){
$psiOut = $res1;
} else {
$psiOut = $res2;
}
if(array_keys($res3)[0] >= array_keys($res4)[0]){
$gpmOut = $res3;
} else {
$gpmOut = $res4;
}
var_dump($psiOut);
var_dump($gpmOut);
When the code runs the value dumped by $psiA is 0, am I missing a step for
variable conversion ( I am fairly new to PHP )
No comments:
Post a Comment