1

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

1 Answer 1

1

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')
Sign up to request clarification or add additional context in comments.

Comments

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.