Hello guys, it's always nice to read the forum and realize that there's help out there for us begginers.
I have a problem with a
foreach loop and I just can't seem to find an answer for it, I'm sure there is a way around this but I'm out of ideas.
My issue is the following:
I have a HTML form that submits both data (text strings) and image files, the input files are as follows:
CODE
<input type="file" name="myInputs[]" />
<input type="text" name="myCaptions[]" />
This approach allows me to initialize an array (separate form each other) for both the files and the text strings, to be a little more specific, what I'm doing is uploading pictures for a gallery and the pictures might have captions.
The php scripts that process the form uses:
CODE
$name = $_FILES["myInputs"]["name"][$key];
$myCaptions = $_POST["myCaptions"];
I'm using a foreach loop to retrieve the file names and upload them to the server, but at the same time the form passes other data that goes to the database. so far so good.
My problem starts when I use the foreach loop to handle multiple uploads and data at the same time and inside the loop I have a mysql_query that writes the info about the picture to the gallery. See the loop below:
CODE
// start the loop and handle the files from the form
foreach ($_FILES["myInputs"]["error"] as $key => $error){
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["myInputs"]["tmp_name"][$key];
$name = $_FILES["myInputs"]["name"][$key];
move_uploaded_file($tmp_name, "$uploads_dir/$name");
// retrieve the rest of the data from the form and add it to the db
mysql_query("INSERT INTO gallery_pictures (date, title, description, thumbnail, image, caption) VALUES('$full_date', '$title', '$description', '$full_path$name', '$full_path$name', 'captions' ) ")
or die(mysql_error());
}
}
This chunk of code works as expected, the specific problem is that in order to retrieve the secondary array
CODE
$_POST["myCaptions"];
I have to run another foreach loop, basically the caption for each picture, but I need to add it to the database along with the mysql_query inside the first loop.
I guess my question is: How can I pass the values from a foreach loop into another foreach loop, so I can have one single row in the database with all the info from the input fields, matching each picture with it's captions.
Tried several things but I was not able to get the data in the right fields.
I appreciate any help I can get, or if my explanation is not clear I'll try to rephrase.
Thanks a lot guys and keep up the good work.
Jnk