Reputation: 11
I am a student PHP Developer and i am making a web app. So i have a problem with inserting JSON data in a database table.
This is my JSON code:
[
{
"HouseCode": "XX-00000-70",
"MediaV2": [
{
"Type": "Photos",
"TypeContents": [
{
"SequenceNumber": 1,
"Tag": "ExteriorSummer",
"Versions": [
{
"Height": 1365,
"Width": 2048,
"URL": "http://image1.com/2048x1365"
},
{
"Height": 40,
"Width": 53,
"URL": "http://imagex.com/2048x1365"
}
]
},
{
"SequenceNumber": 2,
"Tag": "ExteriorSummer",
"Versions": [
{
"Height": 1365,
"Width": 2048,
"URL": "http://image2.com/2048x1365"
},
{
"Height": 40,
"Width": 53,
"URL": "http://imagex.com/2048x1365"
}
]
}
]
}
]
},
{
"HouseCode": "XX-00000-71",
"MediaV2": [
{
"Type": "Photos",
"TypeContents": [
{
"SequenceNumber": 1,
"Tag": "ExteriorSummer",
"Versions": [
{
"Height": 1365,
"Width": 2048,
"URL": "http://image1b/2048x1365"
},
{
"Height": 40,
"Width": 53,
"URL": "http://via.placeholder.com/53x40"
}
]
},
{
"SequenceNumber": 2,
"Tag": "LivingRoom",
"Versions": [
{
"Height": 1365,
"Width": 2048,
"URL": "http://image2b/2048x1365"
},
{
"Height": 40,
"Width": 53,
"URL": "http://via.placeholder.com/53x40"
}
]
}
]
}
]
}
]
And this is my database table:
+-----------+--------+--------+
| HouseCode | Image1 | Image2 |
+-----------+--------+--------+
| | | |
+-----------+--------+--------+
| | | |
+-----------+--------+--------+
The intention is to import each image with the format: 'Height: 1365, Width: 2048' of any HouseCode into the database.
Like this:
+-------------+-----------------------------+-----------------------------+
| HouseCode | Image1 | Image2 |
+-------------+-----------------------------+-----------------------------+
| XX-00000-70 | http://image1.com/2048x1365 | http://image2.com/2048x1365 |
+-------------+-----------------------------+-----------------------------+
| XX-00000-71 | http://image1b/2048x1365 | http://image2b/2048x1365 |
+-------------+-----------------------------+-----------------------------+
I tried this:
//read the json file contents
$jsondata = file_get_contents('import/json/MediaV2.json');
//convert json object to php associative array
$datas = json_decode($jsondata, true);
//Foreach loop
foreach ($datas as $data) {
$housecode = $data['HouseCode'];
if($data = (['MediaV2']['TypeContents']['Versions']['Height'] = '1365')){
$pictures = $data['MediaV2']['TypeContents']['Versions']['URL'];
}
//insert into mysql table
DB::insert("INSERT INTO mediav2(HouseCode, photo1, photo2)
VALUES('$housecode', '$pictures', '$pictures')");
}
What am i doing wrong? who can help me?
Thanks in advance!
P.S. Sorry for my bad english..
Upvotes: 1
Views: 55
Reputation: 32232
This is nonsense:
if($data = (['MediaV2']['TypeContents']['Versions']['Height'] = '1365'))
// ^- this is not how array access works ^
// this is assignment, not comparison ┚
This is the correct way to do it:
if($data['MediaV2']['TypeContents']['Versions']['Height'] == '1365')
Upvotes: 1