473,609 Members | 2,296 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

MySQL to JSON, trouble while creating a nested array.

2 New Member
I'm trying to get a JSON-file looking like this:

Expand|Select|Wrap|Line Numbers
  1. [
  2.     [
  3.     'Disease', [ latitude, longitude, magnitude, latitude, longitude, magnitude, ... ]
  4.     ],
  5.     [
  6.     'Disease', [ latitude, longitude, magnitude, latitude, longitude, magnitude, ... ]
  7.     ]
  8. ];
This is the current code:

Expand|Select|Wrap|Line Numbers
  1. $sqlDisease = mysql_query("SELECT DISTINCT Disease FROM DiseaseData") or die(mysql_error());
  2.  
  3. $result = [];
  4. while($rowDisease = mysql_fetch_assoc($sqlDisease)){
  5.         $theDisease = $rowDisease['Disease'];
  6.         $sqlData = mysql_query("
  7.         SELECT lat, lng, magnitude 
  8.         FROM DiseaseData 
  9.         WHERE Disease = '".mysql_real_escape_string($theDisease)."'") 
  10.         or die(mysql_error());
  11.         $data = [];
  12.         while($rowData = mysql_fetch_row($sqlData)){
  13.             $data[] = $rowData;
  14.         }
  15.         $mergedData = call_user_func_array( 'array_merge', $data );
  16.         $result[] =  [$theDisease, $mergedData];
  17. }
  18.  
  19. $json = json_encode($result);
  20. $file = 'testRes.json';
  21. file_put_contents($file, $json);
I get a JSON-file in the right format, but there's two problems. It seems like WHERE Disease = '".mysql_real_e scape_string($t heDisease)."' do not select distinct values and $data[] keeps growing.

Here's a sample how it looks with current code:
Expand|Select|Wrap|Line Numbers
  1. ["Malaria", ["40.555", "-122.222", "0.5", "85.111", "-33.4444", "0.7"]], 
  2. ["HIV", ["40.555", "-122.222", "0.5", "85.111", "-33.4444", "0.7",
  3. "40.555", "-122.222", "0.5", "85.111", "-33.4444", "0.7"]] 
  4. ]
Jun 22 '16 #1
1 1370
Dormilich
8,658 Recognized Expert Moderator Expert
https://www.sitepoint.com/community/...alues/228037/6
Jun 23 '16 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
2375
by: Michael Iatauro | last post by:
I've been having a little trouble creating users cross-platform. I have a little script that works just fine on Solaris 8 using this command: eval "/path/to/mysql --user=root --password=***** --socket=/path/to/socket --database=mysql --execute=\"INSERT INTO user (Host, User, Password, Select_priv, Insert_priv, Update_priv, Delete_priv, Shutdown_priv, File_priv, Create_tmp_table_priv, Lock_tables_priv) VALUES ('localhost', 'LocalUser',...
3
333
by: Paul Auleciems | last post by:
Hi: I'm having trouble using an Object which is created based on the following: Public CarDetail () as Car Where the CLASS "Car" is defined as: Public Class Car
1
2359
by: elixxir | last post by:
Hiho, I'm trying to deserialize the following XML feed from the weather channel but I'm having an issue with the nested arrays. I got as far as getting the 'day' into arrays but I can't get the 'part' nested array working. This is my first hack at XML feeds and such, so any pointers as to how to best do this or references/tutorials would be good. (I'm doing all this in .NET 2.0 btw)
1
1240
by: StuartJ | last post by:
Hi, I'm trying to create a generic threading application where you can pass in any object and a number of threads, most of it is working but I'm having trouble creating multiple instances of the object handed down. The code that I've supplied should simply create msgbox with 10 different messages, but it the ProcessID gets incremented for all the threads everytime a new object is created.
13
1988
by: Saint48198 | last post by:
I'm having trouble with an array of data created using the 'getElementsByTagName' call. I'm trying to remove duplicates from the Array. code follows: // creates a list of all keywords and the removes any dublicates and printes the results getKeywords : function() { var keywords = articleHolder.getElementsByTagName('span'); var savedKeywords = ; for (var i =0, allKeywords = keywords.length; i<allKeywords; i++) {...
2
1442
by: 4Ankit | last post by:
hey guys i am having trouble changing my array code to include a 'for/ in' structure the code i am trying to change is below: <script type="text/javascript"> var contents = new Array(3)
2
6860
by: phil.swenson | last post by:
I'm using Prototype.js and would like to convert the contents of an HTML table to JSON. Converting to an array first is fine too. Any thoughts on this? I haven't seen anyone do anything this.... thanks phil
1
2913
by: stevedub | last post by:
I am having some trouble configuring my array to read from a sequential file, and then calling on that to fill an array of interests. I think I have the class set up to read the file, but when I run my program the rates array does not get the information. I think my problem is where I am actuall calling the array index, but I am not sure how to do this. Here is my code: /* * MortFrame.java * * Created on February 24, 2008, 7:28 PM */...
7
5188
by: lawpoop | last post by:
Hello all - Is there a way to get a nested array in a $_POST variable? I have a form where there are several questions, each one corresponding to a database row. On submission of the form, I loop through the $_POST array, and update the database appropriately. So my post variable looks like this:
2
14492
by: mouac01 | last post by:
Let's say I have this MySQL table. Year Region Amount 2007 West 100 2007 East 200 2007 North 300 2007 South 400 2008 West 500 2008 East 600 2008 North 700
0
8563
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8210
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8394
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6990
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6052
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4013
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4074
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2527
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.