Jump to content

[SOLVED] foreach help.


New Coder

Recommended Posts

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: [email protected] \r\n";
				}

 

Hard to test as I dont want to start sending out rouge emails

 

Many Thanks

 

 

Link to comment
https://forums.phpfreaks.com/topic/178332-solved-foreach-help/
Share on other sites

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: [email protected] \r\n";
    while ($row = mssql_fetch_array($rs)) {
      $to = $row['$email'];
      // call mail()
    }
  }
}

Link to comment
https://forums.phpfreaks.com/topic/178332-solved-foreach-help/#findComment-940334
Share on other sites

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: [email protected] \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 = "[email protected]";
		$re = "Newsletter Email Sent";
		$msg = "Distribution confirmed \n\nThank You";
		$headers = "From: [email protected] \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: [email protected] \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 = "[email protected]";
		$re = "Newsletter Email Sent";
		$msg = "Distribution confirmed \n\nThank You";
		$headers = "From: [email protected] \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

 

Link to comment
https://forums.phpfreaks.com/topic/178332-solved-foreach-help/#findComment-940388
Share on other sites

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.