As the title says I'm looping with if statement to find a
key-value
from an array called $decoded so basically the function is like this if the
$product-$key
equals
$item
and
$product->$key2
equals
$ProductId
then edit the specific key-value at first I tested the code in this way and it works
$decoded = json_decode( $fetched );
$stmt=$this->connection->prepare('update `bills` set `items`=:json where id=:id');
foreach( $decoded as $index => $product ){
if( $product->$key == $item ){
$decoded[ $index ]->$key = $value;
$json = json_encode( $decoded );
$stmt->execute([
':json' => $json,
':id' => $bid
]);
}
}
but when I added this
$product->$key2 == $ProductId
it stopped working so what I'm missing here?
What I have tried:
Full Code:-
public function AlterJSON( $bid=false, $item=false,$ProductId=false, $key=false, $key2=false, $value=false ){
try{
if( $bid && $item && $key && $value && $ProductId && $this->connected === true ){
$stmt=$this->connection->prepare('select `items` from `bills` where `id`=:id');
$stmt->execute( [ ':id' => $bid ] );
$fetched = $stmt->fetchColumn();
$decoded = json_decode( $fetched );
$stmt=$this->connection->prepare('update `bills` set `items`=:json where id=:id');
foreach( $decoded as $index => $product ){
if( $product->$key == $item && $product->$key2 == $ProductId ){
$decoded[ $index ]->$key = $value;
$json = json_encode( $decoded );
$stmt->execute([
':json' => $json,
':id' => $bid
]);
}
}
return true;
}
return false;
} catch( PDOException $e ){
return $this->errors === true ? $this->error( $e->getMessage() ) : false;
}
}
Function Call
$call = $dbConnection->AlterJSON( $BillID, $CurrentPrice,$id, 'price','id', $ProductPrice );
print_r(var_dump($call));