Jump to content

Foreach statement - getting myself in a mess


c_shelswell

Recommended Posts

I'm trying to add a number of fields to mysql but getting myself in a muddle with the foreach statement.

I've got a whole load of postdata

like: array ([title1]=> "title", [picture1]=> "pic1.jpg", [title2]=>"title2", [picture2]=> "pic2.jpg",)

etc. There's acutally a whole load more data than this. I was hoping to write a foreach to add from title to pic and whatever else is inbetween then start a new query for title2. is this possible? i couldn't figure it out using foreach. At the moment i'm just creating a variable for each one i.e. $title1 = $_POST['title1'] but it's a little long winded.

thanks for any help
Link to comment
Share on other sites

If you just want to get all of the variable out of $_POST, use extract.

http://www.php.net/extract

If you are wanting to use a loop for the number of pictures:

[code]$picture_count = 10;
for ($a = 1; $a <= $picture_count; $a++) {
  echo "title" . $a . ": " . $_POST['title' . $a] . " - picture" . $a . ": " . $_POST['picture' . $a] . "<br />";
}[/code]

That's the general idea anyway.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.