...

View Full Version : Foreach is not looping help please



jigsawsoul
01-25-2010, 02:02 AM
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 )


:confused:


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

abduraooft
01-25-2010, 08:20 AM
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

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum