Processing in an array?

See all posts Reply

Processing in an array? new!
by Tim Gavin, 16 years, 11 months ago
My form is for a newsletter script that will have many articles on it, with each article having an image. The number of articles is determined by the user.

EXAMPLE
// process the form
for($i=1; $i<=count($_POST['art_headline']); $i++){
  $headline = strip_tags($_POST['art_headline']);
  $article = strip_tags($_POST['article']);
  $art_img = $_FILES['art_img'];
  $sql = mysql_query("INSERT INTO articles (art_headline, art_txt, art_img) VALUES ('{$art_headline[$i]}','{$art_txt[$i]}','{$art_img[$i}')") or die(mysql_error());
}

<form method="post" enctype="multipart/form-data">
headline: <input type="text" name="art_headline[]" />
article: <textarea name="art_text[]"></textarea>
image: <input type="file" name="art_img[]" />
</form>


How would I go about using the upload class to process the form, plus get its name and insert it into the db?Reply
Re: Processing in an array? new!
by colin, 16 years, 11 months ago
The class itself handles one uploaded file at a time. In case of multiple uploads, the $_FILES array has a different structure, so you need first to re-structure it so it can be looped through, and each element of the array used to instanciate the class.

$files = array();
foreach ($_FILES['art_img'] as $k => $l) {
 foreach ($l as $i => $v) {
 if (!array_key_exists($i, $files)) 
   $files[$i] = array();
   $files[$i][$k] = $v;
 }
}
and then:
foreach ($files as $file) {
  $handle = new Upload($file);
  if ($handle->uploaded) {
    $handle->Process("./");
    if ($handle->processed) {
      echo 'OK';
      // add your data in the database here
    } else {
      echo 'Error: ' . $handle->error;
    }
  } else {
    echo 'Error: ' . $handle->error;
  }
  unset($handle);
}
Reply
Re: Processing in an array? new!
by Tim Gavin, 16 years, 10 months ago
Thank you very much.

I'm not sure about two things.
1. // add your data in the database here
what is it that I should put there? my INSERT query?

2. To try it out quickly, I copy/pasted your code and inserted it. Then tested with an 8k file. It was uploaded over 900 times as the script went into a loop. :)Reply
Re: Processing in an array? new!
by colin, 16 years, 10 months ago
1. Yes, you can put here whatever you want to store the image name. For instance, you can INSERT into a database, using $handle->file_dst_pathname to know the name of the image. You can output $handle->log to see what are the possible variables that you can be provided with after processing an upload.

2. The code I copied in my post is only indicative. You may need to change it. How many upload fields did you have in your form? You can send me an archive of your code, and I will try to have a look at it.

I am afraid that the free support I can offer in this forum is limited to the class; your request goes a little bit beyond this scope, so my answers are quite limited to my availability.Reply
Re: Processing in an array? new!
by Tim Gavin, 16 years, 10 months ago
Thank you.

The number of upload fields is the rub: it can be as many as the user wants. In my first post, I pasted the actual INSERT query. As you can see it's in a for() loop, iterating every article that's in the form. Each new article is added dynamically via js. So, for each article POSTed there can be an image.

I was able to get your example to work, just couldn't get the value of the filename into the db.

I'll be happy to provide a donation for your help. If you'd rather have me email something to you, which address should I send it to?

Thanks again!Reply
Re: Processing in an array? new!
by colin, 16 years, 10 months ago
So I assume that you have one form, in which users can add new lines via Javascript. Then, you process the form, which may have x articles, each composed of one healine, a text and one image, and you want to store them all in a database. Is that right?

You can send me the code that you already have at colin@verot.net

As for the donations, which are always greatly appreciated, you can have more information on this page. Thank you.Reply
Re: Processing in an array? new!
by Tim Gavin, 16 years, 10 months ago
did you receive my email?Reply
Re: Processing in an array? new!
by colin, 16 years, 10 months ago
Yes, I did. I will answer ASAP.Reply
Re: Processing in an array? new!
by colin, 16 years, 10 months ago
Actually, I answered you yesterday, but your email bounced back.

I just resent the email.Reply
Re: Processing in an array? new!
by Tim Gavin, 16 years, 10 months ago
thank you.
still didn't get it. did it bounce again?Reply
Re: Processing in an array? new!
by Tim Gavin, 16 years, 10 months ago
Hi Colin,

I received your file, thank you.

I sent a donation to Paypal. Wanted to let you know that the thank you page (http://www.verot.net/paypal_thank_you.htm) generates a not found error in the page. Not sure if it's a 404 or not, but thought you should know.

Still having a little trouble getting it configured, but it's coming along. Some parts work, some don't. Could just be because I'm sick and fuzzy in the head :(

Thanks for your help :)Reply
Re: Processing in an array? new!
by colin, 16 years, 10 months ago
Thank you very much for the donation, it is much appreciated.

And thanks for letting me know about the Paypal feedback page. it is now fixed.

As for your script, let me know how you get on, and whether you need more help.Reply