Reputation: 11
I am working on this web application that needs to send a certain report. The report is dynamic however and it is in a table format. The thing i hope to do now is to email the whole table via email to someone. Is there any way I can do so?
php script
<?php
$con = mysql_connect("localhost", "root", "");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("mydb", $con);
$input = $_POST['id'];
$query = mysql_query("SELECT * FROM table WHERE ID = '$input'");
echo "<table border='5' align=center>
<tr>
<th>ID</th>
<th>Type</th>
<th>Setting</th>
<th>Value</th>
<th>Actual</th>
</tr>";
while($row = mysql_fetch_array($query))
{
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['Type'] . "</td>";
echo "<td>" . $row['Setting'] . "</td>";
echo "<td>" . $row['Value'] . "</td>";
echo "<td>" . $row['Actual'] . "</td>";
echo "</tr>";
if($row['Value'] != $row['Actual'])
{
echo "<td bgcolor='#FD2911'>" . "X" . "</td>";
}
else
{
echo "<td bgcolor='#1CDC15'>" . "O" . "</td>";
}
}
echo "<br>";
mysql_close($con);
?>
html code
<form action="query1.php" method="POST">
Enter the choice: <input type="varchar" name="id" /><br />
<input type="submit" value="Audit" />
</form>
Upvotes: 0
Views: 1755
Reputation: 39704
You concatenate the whole table:
$table = '';
$table .= "<table border='5' align=center>
<tr>
<th>ID</th>
<th>Type</th>
<th>Setting</th>
<th>Value</th>
<th>Actual</th>
</tr>";
while($row = mysql_fetch_array($query))
{
$table .= "<tr>";
$table .= "<td>" . $row['ID'] . "</td>";
$table .= "<td>" . $row['Type'] . "</td>";
$table .= "<td>" . $row['Setting'] . "</td>";
$table .= "<td>" . $row['Value'] . "</td>";
$table .= "<td>" . $row['Actual'] . "</td>";
$table .= "</tr>";
if($row['Value'] != $row['Actual'])
{
$table .= "<td bgcolor='#FD2911'>" . "X" . "</td>";
}
else
{
$table .= "<td bgcolor='#1CDC15'>" . "O" . "</td>";
}
}
$table .= "</table><br>";
mail('[email protected]', 'My Subject', $table);
Upvotes: 1
Reputation: 1001
if you are wanting to do more than the table, you could use output buffering;
<?php
ob_start();
# all your code/page/table(s) with echos and all
$toBeSent = ob_get_contents();
ob_end_clean();
?>
then send the entire contents between ob_start and ob_end_clean via email by replacing $table in the mail() line of Mihai's post with $toBeSent.
Upvotes: 0