0

I have a data as json format in mysql database table as mentioned below.

 {
 "dataType": "mis_type",
 "diceCode": "20070100102",
 "forms": {
   "4. Campus": {},
   "7. Classroom": {},
   "1. Details of school": {
          "School_Name": "GOVT. UPG .M.S. KHAGARA",
          "Village_Name": "KHAGARA",
          "Block_Name": "MOHAN PUR",
          "Dist_Name": "DEOGHAR",
          "Type_of_School": "DEPARTMENT OF EDUCATION",
          "Category": "PRIMARY WITH UPPER PRIMARY",
          "No_of_Student": 209,
          "No_of_Section": 8,
          "No_of_Classroom": 5,
          "Govt_Teacher": 2,
          "Para_Teacher": 3,
          "Other_Teacher": 0,
          "Total_Teacher": 5,
          "Total_Toilet": 2,
          "Total_Urinals": 2
             },
    "6. Drinking Water Source": {},
    "8. Kitchen Shed": {},
    "2. Location of School": {},
    "5. Location of Toilet": {},
    "3. Pollution Status": {
    }
   }
 }

I am trying to replace “ Total_Toilet ” value from another new value . For that I wrote following code.

  $selectQuery = "SELECT `json_data` FROM `abc` WHERE `disc_code`='" . $diseCode . "' AND `type`='mis'";
  $res = $conn->query($selectQuery);
  $jsonvalue = $res->fetch_assoc();
  $jsonvalue = json_decode($jsonvalue['json_data'], true);
  unset($value['Dise_Code']);
  unset($value['OBJECTID']);
  if ($res->num_rows > 0) 
 {
   foreach( $jsonvalue['forms']['1. Details of school'] as $key1 => $value1 )
 {
   echo"<br>"; 
   $key2 = str_replace($jsonvalue['forms']['1. Details of school'] ['Total_Toilet'] , 4, $value1 ) ;
   echo $key2;
 }

The code is being compiled without any error but still the previous value is not getting replaced by the new value using str_replace function. If there is something wrong I am doing then please guide me or if any alternative solution anyone know please help me.

Any help would be appreciated.

6
  • what is value of $value1 in foreach Commented Aug 25, 2016 at 12:06
  • and what you need as output? Commented Aug 25, 2016 at 12:14
  • I am getting all values of "Details of School" object in $value1 variable. The values you can see in the json as I posted above. Commented Aug 25, 2016 at 12:16
  • As you can see in the json data Total_Toilet having value 2 which I want to replace by 4 . I also mentioned in str_repalce function. Commented Aug 25, 2016 at 12:19
  • but what output you need. you have used str_replace and please check str_replace function man page Commented Aug 25, 2016 at 12:19

2 Answers 2

1

Ok please add below line before foreach loop

$jsonvalue['forms']['1. Details of school']['Total_Toilet'] = 4;

And make the foreach loop as below

foreach( $jsonvalue['forms']['1. Details of school'] as $key1 => $value1)
{
    echo"<br>"; 
    $key2 = $value1;
    echo $key2;
}

I hope this will help you.

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

18 Comments

Thanks for comment ... By mistake I posted syntax error I am using same as you suggested.
I have editted my answer so please try it @MayankSoni
I am able to get Total_Toilet value which is coming from mysql database. but I am thinking still problem is remain in the str_replace function which is not able to find Total_Toilet key inside passed $value1 array.
You changed the foreach loop as i editted in my answer ?
Actually according to me there is no need of str_replace , just try as I suggested. Just add that line just above foreach and change the foreach loop as I suggested in answer. Just give it try @MayankSoni :)
|
0

Please change your foreach loop as below to update your database

foreach($jsonvalue['forms']['1. Details of school'] as $key1 => $value1 )
{
   $current_value = $jsonvalue['forms']['1. Details of school']['Total_Toilet'];
   $new_value = '12'; // Replace with new value that you want
   echo $new_json = str_replace('"Total_Toilet": '.$current_value.'','"Total_Toilet": '.$new_value.'',$jsonvalue['json_data']);
   echo "<hr>";
   $updateQuery = "update `abc` set `json_data` = '".$new_json."' WHERE `disc_code`='" . $diseCode . "' AND `type`='mis'";
   $res = $conn->query($updateQuery);
}

Note : Edit the variables in the query as per your requirements.Feel free to comment.

Comments

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.