Jump to content


Photo

ForEach 1 to 10?


  • Please log in to reply
3 replies to this topic

#1 barkster

barkster
  • Members
  • PipPipPip
  • Advanced Member
  • 194 posts

Posted 01 September 2005 - 04:56 PM

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


#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 01 September 2005 - 05:28 PM

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

View Post


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--]
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 barkster

barkster
  • Members
  • PipPipPip
  • Advanced Member
  • 194 posts

Posted 01 September 2005 - 06:12 PM

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--]

View Post



#4 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 01 September 2005 - 06:51 PM

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

View Post


that's what we're here for! ;-) i'll mark this topic solved
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users