vaskovasilev Posted August 16, 2013 Share Posted August 16, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/281244-addeditdelete-json-array/ Share on other sites More sharing options...
Irate Posted August 16, 2013 Share Posted August 16, 2013 Try $datasortarray[] = "new value";, that'd add a new element to the end of the array. Quote Link to comment https://forums.phpfreaks.com/topic/281244-addeditdelete-json-array/#findComment-1445369 Share on other sites More sharing options...
AbraCadaver Posted August 16, 2013 Share Posted August 16, 2013 (edited) //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 August 16, 2013 by AbraCadaver Quote Link to comment https://forums.phpfreaks.com/topic/281244-addeditdelete-json-array/#findComment-1445372 Share on other sites More sharing options...
Solution vaskovasilev Posted August 16, 2013 Author Solution Share Posted August 16, 2013 AbraCadaver thanks! i read that if i want to delete some array, i have to use unset($json->Sofia) for example. Quote Link to comment https://forums.phpfreaks.com/topic/281244-addeditdelete-json-array/#findComment-1445375 Share on other sites More sharing options...
jazzman1 Posted August 16, 2013 Share Posted August 16, 2013 Hi vasko (здравей), is there a some particular reason to use JSON notation creating multiple names and input fields inside one form? I'm missing something here. Can you explain your logic, please? Quote Link to comment https://forums.phpfreaks.com/topic/281244-addeditdelete-json-array/#findComment-1445405 Share on other sites More sharing options...
AbraCadaver Posted August 16, 2013 Share Posted August 16, 2013 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); Quote Link to comment https://forums.phpfreaks.com/topic/281244-addeditdelete-json-array/#findComment-1445442 Share on other sites More sharing options...
vaskovasilev Posted August 17, 2013 Author Share Posted August 17, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/281244-addeditdelete-json-array/#findComment-1445531 Share on other sites More sharing options...
jazzman1 Posted August 17, 2013 Share Posted August 17, 2013 (edited) 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: SofiaNumbers: 26 Latitude: 42.696693 Longitude 23.302080City: LovetchNumbers: 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 August 17, 2013 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/281244-addeditdelete-json-array/#findComment-1445551 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.