简体   繁体   中英

Generate JSON Object for Mysql Tier Table (PHP)

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

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...

创建您想要json的数组/对象,并使用json_encode()函数将数组转换为json,然后写入文件。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM