Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I need a simple example of how to send a table of info .

Status
Not open for further replies.

EdLentz

Technical User
Mar 20, 2002
85
US
I am trying to put together an email script that includes a html table. Can someone point me to an example of how to do this? I just have a small table with 5 items in a mysql DB.

Thanks
 
Thanks for the reply.

No user input, the info is already in a table. I just want to put it in a table format ( 5 columns) and email it. I have a php script that writes it to a browser. I want to change that to email it.

Code:
<html>
<body>
<?php 
$username = ""; 
$password = ""; 
$database = "asterisk"; 
$mysqli = new mysqli("localhost", $username, $password, $database); 
$query = "SELECT * FROM device_stats";


echo '<table border="0" cellspacing="2" cellpadding="2"> 
      <tr> 
          <td> <font face="Arial">Type</font> </td> 
          <td> <font face="Arial">Ext #</font> </td> 
          <td> <font face="Arial">Description</font> </td> 
          <td> <font face="Arial">Date Entered</font> </td> 
     </tr>';

if ($result = $mysqli->query($query)) {
    while ($row = $result->fetch_assoc()) {
        $field1name = $row["tech"];
        $field2name = $row["user"];
        $field3name = $row["description"];
        $field4name = $row["CURDATE"];
        
        echo '<tr> 
                  <td>'.$field1name.'</td> 
                  <td>'.$field2name.'</td> 
                  <td>'.$field3name.'</td> 
                  <td>'.$field4name.'</td> 
                   
              </tr>';
    }
    $result->free();
} 
?>
</body>
</html>

Thanks
 
A general way to get the output of any PHP script into a variable to use for something like the HTML body of a mail is using output buffering. See
The only thing that's unbuffered is header outputs, but that's not used by the script you have.

So what you can do is
Code:
$output_buffer = ob_start(); // start output buffering.
require ourphpscript.php
$htmlbody = ob_get_clean(); // end output buffering and get the output.

It would be cleaner to make a function of your script to generate the HTML and return it, that can be used both for output of the HTML page to a browser and as mail body without needing tricks like output buffering. The problematic part of including/requiring PHP scripts is the actually executed script can get messy, have double definitions, reuse of variable names and cause side effects. Using require_once instead would not help in this case, as your mail script will not include or require the table output script multiple times.

It's not a good idea to misuse a buffer as a general variable, create clear procedural style or OOP style program structures. When you need something for both mail and browser that's already showing you it was a bad idea to only do plain scripting and not plan ahead by defining functions or even classes that can be called and used.

Chriss
 
That sounds like a good idea. I'll give that a try

Thanks
 
I spoke to soon and maybe it isn't this php file. I added the require and arranged the $htmlbody and ran the php script from the browser. I got the email with the info in a table just like I wanted it. I changed the info and tried again and I get nothing. I am also having issues with my browser(s) Everytime I try to re access the page, the browser goes into search mode and I get nowhere. Any ideas on what could be going on?

Thanks
 
I can't help you with that, just look out for any dependencies. That script creating the table HTML depends on a connection already been made. If you only go through the initialization of $mysqli elsewhere in your scripts, you also require that before requiring your table HTML script. That's also what boils down to a better solution being a procedural or OOP concept that you can use for both generating output towards browser or towards other PHP code needing to use the same HTML for composing mails.



Chriss
 
I appreciate the help. My last issue was inadvertantly self inflicted. I have it sorted out now.

Thanks again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top