New Coder Posted October 20, 2009 Share Posted October 20, 2009 Hello all, First time I'm attempting to use a foreach statement and was just wondering if my code is correct: $sql = "SELECT email from mail_list where subcribed = \"Y\" "; $rs = mssql_query( $sql, $conn ) or die( "Err EXE" ); $row = mssql_fetch_array( $rs ); foreach($row["email"] as $email) { $to = "$email"; $re = "Newsletter"; $msg = "The latest Newsletter is ready for download \n\nThank You"; $headers = "From: No-Reply@doamin.com \r\n"; } Hard to test as I dont want to start sending out rouge emails Many Thanks Quote Link to comment https://forums.phpfreaks.com/topic/178332-solved-foreach-help/ Share on other sites More sharing options...
trq Posted October 20, 2009 Share Posted October 20, 2009 mssql_fetch_array returns one record as an array of fields then moves the cursor to the next record. So your foreach will return one record only. You need a while loop. $sql = "SELECT email from mail_list where subcribed = \"Y\" "; if ($rs = mssql_query( $sql, $conn )) { if (mssql_num_rows($rs)) { $re = "Newsletter"; $msg = "The latest Newsletter is ready for download \n\nThank You"; $headers = "From: No-Reply@doamin.com \r\n"; while ($row = mssql_fetch_array($rs)) { $to = $row['$email']; // call mail() } } } Quote Link to comment https://forums.phpfreaks.com/topic/178332-solved-foreach-help/#findComment-940334 Share on other sites More sharing options...
New Coder Posted October 20, 2009 Author Share Posted October 20, 2009 Thanks Thorpe. I have added your code but I'm worried it will try and insert the record into the published table and send a confirmation email to the uploader for each subscriber found? Heres codes as it is now <?php $newsletter_id = $_POST['newsletter_id']; $uplaoder = $_POST['admin_name']; $sent = $_POST['sent']; include("../php_include/connection.php"); if($sent) { $valid=true; } if( $valid != true) { echo( "Error - Data not sent from main form." ); } else { $sql = "SELECT email from mail_list where subcribed = \"Y\" "; if ($rs = mssql_query( $sql, $conn )) { if (mssql_num_rows($rs)) { $re = "Newsletter"; $msg = "The latest Newsletter is ready for download \n\nThank You"; $headers = "From: No-Reply@doamin.com \r\n"; while ($row = mssql_fetch_array($rs)) { $to = $row['$email']; } } } if( mail( $to, $re, $msg, $headers ) ) { $sql2 = "insert into published (newsletter_id, published_by) values (\"$newsletter_id\", \"$uploader\") "; $rs2 = mssql_query( $sql2, $conn ) or die( "Err EXE 2"); $to = "$uploader@northamptoncollege.com"; $re = "Newsletter Email Sent"; $msg = "Distribution confirmed \n\nThank You"; $headers = "From: No-Reply@domain.com \r\n"; if( mail( $to, $re, $msg, $headers ) ) { echo("Thank you, An email has been sent to all subcribers plus confirmation to yourself.<br><form action=\"published_newsletters.php\"><input type=\"submit\" name=\"back\" value=\"Back\"></form>"); } else { echo("Error.....Email not sent to Uploader. <br>Please go back and check the record has been saved, if so emails have been sent to subscribers.<br><form action=\"published_newsletters.php\"><input type=\"submit\" name=\"back\" value=\"Back\"></form>"); } } else { echo("Error.....Email not sent and record not saved. <br>Please go back and try again.<br><form action=\"send_newsletter.php\"><input type=\"submit\" name=\"back\" value=\"Back\"></form>"); } } ?> and before, which would only send to 1 subscriber. <?php $newsletter_id = $_POST['newsletter_id']; $uplaoder = $_POST['admin_name']; $sent = $_POST['sent']; include("../php_include/connection.php"); if($sent) { $valid=true; } if( $valid != true) { echo( "Error - Data not sent from main form." ); } else { $sql = "SELECT email from mail_list where subcribed = \"Y\" "; $rs = mssql_query( $sql, $conn ) or die( "Err EXE" ); $row = mssql_fetch_array( $rs ); $email = $row["email"]; $to = "$email"; $re = "Newsletter"; $msg = "The latest Newsletter is ready for download \n\nThank You"; $headers = "From: No-Reply@domain.com \r\n"; if( mail( $to, $re, $msg, $headers ) ) { $sql2 = "insert into published (newsletter_id, published_by) values (\"$newsletter_id\", \"$uploader\") "; $rs2 = mssql_query( $sql2, $conn ) or die( "Err EXE 2"); $to = "$uploader@domain.com"; $re = "Newsletter Email Sent"; $msg = "Distribution confirmed \n\nThank You"; $headers = "From: No-Reply@domain.com \r\n"; if( mail( $to, $re, $msg, $headers ) ) { echo("Thank you, An email has been sent to all subcribers plus confirmation to yourself.<br><form action=\"published_newsletters.php\"><input type=\"submit\" name=\"back\" value=\"Back\"></form>"); } else { echo("Error.....Email not sent to Uploader. <br>Please go back and check the record has been saved, if so emails have been sent to subscribers.<br><form action=\"published_newsletters.php\"><input type=\"submit\" name=\"back\" value=\"Back\"></form>"); } } else { echo("Error.....Email not sent and record not saved. <br>Please go back and try again.<br><form action=\"send_newsletter.php\"><input type=\"submit\" name=\"back\" value=\"Back\"></form>"); } } ?> I hope I'm making sense. Many Thanks Quote Link to comment https://forums.phpfreaks.com/topic/178332-solved-foreach-help/#findComment-940388 Share on other sites More sharing options...
cags Posted October 20, 2009 Share Posted October 20, 2009 If your ever worried about sending out lots of e-mails whilst trying to test code, just comment out the call to mail() and echo out any information you need to check. Quote Link to comment https://forums.phpfreaks.com/topic/178332-solved-foreach-help/#findComment-940396 Share on other sites More sharing options...
New Coder Posted October 20, 2009 Author Share Posted October 20, 2009 I have cracked it. Thank you thorpe for the insight and thank you cags for the tip. Quote Link to comment https://forums.phpfreaks.com/topic/178332-solved-foreach-help/#findComment-940420 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.