Foreach Loop and if statement using PHP
Get the solution ↓↓↓I want to edit a JSON object, so made method calledalterJSON
the function find the specific JSONarray
from the database and then change thekey-values
based on the parameters was given
so my question is as you can see in this foreach loop there is an(if statement)
foreach( $decoded as $index => $product ){
print_r('<pre>');
if( $product->$key == $item) {
echo ('found');
$decoded[ $index ]->$key = $value;
$json = json_encode( $decoded );
$stmt->execute([
':json' => $json,
':id' => $bid
]);
}
}
the loop will printfound
and make some changes on the$JSON
byMySQL
query and that's perfectly working, but it will only find the JSONobject or item
even if there are more then oneitem
in the samekey-value
so, I want to make another condition in thestatement
that checks a uniquekey-value
calledid
so how can I accomplish that? I made some attempts and failed.
what I have tried
<?php
public function AlterJSON( $bid=false, $item=false, $key=false, $Product, $id, $value=false ){
try{
if( $bid && $item && $key && $value && $Product && $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 ){
print_r('<pre>');
if( $product->$key == $item && $product->$id == $Product) {
echo ('found');
$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,'price', $ProductID,'id', $ProductPrice );
Answer
Solution:
Have you tried to simply put the$stmt=$this->connection->prepare
into theforeach
loop?
Just be sure, the functionAlterJSON
have a variable$Product
but when you call thisAlterJSON
you use$ProductID
, can you use$ProductID
for both two variables? The PHP is case sensitive for variables. So it's good, but I for the readable it's better.
And other trick inif( $product->$key
if$key
not exist in$product
will produce an error. Yeah, you have a try catch, but if the replacement is in N loop you will lose the possibility of this update. I would install protection before the$product->$key
to just be sure the key exists.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: call to a member function store() on null
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.