[SOLVED] Starting a loop within an IF statement.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

[SOLVED] Starting a loop within an IF statement.

Post by impulse() »

I want some code that starts a loop depending on a _GET value. I want it to start with

Code: Select all

for ($i = 1; $i < 21; $i++) {
if a _GET value is a certain value and run

Code: Select all

for ($i = 1; $i < 11; $i++) {
if it's anything else. I'm getting an "unexpected T_ELSE" error at the moment with this code:

Code: Select all

if (isset($_GET["addMore"])) {
      for($i = 1; $i < 21; $i++) {
    }
    else {
      for ($i = 1; $i < 11; $i++) {
    }

        if ($i > 9) {
          echo "$i<input type = 'text' name = 'groupCont{$i}' size = '50'> <br>";
        }
        else {
          echo "$i&nbsp; <input type = 'text' name = 'groupCont{$i}' size = '50'> <br>"; }
      }

    echo "</td> </tr> <tr> <td colspan = '2' bgcolor = '$col'>";

      echo "<center> <input type = 'submit' value = 'Add Group'>";

    echo "</td> </tr>";

    echo "</form>";
  echo "</table>";

}
Any ideas here? Its as if PHP is getting confused with the opening brace within the IF loop.

Regards,
Last edited by impulse() on Thu Feb 08, 2007 7:13 am, edited 1 time in total.
mickd
Forum Contributor
Posts: 397
Joined: Tue Jun 21, 2005 9:05 am
Location: Australia

Post by mickd »

Not sure if i get exactly what you're asking but you could try

Code: Select all

if(isset($_GET['value'])) {
$n = 22;
} else {
$n = 11;
}

for($i = 1; $i < $n; $i++) {

// stuff

}
EDIT: You're welcome :)
Last edited by mickd on Thu Feb 08, 2007 7:15 am, edited 1 time in total.
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

That would make much more sense :)

I hate myself when I don't see really simple solutions. That solution should've been the first thing to pop in my head.

Thank you anyway.
Post Reply