Jump to content

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: noreply@meeting.com" );

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

 

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: noreply@meeting.com" );

				}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.