# Email Loop - One Email Rather Than Many

**URL:** <https://www.phphelp.com/t/email-loop-one-email-rather-than-many/31188>\
**Category:** Beginners - Learning PHP\
**Created:** [March 2, 2020, 12:53pm UTC](https://www.phphelp.com/t/email-loop-one-email-rather-than-many/31188 "2020-03-02T12:53:52Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Leemo](https://www.phphelp.com/letter_avatar_proxy/v4/letter/l/3d9bf3/32.png) [@Leemo](https://www.phphelp.com/u/Leemo)\
**Post date:** [March 2, 2020, 12:53pm UTC](https://www.phphelp.com/t/email-loop-one-email-rather-than-many/31188/1 "2020-03-02T12:53:53Z")

</div>

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="me@email.com";
$from="from@email.com";
$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));
}
```

---

<div class="post-metadata">

**Author:** ![astonecipher](https://www.phphelp.com/user_avatar/www.phphelp.com/astonecipher/32/2797_2.png) [@astonecipher](https://www.phphelp.com/u/astonecipher)\
**Post date:** [March 2, 2020, 2:24pm UTC](https://www.phphelp.com/t/email-loop-one-email-rather-than-many/31188/2 "2020-03-02T14:24:48Z")

</div>

What are the brackets for?

---

<div class="post-metadata">

**Author:** ![Leemo](https://www.phphelp.com/letter_avatar_proxy/v4/letter/l/3d9bf3/32.png) [@Leemo](https://www.phphelp.com/u/Leemo)\
**Post date:** [March 2, 2020, 3:16pm UTC](https://www.phphelp.com/t/email-loop-one-email-rather-than-many/31188/3 "2020-03-02T15:16:52Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![astonecipher](https://www.phphelp.com/user_avatar/www.phphelp.com/astonecipher/32/2797_2.png) [@astonecipher](https://www.phphelp.com/u/astonecipher)\
**Post date:** [March 2, 2020, 3:33pm UTC](https://www.phphelp.com/t/email-loop-one-email-rather-than-many/31188/4 "2020-03-02T15:33:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Leemo](https://www.phphelp.com/letter_avatar_proxy/v4/letter/l/3d9bf3/32.png) [@Leemo](https://www.phphelp.com/u/Leemo)\
**Post date:** [March 2, 2020, 4:51pm UTC](https://www.phphelp.com/t/email-loop-one-email-rather-than-many/31188/5 "2020-03-02T16:51:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![phdr](https://www.phphelp.com/letter_avatar_proxy/v4/letter/p/91b2a8/32.png) [@phdr](https://www.phphelp.com/u/phdr)\
**Post date:** [March 2, 2020, 5:26pm UTC](https://www.phphelp.com/t/email-loop-one-email-rather-than-many/31188/6 "2020-03-02T17:26:21Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![astonecipher](https://www.phphelp.com/user_avatar/www.phphelp.com/astonecipher/32/2797_2.png) [@astonecipher](https://www.phphelp.com/u/astonecipher)\
**Post date:** [March 2, 2020, 5:26pm UTC](https://www.phphelp.com/t/email-loop-one-email-rather-than-many/31188/7 "2020-03-02T17:26:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![astonecipher](https://www.phphelp.com/user_avatar/www.phphelp.com/astonecipher/32/2797_2.png) [@astonecipher](https://www.phphelp.com/u/astonecipher)\
**Post date:** [March 2, 2020, 5:27pm UTC](https://www.phphelp.com/t/email-loop-one-email-rather-than-many/31188/8 "2020-03-02T17:27:55Z")

</div>

> [@phdr](#):
>
> Actually, you can have a pair of { }, not part of anything, and php doesn’t complain.

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

---

<div class="post-metadata">

**Author:** ![phdr](https://www.phphelp.com/letter_avatar_proxy/v4/letter/p/91b2a8/32.png) [@phdr](https://www.phphelp.com/u/phdr)\
**Post date:** [March 2, 2020, 5:34pm UTC](https://www.phphelp.com/t/email-loop-one-email-rather-than-many/31188/9 "2020-03-02T17:34:37Z")

</div>

> [@Leemo](#):
>
> when a save all button is pressed…

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.
