Email Loop - One Email Rather Than Many

I have rows of data that when a save all button is pressed the contents of the changes are sent in an Email to myself.
The code below works OK, but sends each record as a separate Email. I would prefer all the data to be sent in one email.
I am guessing a loop is needed somewhere using curly brackets and perhaps a line of code like this:
while ($msg = mysqli_fetch_array($result))

Any help would be appreciated. Many thanks.

{
$email="[email protected]";
$from="[email protected]";
$msg="";
$subject="Registers Info for: ".$values["classname"]." ".$values["attendance_date"]."";

$msg.= "Student: ".$values["first_name"]." ".$values["last_name"]."\r\n";
$msg.= "Absent or Present: ".$values["attendance_status"]."\r\n";
$msg.= "Class: ".$values["classname"]."\r\n";
$msg.= "Class Date: ".$values["attendance_date"]."\r\n";
$msg.= "Amount Received: ".$values["cash_received"]."\r\n";
$msg.= "Paid For: ".$values["cash_whatfor"]."\r\n";

$ret=runner_mail(array('to' => $email, 'subject' => $subject, 'body' => $msg, 'from'=>$from));
}

What are the brackets for?

I thought that helps split sections, helping create a loop? I’m guessing those are not necessary. But, I’m sure I need some more within the above code.

brackets create blocks, by themselves they are syntax errors. What is all of that code?

The code is an After Record Updated event with PHP Runner software

Actually, you can have a pair of { }, not part of anything, and php doesn’t complain.

In your first, partial, posted code, that is producing a separate email for each record, you must have some kind of loop that is retrieving each row of data from an sql query and assigning each row to $values.

You would put the common/initialization logic before that start of that existing loop, just add to the message body inside that loop, then send the email after the end of that loop.

Then you are stuck on the solution, since the event is triggered on update. If it weren’t for that you could get a collection, but as it is, the event is triggered on each, not collectively.

Well crap. Guess I am just use to languages that they mean something important and not as ignored characters

You are going to need to post ALL the relevant form processing code so that we are not guessing as to what the context actually is.

As stated in the first post I am receiving all the information correctly but in separate Emails. My results are between 1-20 records, which of course is 1-20 Emails.

Requinix I have changed the subject variable to make more sense if receving one Email.

I need to loop the information in the $msg.= sections into one email. I have added:

while ($values = mysqli_fetch_array($result))

and put the area I need looped in curly brackets, not sure if that is the correct way to do it. But, as the code stands below I am not receiving any Emails at all.

$email="[email protected]";
$from="[email protected]";
$msg="";
$subject="Registers Info for: ".$values["classname"]." ".$values["attendance_date"]."";
while ($values = mysqli_fetch_array($result))
{
$msg.= "Student: ".$values["first_name"]." ".$values["last_name"]."\r\n";
$msg.= "Absent or Present: ".$values["attendance_status"]."\r\n";
$msg.= "Class: ".$values["classname"]."\r\n";
$msg.= "Class Date: ".$values["attendance_date"]."\r\n";
$msg.= "Amount Received: ".$values["cash_received"]."\r\n";
$msg.= "Paid For: ".$values["cash_whatfor"]."\r\n";
}
$ret=runner_mail(array('to' => $email, 'subject' => $subject, 'body' => $msg, 'from'=>$from));

SQL Query

SELECT
attendance.attendance_id,
attendance.student_id,
attendance.class_id,
attendance.attendance_status,
attendance.attendance_date,
attendance.cash_received,
attendance.cash_whatfor,
students.last_name,
students.first_name,
classes.classname
FROM attendance
INNER JOIN classes ON attendance.class_id = classes.class_id
INNER JOIN students ON attendance.student_id = students.student_id
ORDER BY students.last_name
Sponsor our Newsletter | Privacy Policy | Terms of Service