Page 1 of 1

How do I update a json file with mysql data using php

Posted: Fri Jan 02, 2015 4:02 pm
by adsegzy
Hello friends,

I am knowledgeable in PHP but not in JSON. But I have a JSON script that I want some of the data in it to be dynamic from mysql database. I have products page where all products are listed, on clicking any product it opens product detail page where this JSON script coordinates. Below is d JSON file

[text]
{
"AllCategorys": {
"Category": [
{
"id": "7",
"title": "Women",
"isDefault": "7",
"subCategory": [
{
"products": [
{
"isDefault": "25",
"id": "25",
"title": "Men Tshirt",
"thumbnail": "imgs/cards/61946.Thumb.jpg",
"rawprice": "0",

"colors": {
"color": [
{

"views": {
"view": [
{
"id": "1",
"isDefault": "1",
"title": "Front",
"viewname": "Front",
"imagewidth": "322",
"imageheight": "422",
"subitemid": " 0 ",
"viewthumbnail": "imgs/cards/61946-Bg.png",
"container": "true",
"x": "0",
"y": "0",
"width": "381",
"height": "479",
"printwidth": "10",
"printheight": "15"
},{
"id": "2",
"isDefault": "2",
"title": "FrontInside",
"viewname": "Front Inside",
"imagewidth": "351",
"imageheight": "439",
"subitemid": " 0 ",
"viewthumbnail": "imgs/cards/FrontInside.png",
"container": "true",
"x": "0",
"y": "0",
"width": "350",
"height": "500",
"printwidth": "100",
"printheight": "150"
},{
"id": "3",
"isDefault": "3",
"title": "BackInside",
"viewname": "Back Inside",
"imagewidth": "351",
"imageheight": "439",
"subitemid": " 0 ",
"viewthumbnail": "imgs/cards/BackInside.png",
"container": "true",
"x": "0",
"y": "0",
"width": "350",
"height": "500",
"printwidth": "100",
"printheight": "150"
}, {
"id": "4",
"title": "Back",
"viewname": "Back",
"imagewidth": "322",
"imageheight": "422",
"subitemid": " 0 ",
"viewthumbnail": "imgs/cards/938324.Bg.png",
"container": "true",
"x": "0",
"y": "0",
"width": "381",
"height": "479",
"printwidth": "10",
"printheight": "15"
}
],
"Sizes": {
"size": [
{
"id": "22",
"isDefault": "22",
"title": "A4",
"sizeprice": "2000"
}, {
"id": "23",
"title": "A5",
"sizeprice": "1500"
}, {
"id": "21",
"title": "A6",
"sizeprice": "1000"
}
]
}
}
}
]
}
}
]
}
]
}
]
}
}
[/text]
My problems are how to make the below parts dynamic from mysql database (probably using PHP)

[text]
"Category": [
{
"id": "7",
"title": "Women",
"isDefault": "7",
[/text]
[text]
"products": [
{
"isDefault": "25",
"id": "25",
"title": "Men Tshirt",
"thumbnail": "imgs/cards/61946.Thumb.jpg",
"rawprice": "0",
[/text]
[text]
"Sizes": {
"size": [
{
"id": "22",
"isDefault": "22",
"title": "A4",
"sizeprice": "2000"
}, {
"id": "23",
"title": "A5",
"sizeprice": "1500"
}, {
"id": "21",
"title": "A6",
"sizeprice": "1000"
}
]
}
[/text]

Thanks

Re: How do I update a json file with mysql data using php

Posted: Fri Jan 02, 2015 6:41 pm
by requinix
Build that data in a PHP array. Or object. Then use json_encode with it.

For example, the "Sizes" sub-array would look like

Code: Select all

"Sizes" => array(
	"size" => array(
		array(
			"id" => "22",
			"isDefault" => "22",
			"title" => "A4",
			"sizeprice" => "2000"
		),
		array(
			"id" => "23",
			"title" => "A5",
			"sizeprice" => "1500"
		),
		array(
			"id" => "21",
			"title" => "A6",
			"sizeprice" => "1000"
		),
	)
)
(which you build according to data you get from the database)

Do you already have the queries and such you need to get the data into PHP, JSON aside?

Re: How do I update a json file with mysql data using php

Posted: Sat Jan 03, 2015 4:10 am
by adsegzy
Thanks requinix,

Yes i know how to query out the id, title & sizeprice from the DB. Let me give it a try.