0

Straight to point i have 3 tables like following -

QTY Types Table

--------------------
| ID  |  QTY types |
|  1  |  1         |
|  2  |  2-4       |
|  3  |  5-9       |
|  4  |  10-24     |
|  5  |  25+       |
--------------------

Poster Size Table

-----------------------
| ID  |  Poster Size  |
+-----+---------------+
|  1  |  A4           |
|  2  |  A3           |
|  3  |  A2           |
|  4  |  A1           |
|  5  |  A0           |
-----------------------

Price table (based on Qty ID- column title)

--------------------------------------------------------------------------------
| qty_id_1  |  qty_id_2  |  qty_id_3  |  qty_id_4  |  qty_id_5  |  poster_size |
+-----------+------------+------------+------------+------------+--------------+
|     4     |    3.5     |   2.75     |   3.25     |         3  |            1 |
|     6     |    5.5     |      4     |   4.75     |       4.5  |            2 |
|    12     |    9.5     |    6.5     |    8.5     |         8  |            3 |
|    18     |     16     |   10.5     |     14     |      12.5  |            4 |
|    34     |     33     |     25     |     31     |        28  |            5 |
--------------------------------------------------------------------------------

I just wanted to Generate Price According to User Input for example if user selects Qty.id=2 Poster_size.id=3 so answer will be $9.5.

All of this data are static so after that i just wanted to generate JSON file using fwrite (php) something like this

    {
        "qty_1":{
            "poster_size_1":[
                {
                    "price":4
                }
            ],
            "poster_size_2":[
                {
                    "price":6
                }
            ],
            "poster_size_3":[
                {
                    "price":12
                }
            ],
            "poster_size_4":[
                {
                    "price":18
                }
            ],
            "poster_size_5":[
                {
                    "price":34
                }
            ]
        },
        "qty_2":{
            "poster_size_1":[
                {
                    "price":3.5
                }
            ],
            "poster_size_2":[
                {
                    "price":5.5
                }
            ],
            "poster_size_3":[
                {
                    "price":9.5
                }
            ],
            "poster_size_4":[
                {
                    "price":16
                }
            ],
            "poster_size_5":[
                {
                    "price":33
                }
            ]
        }
    }

By this i can easily access obj.qty_1.poster_size_3.price as $9.5.

How can i populate JSON file like this or is there any alternate way to populate different JSON structure to get my required data. (im new to JSON objects)

Help Appreciated | Thanks in Advance

Please Check this my sample PHP file to generate JSON

7
  • yest but its not generating json array like i wished .. hope you understand my situation with retrieving large amount of data Commented Jul 21, 2017 at 2:36
  • gist.github.com/joeljerushan/686454b52688f3d4d3b5319cc3b68d3c here this is sample but this is not what i wanted i just wanted to nest 3 tables data to one Object like i asked in question .. Commented Jul 21, 2017 at 2:55
  • I've Just got an idea if i create a JSON object like i mentioned in question it will help to get prices from. that's why i didn;t have a code to show you; but i attempt to create a json file like above comment .. (gist file) Commented Jul 21, 2017 at 2:57
  • Have you tried to echo json_encode() ? Before doing it, are you sure that your sql queries are correct and the arrays are building properly? do step by step debugging. Commented Jul 21, 2017 at 3:20
  • 1
    It is my opinion that doing this task "the right way" would mean using MYSQL to its fullest and making a single query call with JOINs. You will need to return a resultset like this and then building the desired array then converting to json will be a snap. If you are unable to write such a query, then you may ask for help, but we will be better equipped (and can test our work / provide a sqlfiddle) if you post an sql export of the necessary tables. Commented Jul 21, 2017 at 3:20

2 Answers 2

1

explain like this a array like json.

$arr = array(
  'qty_1' => array(
    "poster_size_1" => array(
      "price" => 4
    ),
  'qty_2' => array(
    'key' => 'value',
    'key2' => array(
       value, value2
    )
  )
);

this array will make

{"qty_1": {"poster_size_1" : {"price": 4}}} blabla...
Sign up to request clarification or add additional context in comments.

2 Comments

This low-quality answer may actually confuse the OP about proper array structure since the first subarray is different from the second. Please improve or delete.
This one is maybe Low Quality but this did a trick and helpful to understand how to populate object with PHP thank you
0

create an array / Object that the way you want json and use json_encode() function to convert array to json then write into a file.

3 Comments

yest could you please explain me how to generate JSON object like my need with PHP (its nested JSON Object but using QTY table id as top level object)
for nested JSON, create multi dimensional array / object. You may refer this link<stackoverflow.com/questions/40102747/…>. But it was created different purpose. Still it will help you to use the logic and some php codes.
Have you tried to echo json_encode() ? Before doing it, are you sure that your sql queries are correct and the arrays are building properly? do step by step debugging.

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.