Jump to content

add/edit/delete json array


Go to solution Solved by vaskovasilev,

Recommended Posts

hello,

 

i have on row in database with this:

{"Sofia":["26","42.696693","23.302080"],"Lovetch":["13","43.140461","24.718996"]}

to read the data, i use :

..
$datasortarray=$row_info['seats_id'];
$datasortarray=json_decode($datasortarray);
foreach ($datasortarray as $key=>$row){
 echo $key ;
}

first is the name of the city, every city has an array with data - number seats, latitude, longitude

Can you please tell me how to add/delete other data to this array?

if i have these input fields:

<input name="city" type="text"> -> city name
<input name="numbers" type="text"> -> for first number..
<input name="latitude" type="text"> -> for second number
<input name="longitude" type="text"> -> for third number

Link to comment
https://forums.phpfreaks.com/topic/281244-addeditdelete-json-array/
Share on other sites


//to add or change
$datasortarray->{$_POST['city']}[0] = $_POST['numbers'];
$datasortarray->{$_POST['city']}[1] = $_POST['latitude'];
$datasortarray->{$_POST['city']}[2] = $_POST['longitude'];

//or
$datasortarray->{$_POST['city']} = array($_POST['numbers'], $_POST['latitude'], $_POST['longitude']);

 

Edited by AbraCadaver

AbraCadaver thanks!

i read that if i want to delete some array, i have to use unset($json->Sofia) for example.

 

Yes, to you delete one you would do:

unset($datasortarray->{$_POST['city']});
//or
unset($datasortarray->Sofia);

jazzman1, i wanted to create a way to save different information about different cities in one field, because when there are many users in one table, and if there are many information about cities for each user .. the rows will be much more.

So in this way you have the whole information in one field:)

Maybe there is and another way, but i dont know it.

But, you have already saved this information, everything you have to do, is to display this JSON object in proper readable format!

 

To keep the things simple, let's say we have two files:

json.php ( it's a file that we're connecting to our database and retrieve our database data into a JSON object)

display.php (it's a file that we want to display JSON) 

 

So, the conetent is:

 

json.php

<?php
echo '{"Sofia":["26","42.696693","23.302080"],"Lovetch":["13","43.140461","24.718996"]}';

display.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
 
  <script type="text/javascript">
      
  $(document).ready(function(){
    
     $.getJSON('json.php',null, processJSON);
     
      function processJSON(data){
      var infoHtml = '';
      $.each(data, function(city, cityInfo) {
       infoHtml += '<h4> City: '+city+'</h4>'
       infoHtml += '<p>Numbers: '+cityInfo[0]+'</p>'
       infoHtml += '<p>Latitude: '+cityInfo[1]+'</p>'
       infoHtml += '<p>Longitude '+cityInfo[2]+'</p>'
       }); //end each loop
       
       //display infoHtml variable
       $('#infoBox').html(infoHtml);
      }
  });
  </script>
 
</head>
<body>
  <div id="infoBox"></div>
</body>
</html>

So, if we run the script, the result shoud be someting like:

 

 

City: Sofia

Numbers: 26

Latitude: 42.696693

Longitude 23.302080

City: Lovetch

Numbers: 13

Latitude: 43.140461

Longitude 24.718996

 

If we want to edit(update) or delete some particular row in our database, we should have to create only two html input fields (or links) and each of them can provide some paricular information itself to delete or update something in our database. Send this request by Ajax and everything should be Ok. That's all you have to do.  

 

PS:  If you want to add a new info, you have to create an html form to do this. Just forgot to mention :)

Edited by jazzman1
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.