0

I want to edit a JSON object, so made method called alterJSON the function find the specific JSON array from the database and then change the key-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 print found and make some changes on the $JSON by MySQL query and that's perfectly working, but it will only find the JSON object or item even if there are more then one item in the same key-value so, I want to make another condition in the statement that checks a unique key-value called id 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 );

2
  • 2
    shouldn't that be $product->id == $id instead? and wouldn't you think it would be easier to delegate this in another table making the id just a foreign key, instead of saving the whole json string, decoding and encoding and whatnot Commented Jul 27, 2020 at 1:26
  • What is $item from here if( $product->$key == $item)? Commented Jul 27, 2020 at 1:32

1 Answer 1

1

Have you tried to simply put the $stmt=$this->connection->prepare into the foreach loop?

Just be sure, the function AlterJSON have a variable $Product but when you call this AlterJSON 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 in if( $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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer but the $ProductID is a Post value it contains an integer that exists in the Json what i want to do is check if the Price = $item and Id = $ProductID

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.