Jump to content

Archived

This topic is now archived and is closed to further replies.

barkster

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

 

Share this post


Link to post
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--]

Share this post


Link to post
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]

 

Share this post


Link to post
Share on other sites
Thanks, that's exactly what I needed.  I appreciate it.

273657[/snapback]

 

that's what we're here for! ;-) i'll mark this topic solved

Share this post


Link to post
Share on other sites

×

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.