Including while statements in mail()

  • Thread starter Thread starter Anonymous
  • Start date Start date
A

Anonymous

Guest
how do send the output of this code to an address? I can put it on the screen, but I dont know how to include it in the mail() function

Code:
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
        printf ("User: %s  Product: %s", $row[1], $row[2]);  
		print "<br>";
    }

TIA
 
$mailBody = sprintf("User: %s Product: %s", $row[1], $row[2]);

:P
 
Thanks for fast reply, but this wont and doesnt work.

The while statement is so that I can print more than one thing. eg.

User: frogrocker Product: 1
User: frogrocker Product: 2

However, with your code, there's only one that will show up.

Any ideas?
 
In your while loop, append the contents of each row to a string. Then, after the loop, feed the string to the mail() function. Like so:

Code:
<?
$mailcontents = '';

while ($row = mysql_fetch_array($result, MYSQL_NUM)) { 
   // adds the contents of each row to the string $mailcontents
   $mailcontents .= 'User: ' . $row[1] . ' Product: ' . $row[2] . "\n";  
}

mail('recipient@mail.com', 'You\'ve Got Mail', $mailcontents);
?>

Also, you're making one of the cardinal mistakes of PHP with your usage of printf(). Instead of using printf(), you should just be using string parsing or concatenation. For reference, check out the Top 21 PHP Programming Mistakes - Part I.
 
$mailBody .= sprintf("User: %s Product: %s", $row[1], $row[2]);

missed the dot
 
swirlee said:
Code:
<?
$mailcontents = '';

while ($row = mysql_fetch_array($result, MYSQL_NUM)) { 
   // adds the contents of each row to the string $mailcontents
   $mailcontents .= 'User: ' . $row[1] . ' Product: ' . $row[2] . "\n";  
}

mail('recipient@mail.com', 'You\'ve Got Mail', $mailcontents);
?>

Nope, this doesnt work... it says
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/neo/public_html/studio/new/basket/check.php on line 25

Also,

Also, you're making one of the cardinal mistakes of PHP with your usage of printf(). Instead of using printf(), you should just be using string parsing or concatenation. For reference, check out the Top 21 PHP Programming Mistakes - Part I.

Please explain...

I need to learn!
 
frogrocker said:
Nope, this doesnt work... it says
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/neo/public_html/studio/new/basket/check.php on line 25


My code assumes that you've already connected to the database and performed a query using mysql_query() and have passed the results to $results before doing mysql_fetch_array(). If you have not done this, then $results isn't defined, so you get the invalid resource error. The first part of Example 2 in the documentation will help you with this, but I recommend finding and reading good PHP+MySQL tutorial online before tackling databases.


frogrocker said:
Also, you're making one of the cardinal mistakes of PHP with your usage of printf(). Instead of using printf(), you should just be using string parsing or concatenation. For reference, check out the Top 21 PHP Programming Mistakes - Part I.

Please explain...

Did you read the article that I linked to? That gives a better explanation than I could come up with. Please read it thoroughly and if you still don't understand, come back and let me know.
 
thank you very much- i now have a verfy nice code....
i need to find out how it works!

can u tell me please?

Thanks anyway.

$mailcontents = '';

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
// adds the contents of each row to the string $mailcontents
$mailcontents .= 'User: ' . $row[1] . ' Product: ' . $row[2] . "\n";
}


1. What's with all the dots?
2. How does $mailcontents seem to have more than one value..
 
frogrocker said:
1. What's with all the dots?
2. How does $mailcontents seem to have more than one value..

1. The documentation is always useful for this sort of question. Please use it to answer your questions as frequently as possible. But I'll indulge you anyhow. In php, . is the concatenation operator, and .= is the concatenating assignment (append) operator. They are the operators for joining two or more strings. To demonstrate:

Code:
<?
   $a = 'united';
   $b = 'states';
   echo $a . $b; // This will print "unitedstates"

   $c = $a . $b;
   echo $c; // This will also print "unitedstates"

   $a .= $b // This appends the contents of $b to the end of $a
   // The above is essentially a shortcut for $a = $a . $b;
   echo $a; // This will also print "unitedstates"
?>

This is very basic PHP and you will need to know it to create all but the simplest scripts in PHP. I recommend that you read a good basic PHP tutorial. Webmonkey has a tutorial that looks reasonably good. Once you've gone through all of these exercises, you may want to read the documentation for the String type. This will answer your second question, as well, but I'll take a stab at it anyhow.

2. $mailcontents does not contain more than one value. It contains a single string. When denoting a string using double-quotes (e.g. $mystring = "something";), you can also include special characters. The characters "\n" represent the "newline" character, which is what split the $mailcontents into several lines when it sent the e-mail. To demonstrate:

Code:
<?
   $a = "this is\nthree\nseparate lines";
   echo $a;
   /* The output will look like this:
      ----------
      this is
      three
      separate lines
      ----------
      Using the concatenation operator as I demonstrated
      above, you can also rewrite the above in a slightly more
      readable manner, and it is a good idea to do so:
   */

   $b =  "this is\n" .
         "three\n" .
         "separate lines";
   /* Make note of the dots at the end of each line (except the
      last). The variables $a and $b now have identical contents
      (you can test this using "if($a === $b) ...").
   */
?>

So you see, it has converted each instance of "\n" into a line break. It's important to note, however, that when you output to an HTML file and view that file in your browser, newlines will be ignored and the above would look like "this is three separate lines". To produce a line break in HTML, you must use the HTML tag "<br />" (the trailing slash is for XHTML compliance). So to make the above appear as three lines in your browser, you should instead make your code like the following:

Code:
<?
   $a =  "this is<br />\n" .
         "three<br />\n" .
         "separate lines";
?>

For reference, another way to insert an HTML line break before every newline is using the PHP function nl2br().

I hope that was helpful, and I hope you take the time to read all of the links I have posted.
 
AHA!!

Thanks alot!
I understand now... and i will try to read those links, but I'm currently working through SAMS PHP book! - it gets a bit heavy going...
 
Ah, I'm glad you've got a book and you aren't just going at it blind. I've never read any of the SAMS books, but I've always found it easier to learn something when I have a real book in my hands that I can flip through. Good luck.
 
Back
Top