0

I am attempting to insert an array of image information into my database. The array consists of a ton of image information and originally i started with just allowing 3 images out of the array to be entered into the database. The images detail goes into a table called print and each of the three image urls has a column of their own to go into, along with a caption for each image, the date the image was taken and its location. All of this info goes into one row with the users id.

// Assign images and data to appropriate variables
                    $image_one = $feed['0']['images']['standard_resolution']['url'];
                    $image_one_caption = htmlspecialchars($feed['0']['caption']['text'], ENT_QUOTES);
                    $image_one_date = $feed['0']['created_time'];
                    $image_one_location = $feed['0']['location']['name'];

                    $image_two = $feed['1']['images']['standard_resolution']['url'];
                    $image_two_caption = htmlspecialchars($feed['1']['caption']['text'], ENT_QUOTES);
                    $image_two_date = $feed['1']['created_time'];
                    $image_two_location = $feed['1']['location']['name'];

                    $image_three = $feed['2']['images']['standard_resolution']['url'];
                    $image_three_caption = htmlspecialchars($feed['2']['caption']['text'], ENT_QUOTES);
                    $image_three_date = $feed['2']['created_time'];
                    $image_three_location = $feed['2']['location']['name'];

However I now want to make it so that the number of prints entered into the table can vary from 3 up to 10 as a maximum. I'm wondering about the best way to do this. I could simply assign the array values to more variables in the way shown above, however this is bulky and I feel unnecessarily repetitive.

I was thinking about a foreach loop, however I'm uncertain of how to insert each image and each images accompanying data into the correct columns of the users print row.

The other option would be to create a separate print_info table and store the users id and print_job number, then in the print table purely have the image info in separate rows with a print_job column to link the print_job to the users print_info row. is this the best way forward with this little problem?

1
  • Always use loops in situations where you have more than 1 repetitive jobs to do. You need to separate image_info table from the user only if different users can have the same picture. It's called normalization, more info here: stackoverflow.com/questions/1258743/normalization-in-mysql Commented Mar 15, 2015 at 11:31

1 Answer 1

1

What about this:

$numOfImages = 4; //the number you want
for ($i=0; $i < $numOfImages; $i++) {
    $image = $feed[$i]['images']['standard_resolution']['url'];
    $image_caption = htmlspecialchars($feed[$i]['caption']['text'], ENT_QUOTES);
    $image_date = $feed[$i]['created_time'];
    $image_location = $feed[$i]['location']['name'];

    //...operation with $image
}
Sign up to request clarification or add additional context in comments.

4 Comments

this is what I was thinking, however should I have a separate insert query for each image, or try to insert all of the images in one query? like i said it will be a maximum of 10-15 images so 10-15 insert queries if it's one each.
in the example above separate it's a separate query for each array, or you could store the Insert statement in an array and at the end perform the bulk insertion. INSERT INTO table VALUES (imgA1, imgA2, imgA3), (imgB1, imgB2, imgB3)
thanks nik, i'm just looking at doing multiple row inserts in one PDO statement and looks quite bulky. will give it a go. I may do a separate query for each as like i said it will be max 10-15 inserts with only 4 columns per insert so not a massive amount of work going on
It's not really difficult, see this other answer and if my answer was helpful mark it as that.

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.