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 mOrloff's Avatar
    Join Date
    Nov 2008
    Location
    The Great Pacific NW, USA
    Posts
    422
    Thanks
    8
    Thanked 6 Times in 6 Posts

    How can I use a FOREACH conditionally ??

    I have a table of employees.
    Under normal circumstances I loop through the whole array and gather data for all employees, however, if an employee is specified via $_GET, then I want to limit the scope to that single emp.

    Here's a simplified example:
    PHP Code:
    $empArray=array(1=>"Bob",2=>"Sally"3=>"Dick"4=>"Harry"); 

    if(empty(
    $_GET['empId'])){  // if NO emp is specified...
        
    foreach($empArray as $empId=>$name){  //...go into a loop
    }else{  //  if an emp IS specified...
        
    $empID=;$_GET['empId']  //...grab it
    }
    echo 
    "ID: $empId <br/>";
    echo 
    "Name: $name <br/>";

    if(empty(
    $_GET['empId'])){  // if NO emp is specified..
        
    }  //...close the FOREACH() loop

    I am getting an error on the }else{ line.
    How can this be done??
    How can I use the foreach() only if certain criteria are met??

    Thanks-a-bunch,
    ~ Mo

  • #2
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    You can't start and finish within different conditionals like that.

    Code:
    $empArray=array(1=>"Bob",2=>"Sally", 3=>"Dick", 4=>"Harry"); 
    
    if (empty($_GET['empId']))
    {
        foreach($empArray as $empId=>$name)
        {
            echo "ID: $empId <br/>";
            echo "Name: $name <br/>";
        }
    }
    else
    {
        $empID=$_GET['empId'];
        echo "ID: $empId <br/>";
        echo "Name: $name <br/>";
    }

  • #3
    Regular Coder mOrloff's Avatar
    Join Date
    Nov 2008
    Location
    The Great Pacific NW, USA
    Posts
    422
    Thanks
    8
    Thanked 6 Times in 6 Posts
    Aww man!!!
    I've got about 150 lines of code which are now going to have to be duplicated.

    That would have been a SLICK way to save some unnecessary redundancy.
    Thanks.
    ~ Mo

  • #4
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by mOrloff View Post
    I've got about 150 lines of code which are now going to have to be duplicated.
    Why? That's what functions are for.

  • #5
    Regular Coder mOrloff's Avatar
    Join Date
    Nov 2008
    Location
    The Great Pacific NW, USA
    Posts
    422
    Thanks
    8
    Thanked 6 Times in 6 Posts
    Quote Originally Posted by MattF View Post
    Why? That's what functions are for.
    Very good point. Thank you.

    As an aside, I did figure out a way to make it work.
    Simply, if $_GET['empId'] was passed, drop all other emps from the $empsArray array. Therefor the foreach() would only have one record to do its thing for.

    Thank you again.
    ~ Mo


  •  

    Posting Permissions

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