Jump to content

FOREACH IS NOT LOOPING


jigsawsoul

Recommended Posts

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

 

<?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: [email protected]" );

					 $_SESSION['message'] = "
						<div class='notification success 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>
								Meeting has been created and all staff have been informed
							</div>
						</div>";
						header("Location: list.php");

				} 
				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');
				}
		    }
		}
	}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/189670-foreach-is-not-looping/
Share on other sites

redirect i have taken out...

 

there is still a problem 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.

 

<?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: [email protected]" );

					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');
				}
		    }
		}
	}
}
?>

 

echo print_r displays

 

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

 

:wtf:

 

 

 

Here's a smaller version

 

on the form i selected the person with the staff_id which equals 8, on the next page i do

print_r($_POST["selectedstaff"])

it shows me the array with the staff_id 8

 

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

 

just goes straight to the else  :shrug:

 

[attachment deleted by admin]

the problem is in this part of the code without this part of the code it works fine and i can echo out the ideas. once hitting this part of the code it stops looping ?? but i can't see anything wrong, i can have foreach with a if statement inside and an if statement inside that statement right?

 

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: [email protected]" );

				}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.