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
https://forums.phpfreaks.com/topic/2478-foreach-1-to-10/
Share on other sites

  Quote
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
https://forums.phpfreaks.com/topic/2478-foreach-1-to-10/#findComment-8231
Share on other sites

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

 

 

  Quote
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
https://forums.phpfreaks.com/topic/2478-foreach-1-to-10/#findComment-8233
Share on other sites

Archived

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

×
×
  • 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.