How do I echo multiple DB rows in one email?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

How do I echo multiple DB rows in one email?

Post by simonmlewis »

I'm sure I am missing something silly here.
This is a basic setup where once a success is made, all the entrants in the database are populated into the $body of the email, row by row.

But I cannot seem to see how to place them in the $body tag. Maybe I need a \n somewhere, but I just cannot suss it out. Help please.

Code: Select all

if ($success == "yes")
  {
  mysql_query("UPDATE register SET paid = 'yes' WHERE sessionid = '$sessionid'");
 
 $to = "email@site.com";
        $subject =  "Booking";
        $headers = "From: info@site.com";
        $body = " .
        
  $result = mysql_query ("SELECT * FROM register WHERE sessionid = '$sessionid' AND paid = 'yes'");
while ($row = mysql_fetch_object($result))
{
if ($row->leaderattending == "yes") { echo "Leader attending: $row->leaderattending";
echo "
$row->title $row->firstname $row->lastname
$row->telephone
$row->mobiletelephone
$row->email
Company (if applicable): $row->company
$row->address1
$row->address2
$row->town
$row->postcode
$row->country
Special Needs (if applicable: $row->specialneeds
Where did they hear about us: $row->wheredidyouhear
$row->datebooked
$row->dateofbooking
Paid: $row->paid
-----------------

";
}
mysql_free_result($result);

. ";
        mail ($to, $subject, $body, $headers);
  }      
      
  echo "<script>
  window.location.replace('/register_c&success=yes')
  </script>";
  unset($_SESSION['sessionid']);
  }
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: How do I echo multiple DB rows in one email?

Post by Christopher »

Shouldn't it be "$body .=" instead of "echo" ?
(#10850)
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do I echo multiple DB rows in one email?

Post by simonmlewis »

Like this?
Or do I somehow append each echo " result in there??

Code: Select all

if ($success == "yes")
  {
  mysql_query("UPDATE register SET paid = 'yes' WHERE sessionid = '$sessionid'");
 
 $to = "email@site.com";
        $subject =  "Booking";
        $headers = "From: info@site.com";
        $body .=
       
  $result = mysql_query ("SELECT * FROM register WHERE sessionid = '$sessionid' AND paid = 'yes'");
while ($row = mysql_fetch_object($result))
{
if ($row->leaderattending == "yes") { echo "Leader attending: $row->leaderattending";
echo "
$row->title $row->firstname $row->lastname
$row->telephone
$row->mobiletelephone
$row->email
Company (if applicable): $row->company
$row->address1
$row->address2
$row->town
$row->postcode
$row->country
Special Needs (if applicable: $row->specialneeds
Where did they hear about us: $row->wheredidyouhear
$row->datebooked
$row->dateofbooking
Paid: $row->paid
-----------------

";
}
mysql_free_result($result);

. ";
        mail ($to, $subject, $body, $headers);
  }      
     
  echo "<script>
  window.location.replace('/register_c&success=yes')
  </script>";
  unset($_SESSION['sessionid']);
  }
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do I echo multiple DB rows in one email?

Post by Celauran »

The line before mail() looks like it needs to be removed. That otherwise should work. Does it?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do I echo multiple DB rows in one email?

Post by simonmlewis »

Sorry, what if I need to add some text in the bottom of the eamil, like "System run by Simon"?
How do I append that into the bottom of the code, below mail()?
. "System designed by Simon"; ???
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do I echo multiple DB rows in one email?

Post by Celauran »

simonmlewis wrote:Sorry, what if I need to add some text in the bottom of the eamil, like "System run by Simon"?
How do I append that into the bottom of the code, below mail()?
. "System designed by Simon"; ???
You don't. If you want it to be included in the mail, it has to come before sending the mail.

I had missed that you're still using 'echo'. Serves me right for replying before having had coffee. You need to get rid of the echo and append to your $body variable as you iterate over the results.

Code: Select all

$body = '';
foreach ($results as $result) {
	$body .= "whatever";
}
$body .= "System by Simon";

mail($to, $subject, $body, $headers);
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do I echo multiple DB rows in one email?

Post by simonmlewis »

Sorry you have lost me now.
What's that about foreach - where is my $row-> code??
I know what you mean about the coffee. As a side, since ur in Canada, what is your preferred coffee? Having been to the States before, I like Folgers and Yuban. Some some empty tins in my kitchen from my adventures there.

Back to the job though, where in that code does my database query go?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do I echo multiple DB rows in one email?

Post by Celauran »

Was just dummy code to avoid having to copy/paste 50 lines of code.

Code: Select all

if ($success == "yes") {

	mysql_query("UPDATE register SET paid = 'yes' WHERE sessionid = '$sessionid'");

	$to = "email@site.com";
	$subject =  "Booking";
	$headers = "From: info@site.com";

	$result = mysql_query ("SELECT * FROM register WHERE sessionid = '$sessionid' AND paid = 'yes'");
	while ($row = mysql_fetch_object($result)) {
		$body = '';
		if ($row->leaderattending == "yes") {
			$body .= "Leader attending: $row->leaderattending";
			$body .= "
				$row->title $row->firstname $row->lastname
				$row->telephone
				$row->mobiletelephone
				$row->email
				Company (if applicable): $row->company
				$row->address1
				$row->address2
				$row->town
				$row->postcode
				$row->country
				Special Needs (if applicable: $row->specialneeds
				Where did they hear about us: $row->wheredidyouhear
				$row->datebooked
				$row->dateofbooking
				Paid: $row->paid
				-----------------

				";
		}
		mysql_free_result($result);
        mail ($to, $subject, $body, $headers);
	}
     
	echo "<script>
	window.location.replace('/register_c&success=yes')
	</script>";
	unset($_SESSION['sessionid']);

}
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do I echo multiple DB rows in one email?

Post by Celauran »

Note that could be slightly off as you had mismatched braces, so check the nesting carefully. It's currently sending one email per result, which is what the code suggested but not the title of the thread.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do I echo multiple DB rows in one email?

Post by simonmlewis »

This is throwing an error on line 44:
[text]Notice: Undefined variable: body in C:\xampp\phpMyAdmin\site\includes\register_complete.inc on line 44[/text]

Line 44 is:

Code: Select all

$body .= "--------------------------------------------
System designed and maintained by me";

Code: Select all

if ($success == "yes")
  {
  mysql_query("UPDATE register SET paid = 'yes' WHERE sessionid = '$sessionid'");
 
 $to = "judith@site.com";
        $subject =  "Booking";
        $headers = "From: info@site.com";
                
  $result = mysql_query ("SELECT * FROM register WHERE sessionid = '$sessionid' AND paid = 'yes'");
while ($row = mysql_fetch_object($result))
{
$body = "";
if ($row->leaderattending == "yes") 
{ 
$body .= "Leader attending: $row->leaderattending";
}

$body .= "
    $row->title $row->firstname $row->lastname
    $row->telephone
    $row->mobiletelephone
    $row->email
    Company (if applicable): $row->company
    $row->address1
    $row->address2
    $row->town
    $row->postcode
    $row->country
    Special Needs (if applicable: $row->specialneeds
    Where did they hear about us: $row->wheredidyouhear
    $row->datebooked
    $row->dateofbooking
    Paid: $row->paid
    -----------------";
}
mysql_free_result($result);
$body .= "--------------------------------------------
System designed and maintained by me";
        mail ($to, $subject, $body, $headers);   
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do I echo multiple DB rows in one email?

Post by Celauran »

$body is being defined inside the while loop. If you have an empty result set, it isn't defined. Move that outside the loop.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do I echo multiple DB rows in one email?

Post by simonmlewis »

Got it. Ta.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do I echo multiple DB rows in one email?

Post by simonmlewis »

At the end of the code, it should be clearing the sessionid, but it's not working.
It runs through the code, unsets it, and then moves on.
But when I test the form again, the same session ID is being used.

Code: Select all

$result = mysql_query ("SELECT * FROM register WHERE sessionid = '$sessionid' AND paid = 'yes'");
while ($row = mysql_fetch_object($result))
{
if ($row->attending == "Yes")
{
$attendingtext = "Information only for person registering person or persons.  Your invoice will be sent via PayPal, along with number of attendees and fees paid.";
}
else
{
$attendingtext = "";
}
$to = "$row->email";
$subject =  "site Booking";
$headers = "From: judith@site.com";
$body = "$attendingtext

Your Booking with site details shown below:

Booking ID: $row->sessionid
$row->title $row->firstname $row->lastname
Special Needs (if applicable): $row->specialneeds
$row->datebooked
$row->dateofbooking

-----------------
    
Kind regards
site.com";
mail ($to, $subject, $body, $headers); 
}
mysql_free_result($result);
        
        
  unset($_SESSION['sessionid']);           
  echo "<script>
  window.location.replace('/register_c&success=yes')
  </script>";
Right at the start of the process, I use this for the Session ID:

Code: Select all

if (isset($_SESSION["sessionid"])) 
{
$sessionid = $_SESSION["sessionid"];
}
else
{
$sessionid = session_id();
$_SESSION['sessionid'] = $sessionid;
}
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do I echo multiple DB rows in one email?

Post by simonmlewis »

I thought you could do it via the *.inc file at the bottom. I know the session() start script has to be in the *.php, but not the clear session. What am I doing wrong??
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply