Can't find my way into a loop

I have a question about a foreach loop that my own logic is not able t

Page 1 of 1

3 Replies - 934 Views - Last Post: 04 July 2009 - 08:39 PM Rate Topic: -----

#1 jnkrois  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 05-March 08

Can't find my way into a loop

Post icon  Posted 03 July 2009 - 10:18 PM

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:
<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:
$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:
// 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
$_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

Is This A Good Question/Topic? 0
  • +

Replies To: Can't find my way into a loop

#2 noorahmad  Icon User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Re: Can't find my way into a loop

Posted 03 July 2009 - 11:27 PM

here is how to upload multi files and insert data into table
<?PHP
$i = 0;
foreach ($_FILES["file"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["file"]["tmp_name"][$key];
        $name = $_FILES["file"]["name"][$key];
        move_uploaded_file($tmp_name, "$name");
		$cap = $_POST['cap'][$i];
		$str= "Insert into tbl1 value($cap,$name)";
		$i++;
    }
}
?>


hope it help you :)
Was This Post Helpful? 0
  • +
  • -

#3 jnkrois  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 05-March 08

Re: Can't find my way into a loop

Posted 04 July 2009 - 06:44 AM

View Postnoorahmad, on 3 Jul, 2009 - 10:27 PM, said:

here is how to upload multi files and insert data into table
<?PHP
$i = 0;
foreach ($_FILES["file"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["file"]["tmp_name"][$key];
        $name = $_FILES["file"]["name"][$key];
        move_uploaded_file($tmp_name, "$name");
		$cap = $_POST['cap'][$i];
		$str= "Insert into tbl1 value($cap,$name)";
		$i++;
    }
}
?>


hope it help you :)


Thank you very much noorahmad, it worked perfect.
Was This Post Helpful? 0
  • +
  • -

#4 noorahmad  Icon User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Re: Can't find my way into a loop

Posted 04 July 2009 - 08:39 PM

Your Welcome :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1