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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
adsegzy
Forum Contributor
Posts: 184
Joined: Tue Jul 28, 2009 9:26 am

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

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post 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?
adsegzy
Forum Contributor
Posts: 184
Joined: Tue Jul 28, 2009 9:26 am

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

Post by adsegzy »

Thanks requinix,

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