[SOLVED] Help with php, JSON and array that has arrays
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I've been trying a long time now to create a JSON output that has the "correct" layout of what I'm looking for but 'nada' so far
The tables I get the data from is from MySQL, or better yet MariaDB :P
It's the typical Head and Rows situation. I got an Order table and an OrderRows table and it's no problem joining these in SQL but the list from it is as you know, ALL header data for ALL the rows are shown in the result.
I copied this example from a page I saw some days ago, the code for it won't work but the output it perfect (I think)
This is an example of a JSON output and it's this layout of it I want to create (or hope it is):
I need the layout of the data for an Android App I'm trying to create that'll show the header data 'once' and ALL it's rows in a ListView with a custom adapter.
My question now is, how do I create this in php, cos with pure SQL I can't fix it.
This is the latest code I tried:
and as you see this is NOT the correct output.
The 'X' is there just to see what I get and where.
What I need is to get ONE!! 'BillingAddress' and it's value with ALL it's rows and as you see, this output is exactly the same as the SQL output.
PS.
As you might see, I'm from Sweden and we got some 3 extra special letters that you US/UK/AUS/etc people don't and as you also might see I got some code in there trying to fix the JSON_ENCODE-error(warning) I get for one of these characters, so if you can help out with that too would be perfect
I sure can tell you I tried lots of the google-result I found but 'nada' AGAIN :P
Hmm,
Sorry but there must be some mistake since in my code "X" is not repeating and can not give exactly the same result, thus the output should be something like this:
I might have written 'stuff' in here wrong, I don't know, but what I need is:
ONE!! 'head'(="BillingAddress") and several 'rows' of 'ArticleNo'.
These things, 'BillingAddress', 'ArticleNo' are example column from each table.
'BillingAddress' is just ONE column from the 'HEAD' and 'ArticleNo' is just ONE column from the 'ROWS' of the previous 'HEAD'.
PS. the 'X' I just put in there 'cos I saw it in an example but it didn't make it work.
Last edited by Basher52; 10-16-2015 at 08:25 PM.
Reason: a PS. created
Just tried this and THIS is promising
Seems the result is what I want too
(well I hope so, this all depends on that my thoughts about how the data into the Android App is supposed to be is correct)
I'm gonna try this with more "heads" and "rows" per 'head' and if that'll work.............. You just won a toaster :P
*and later I gotta READ your post and try to understand it, but that'll be tomorrow or such.
NOW, I gotta be free from "work"
(although I don't consider computer work WORK :P )
@Smokey_justme, how can I show the column names for the "head data" as the row's data shows also how do I add other columns I need from the head data?
something like this: (if I haven't missed some parenthesis or such...). I've kept trying for hours and read lots of stuff with no luck
When googling the only thing I find is converting a one table query into JSON, but when the heck is that ever used, kinda.
I can't understand why I can't find any result on a mysql one-to-many into JSON, weird
because I didn't want that in the output but if I do the result will be that all heads comes first and then all rows.
This is probably due to the fact that each row can't be bound to its head data. (any other point on this is welcome)
When I first read about JSON on a page somewhere it made me think it could create a hierarchy of heads/rows automatically but I was mistaken when reading the "correct" info
If someone knows if it IS possible to remove the above shown
Code:
$rowparent['Id']
then let me know
I might be back later with other stuff related to this though
Thanks to Smokey_justme for putting me on the right track, well kinda
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.