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?

Lewis Thomas
  • 129
  • 11
  • 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: http://stackoverflow.com/questions/1258743/normalization-in-mysql – Keloo Mar 15 '15 at 11:31

1 Answers1

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
}
nik.longstone
  • 244
  • 2
  • 8
  • 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. – Lewis Thomas Mar 15 '15 at 18:02
  • 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) – nik.longstone Mar 15 '15 at 18:46
  • 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 – Lewis Thomas Mar 15 '15 at 18:50
  • It's not really difficult, see this other [answer](http://stackoverflow.com/questions/10060721/pdo-mysql-insert-multiple-rows-in-one-query) and if my answer was helpful mark it as that. – nik.longstone Mar 15 '15 at 19:26