This is an excerpt from an activity that I'm doing:
//checkpoint A
$allowed_area = array('MarketA', 'MarketB');
$valid_area = in_array($area,$allowed_area) === true ? TRUE : FALSE;
//checkpoint B
$crop_check = $db->query("SELECT Crop FROM crops");
$allowed_product = $crop_check->fetchall(PDO::FETCH_COLUMN);
$valid_product = in_array($product,$allowed_product) === true ? TRUE : FALSE;}
In which both will result to '1' if TRUE.
This is my sample Message: Eggplant MarketB
(These two strings are split using preg_split as $product
and $area
, respectively.)
And this is my IF Statement:
if($valid_area == $valid_product){
$crop_query = $db->query("SELECT Crop, Price, Area FROM crops WHERE Crop = '{$product}' AND Area = '{$area}' LIMIT 1");
$result = $crop_query->fetch(PDO::FETCH_OBJ);
$message = "The current price of {$result->Crop} is P{$result->Price}/kg in {$result->Area}. }
And this is my sample SQL table:
Crop Price Area
Eggplant 23 MarketA
Rice 45 MarketB
As shown above, the code will execute the IF statement because both statements returned a TRUE value. However, the result would then be an error because Eggplant and MarketB does not exists on the same row.
Any suggestions on how to merge both checkpoints, so that they both have to check if $product
and $area
exists in the same row before executing the IF statement?
I've only remedied the problem with if the result is false, it will echo that "Not enough Data", however the solution would be greatly appreciated.