Page 1 of 1

[SOLVED] Starting a loop within an IF statement.

Posted: Thu Feb 08, 2007 6:42 am
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,

Posted: Thu Feb 08, 2007 6:59 am
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 :)

Posted: Thu Feb 08, 2007 7:13 am
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.