Jump to content

Putting while loop results into a variable


elmas156

Recommended Posts

I'm trying to take a number of items from a database using a while loop and put the results into a variable so that I can send one email with all the results.  I'm having trouble figuring this out though.  Can anyone please give me any ideas as to how to make this work?  Here's what I've done so far... all it does is result in an email with the first set of results in the while loop, not all of them.

 

<?php
include("conf.inc.php");


$result = mysql_query("SELECT `to`,`from`,`subject`,`message`,`date` FROM allmsgs WHERE reported = 'n' ORDER BY `messid` ASC");
$row = mysql_fetch_row($result);

$cdate = date('m-d-Y');

			$sendto = "List@emails.here";
			$emailsubject = "Webstats Report For $cdate.";				

			while ($row = mysql_fetch_row($result)) {

					$to = $row[0];
					$from = $row[1];
					$subject = $row[2];
					$message = $row[3];
					$datetime = $row[4];

					$eachmessage = "<p>
					<table width=\"400\">
					<tr>
					<td>
					<hr width=\"400\">
					To: $to<br>
					From: $from<br>
					On $datetime<br> <br>
					$subject<br> <br>
					$message
					</td>
					</tr>
					</table>
					</p>";

			}

			$emailmessage = "<html>
			<body>
			<p>Here is a list of the messages that have been exchanged in the last 24 hours using the webstat system system.</p>
			$eachmessage
			</body>
			</html>";

			// To send HTML mail, the Content-type header must be set
			$headers  = 'MIME-Version: 1.0' . "\r\n";
			$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

			// Additional headers

			$headers .= 'From: Webstats <reports@webstats.com>' . "\r\n";

			// Mail it
			mail($sendto, $emailsubject, $emailmessage, $headers);


?>

Link to comment
Share on other sites

<?php
include("conf.inc.php");


$result = mysql_query("SELECT `to`,`from`,`subject`,`message`,`date` FROM allmsgs WHERE reported = 'n' ORDER BY `messid` ASC");

$cdate = date('m-d-Y');
         
            $sendto = "List@emails.here";
            $emailsubject = "Webstats Report For $cdate.";            
            $eachmessage = '';
            while ($row = mysql_fetch_row($result)) {
                  
                  $to = $row[0];
                  $from = $row[1];
                  $subject = $row[2];
                  $message = $row[3];
                  $datetime = $row[4];
                  
                  $eachmessage .= "<p>
                  <table width=\"400\">
                  <tr>
                  <td>
                  <hr width=\"400\">
                  To: $to<br>
                  From: $from<br>
                  On $datetime<br> <br>
                  $subject<br> <br>
                  $message
                  </td>
                  </tr>
                  </table>
                  </p>";
                  
            }
            
            $emailmessage = "<html>
            <body>
            <p>Here is a list of the messages that have been exchanged in the last 24 hours using the webstat system system.</p>
            $eachmessage
            </body>
            </html>";
      
            // To send HTML mail, the Content-type header must be set
            $headers  = 'MIME-Version: 1.0' . "\r\n";
            $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
            
            // Additional headers
            
            $headers .= 'From: Webstats <reports@webstats.com>' . "\r\n";
            
            // Mail it
            mail($sendto, $emailsubject, $emailmessage, $headers);
                  

?>

 

You need to concatenate each message to the end of the string using the .= operator like above. Also the variable needs to be pre-defined outside (before) the loop in order to avoid a warning error.

Link to comment
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.