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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.