Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Mar 2010
    Posts
    235
    Thanks
    39
    Thanked 6 Times in 6 Posts

    For Loop Start/End Syntax inside an if Statement

    I have a loop that needs to execute twice. The meat of the code acts the same inside both loops, so I wanted to save code and just make a dynamic start syntax. The following two blocks both bomb. When I do a regular loop with either the first or second it works.

    PHP Code:
    if ($matmaster==1)
    {
    for (
    $tcol=1;$tcol<=$rowmatrix1;$tcol++)
    }
    else
    {
    for (
    $tcol=$tcolstart;$tcol>=1;$tcol--)
    }

    PHP Code:
    if ($matmaster==1)
    {
    for (
    $tcol=1;$tcol<=$rowmatrix1;$tcol++) {
    }
    else
    {
    for (
    $tcol=$tcolstart;$tcol>=1;$tcol--) {

    Is there a way to do the above, or, is there a way to make the for statement a string to be executed as code?

  • #2
    New to the CF scene
    Join Date
    Jul 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You have syntax errors in both, try the following...
    if ($matmaster==1)
    {
    for ($tcol=1;$tcol<=$rowmatrix1;$tcol++) {
    }
    }
    else
    {
    for ($tcol=$tcolstart;$tcol>=1;$tcol--) {
    }
    }

  • #3
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    These both bomb because your {} are unmatched.
    PHP Code:
    if () {
      for() {
      }
    } else {
      for () {
      }

    that's what you need. You're missing braces.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Loops cannot be logically intersected by any type of control. This applies in every language I've used.
    Use your controls to declare new variables to be used within the loop condition.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    mathceleb (07-01-2010)

  • #5
    Regular Coder
    Join Date
    Mar 2010
    Posts
    235
    Thanks
    39
    Thanked 6 Times in 6 Posts
    I only wanted the loop to start in the if statement, not end.

    The end is later on down the road. From the post above, it sounds like it cannot be done, thanks!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •