PHP School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a PHP Expert!

Join 300,430 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,498 people online right now. Registration is fast and FREE... Join Now!




Can't find my way into a loop

 

Can't find my way into a loop, I have a question about a foreach loop that my own logic is not able t

jnkrois

3 Jul, 2009 - 09:18 PM
Post #1

New D.I.C Head
*

Joined: 5 Mar, 2008
Posts: 9


My Contributions
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


User is offlineProfile CardPM
+Quote Post


noorahmad

RE: Can't Find My Way Into A Loop

3 Jul, 2009 - 10:27 PM
Post #2

Webmaster
Group Icon

Joined: 12 Mar, 2009
Posts: 2,018



Thanked: 125 times
Dream Kudos: 1350
My Contributions
here is how to upload multi files and insert data into table
php
<?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 smile.gif
User is online!Profile CardPM
+Quote Post

jnkrois

RE: Can't Find My Way Into A Loop

4 Jul, 2009 - 05:44 AM
Post #3

New D.I.C Head
*

Joined: 5 Mar, 2008
Posts: 9


My Contributions
QUOTE(noorahmad @ 3 Jul, 2009 - 10:27 PM) *

here is how to upload multi files and insert data into table
php
<?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 smile.gif


Thank you very much noorahmad, it worked perfect.
User is offlineProfile CardPM
+Quote Post

noorahmad

RE: Can't Find My Way Into A Loop

4 Jul, 2009 - 07:39 PM
Post #4

Webmaster
Group Icon

Joined: 12 Mar, 2009
Posts: 2,018



Thanked: 125 times
Dream Kudos: 1350
My Contributions
Your Welcome smile.gif
User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 12:33AM

Live PHP Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month