jigsawsoul Posted January 25, 2010 Share Posted January 25, 2010 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 More sharing options...
Zane Posted January 25, 2010 Share Posted January 25, 2010 seems as if the foreach only works once foreach($_POST["selectedstaff"] as $staff_id) { } Given the very little information... I can only assume that $_POST['selectedstaff'] has only one key.. Link to comment https://forums.phpfreaks.com/topic/189670-foreach-is-not-looping/#findComment-1001014 Share on other sites More sharing options...
MadTechie Posted January 25, 2010 Share Posted January 25, 2010 It only loops once because during the first loop you redirect to another page header("Location: list.php"); Link to comment https://forums.phpfreaks.com/topic/189670-foreach-is-not-looping/#findComment-1001015 Share on other sites More sharing options...
Zane Posted January 25, 2010 Share Posted January 25, 2010 It only loops once because during the first loop you redirect to another page header("Location: list.php"); That probably has a lot to do with it.. Link to comment https://forums.phpfreaks.com/topic/189670-foreach-is-not-looping/#findComment-1001017 Share on other sites More sharing options...
jigsawsoul Posted January 25, 2010 Author Share Posted January 25, 2010 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 ) Link to comment https://forums.phpfreaks.com/topic/189670-foreach-is-not-looping/#findComment-1001022 Share on other sites More sharing options...
jigsawsoul Posted January 25, 2010 Author Share Posted January 25, 2010 no one have the answer ? Link to comment https://forums.phpfreaks.com/topic/189670-foreach-is-not-looping/#findComment-1001026 Share on other sites More sharing options...
MadTechie Posted January 25, 2010 Share Posted January 25, 2010 Bumping already! Do some debugging.. narrow down the point of error! Link to comment https://forums.phpfreaks.com/topic/189670-foreach-is-not-looping/#findComment-1001028 Share on other sites More sharing options...
jigsawsoul Posted January 25, 2010 Author Share Posted January 25, 2010 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 [attachment deleted by admin] Link to comment https://forums.phpfreaks.com/topic/189670-foreach-is-not-looping/#findComment-1001038 Share on other sites More sharing options...
jigsawsoul Posted January 25, 2010 Author Share Posted January 25, 2010 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]" ); } Link to comment https://forums.phpfreaks.com/topic/189670-foreach-is-not-looping/#findComment-1001044 Share on other sites More sharing options...
jigsawsoul Posted January 25, 2010 Author Share Posted January 25, 2010 the query is $query = "INSERT INTO web_roles (occurrence_id, staff_id, role) VALUES ('$occurrenceid', '$staff_id', 'Staff')"; :'( Link to comment https://forums.phpfreaks.com/topic/189670-foreach-is-not-looping/#findComment-1001045 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.