1

I am using an api that comes back in JSON and in i guess called "pairs". excuse me if that is the wrong terminology

I am then searching thru looking the json that comes back for a specific user ID. which happens to be the second of the two pairs.

I am looking to get the balance of the id

so the JSON comes Back like this:

{"balance":"104","id":"2264511436216725766"},
{"balance":"100","id:"13430408307535451875"},
{"balance":"160","id":"4496198869167276848"},

This is what i am using now:

$set="4496198869167276848";
foreach($result['Balances'] as $key=>$value){ 

    $qty =  number_format($value['balance'] / 100000, 4);
    $ID= $value['id'];

    if($ID == $set){

         $sql = "UPDATE $table SET meta_value='$qty' WHERE user_id = '$user' AND meta_key = 'default_total' ";

         if ($conn->query($sql) === TRUE) {
              // echo "Record updated successfully";
         } else {
              // echo "Error updating record: " . $conn->error;
          }

         break;
     } else {
            $msg="<strong style='text-decoration:none;color:#289dcc;font-weight:bold'>ERROR !!! It Looks like you do not Have any Shares</strong><br>";
            echo $msg;
                break; 
   }
}

well what is happening is it only showing the 1st balance which is 104

I need it to stop once it finds it so i put the break;

without it will show the error message.

2
  • 1
    you have missing " around id in second data of JSON here:- {"balance":"100","id:"13430408307535451875"}, is it a typo? Commented Aug 9, 2017 at 4:39
  • first pair doesn't match the condition, so will go to else condition. 'break' in the else will cause the termination of loop. Remove the 'break' from else condition to continue the loop. Commented Aug 9, 2017 at 4:56

1 Answer 1

1

Look Here you make mistakes several things

Explanation

  • at here if($ID == $set) than you make update query it is fine. But when you it makes first foreach loop than $ID != 4496198869167276848 so it is goes to else condition and at in else condition you put break so after first execution it will break that's you only get first balance

So make it like this

$set="4496198869167276848";
foreach($result['Balances'] as $key=>$value){ 

    $qty =  number_format($value['balance'] / 100000, 4);
    $ID= $value['id'];

    if($ID == $set){

         $sql = "UPDATE $table SET meta_value='$qty' WHERE user_id = '$user' AND meta_key = 'default_total' ";

         if ($conn->query($sql) === TRUE) {
              // echo "Record updated successfully";
         } else {
              // echo "Error updating record: " . $conn->error;
          }
          break;
     } else {
            $msg="<strong style='text-decoration:none;color:#289dcc;font-weight:bold'>ERROR !!! It Looks like you do not Have any Shares</strong><br>";
            echo $msg;
   }
}

Just remove break from your else condition

And more important thing

Your mysql query is in danger of sql injection. So try to use bind parameters

Hope this will helps you :)

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

3 Comments

thank you that works But it it is sill showing the error.
it is still the echo $msg
Yes it will so you can use echo $msg outside the loop

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.