Jump to content

ForEach 1 to 10?


Recommended Posts

I'm trying to do a multiple row insert from a form using field names Name1, Name2, Description1, Description2, etc... How do I build something like a ForEach 1 to 10 statement to use for my insert. I was going try something like

 

Foreach i 1 to 10

insert Description & i

etc..

 

don't know the syntax of course but from what I found I had to build an array then use the array in the foreach command. Is there no way to tell it 1 to 10 or something like that?

 

Thanks

 

Link to comment
Share on other sites

I'm trying to do a multiple row insert from a form using field names Name1, Name2, Description1, Description2, etc... How do I build something like a ForEach 1 to 10 statement to use for my insert.  I was going try something like

 

Foreach i 1 to 10

insert Description & i

etc..

 

don't know the syntax of course but from what I found I had to build an array then use the array in the foreach command.  Is there no way to tell it 1 to 10 or something like that?

 

Thanks

273630[/snapback]

 

if you know how many fields there are, use for() instead of foreach():

[!--PHP-Head--][div class=\'phptop\']PHP[/div][div class=\'phpmain\'][!--PHP-EHead--]

[span style=\"color:#0000BB\"]<?php

[/span][span style=\"color:#007700\"]for ([/span][span style=\"color:#0000BB\"]$i [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]1[/span][span style=\"color:#007700\"]; [/span][span style=\"color:#0000BB\"]$i [/span][span style=\"color:#007700\"]<= [/span][span style=\"color:#0000BB\"]10[/span][span style=\"color:#007700\"]; [/span][span style=\"color:#0000BB\"]$i[/span][span style=\"color:#007700\"]++) {

  echo [/span][span style=\"color:#0000BB\"]$_POST[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\"Name$i\"[/span][span style=\"color:#007700\"]];

}

[/span][span style=\"color:#0000BB\"]?>

[/span]

[/span][!--PHP-Foot--][/div][!--PHP-EFoot--]

Link to comment
Share on other sites

Thanks, that's exactly what I needed. I appreciate it.

 

 

if you know how many fields there are, use for() instead of foreach():

[!--PHP-Head--][div class=\'phptop\']PHP[/div][div class=\'phpmain\'][!--PHP-EHead--]

[span style=\\\"color:#0000BB\\\"]<?php

for [/span][span style=\\\"color:#007700\\\"]([/span][span style=\\\"color:#0000BB\\\"]$i [/span][span style=\\\"color:#007700\\\"]=[/span][span style=\\\"color:#0000BB\\\"] 1[/span][span style=\\\"color:#007700\\\"];[/span][span style=\\\"color:#0000BB\\\"] $i [/span][span style=\\\"color:#007700\\\"]<=[/span][span style=\\\"color:#0000BB\\\"] 10[/span][span style=\\\"color:#007700\\\"];[/span][span style=\\\"color:#0000BB\\\"] $i[/span][span style=\\\"color:#007700\\\"]++)[/span][span style=\\\"color:#0000BB\\\"] [/span][span style=\\\"color:#007700\\\"]{

[/span][span style=\\\"color:#0000BB\\\"]  echo $_POST[/span][span style=\\\"color:#007700\\\"][[/span][span style=\\\"color:#DD0000\\\"]\\\"Name$i\\\"[/span][span style=\\\"color:#007700\\\"]];

}

[/span][span style=\\\"color:#0000BB\\\"]?>[/span]

[/span][!--PHP-Foot--][/div][!--PHP-EFoot--]

273636[/snapback]

 

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.