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 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jan 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Foreach is not looping help please

    can anyone see a problem which the below could and why it could not be looping. the array has multi number of results but seems as if the foreach only works once, help??

    the problem is i have in this example 3 staff members checked/selected and but in the array but the foreach still doesn't loop and add them all to the database.

    echo print_r displays

    Array (
    => 2 [1] => 8 [2] => 7 )




    Code:
    <?php
    
    session_start();
    
    include '../../_library/opendb.php';
    include '../../_functions/login.php';
    include '../../_functions/nav-admin.php';
    
    $result = 	"SELECT * FROM web_staff
    				LEFT JOIN web_login ON web_staff.login_id=web_login.login_id
    				WHERE userlevel = '2' ORDER BY firstname ASC";
    				
    $result = mysql_query($result) or die( mysql_error() );
    while($row = mysql_fetch_assoc($result)) 
    	{	
    	
    		if (isset($_POST["selectedstaff"])) {
    				
    			foreach($_POST["selectedstaff"] as $staff_id) {
    			
    			    if ( $staff_id == $row['staff_id'] ) {
    
    			    	$occurrenceid = $_SESSION['occurrenceid'];
    			    	
    			    	$query = "INSERT INTO web_roles (occurrence_id, staff_id, role) VALUES ('$occurrenceid', '$staff_id', 'Staff')";
    	
    					if (mysql_query ($query)) {
    						$result = 	"SELECT * FROM web_login
    										LEFT JOIN web_staff ON web_login.login_id=web_staff.login_id
    										WHERE web_staff.staff_id = $staff_id";
    						$result = mysql_query ($result) or die (mysql_error());
    						$row = mysql_fetch_assoc($result);
    	
    						$email = $row['email'];
    						$subject = "Meeting System, Meeting Notification" ;
    						$message = "You have a new meeting you are required to attend";
    							mail("$email", "$subject", $message, "From: noreply@meeting.com" );
    							
    						print_r($_POST["selectedstaff"]);
    							
    					} 
    					else {
    					 	$_SESSION['message'] = "
    								<div class='notification error png_bg'>
    									<a href='#' class='close'><img src='http://stuweb.cms.gre.ac.uk/~hr728/_web/_resources/images/icons/cross_grey_small.png' title='Close this notification' alt='close' /></a>
    									<div>
    										Error. please can't admin
    									</div>
    								</div>";
    						header('Location: list.php');
    					}
    			    }
    			}
    		}
    	}
    ?>

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,861
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    the problem is i have in this example 3 staff members checked/selected and but in the array but the foreach still doesn't loop and add them all to the database.
    How did you conclude this as an issue of loop? Try changing
    if (mysql_query ($query)) {
    $result = "SELECT * FROM web_login
    LEFT JOIN web_staff ON web_login.login_id=web_staff.login_id
    WHERE web_staff.staff_id = $staff_id";
    $result = mysql_query ($result) or die (mysql_error());
    $row = mysql_fetch_assoc($result);

    $email = $row['email'];
    $subject = "Meeting System, Meeting Notification" ;
    $message = "You have a new meeting you are required to attend";
    mail("$email", "$subject", $message, "From: noreply@meeting.com" );

    print_r($_POST["selectedstaff"]);

    }
    else {
    $_SESSION['message'] = "
    <div class='notification error png_bg'>
    <a href='#' class='close'><img src='http://stuweb.cms.gre.ac.uk/~hr728/_web/_resources/images/icons/cross_grey_small.png' title='Close this notification' alt='close' /></a>
    <div>
    Error. please can't admin
    </div>
    </div>";
    header('Location: list.php');
    }
    to
    PHP Code:
    $inserted=    mysql_query ($query) or die(mysql_error());

    if (
    $inserted) {
        
    $result =     "SELECT * FROM web_login
                        LEFT JOIN web_staff ON web_login.login_id=web_staff.login_id
                        WHERE web_staff.staff_id = $staff_id"
    ;
        
    $result mysql_query ($result) or die (mysql_error());
        
    $row mysql_fetch_assoc($result);

        
    $email $row['email'];
        
    $subject "Meeting System, Meeting Notification" ;
        
    $message "You have a new meeting you are required to attend";
            
    mail("$email""$subject"$message"From: noreply@meeting.com" );
            
        
    print_r($_POST["selectedstaff"]);
            

    else {
        
    $_SESSION['message'] = "
                <div class='notification error png_bg'>
                    <a href='#' class='close'><img src='http://stuweb.cms.gre.ac.uk/~hr728/_web/_resources/images/icons/cross_grey_small.png' title='Close this notification' alt='close' /></a>
                    <div>
                        Error. please can't admin
                    </div>
                </div>"
    ;
        
    header('Location: list.php');

    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)


  •  

    Posting Permissions

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