send dynamic table via email - php

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>

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('to#example.com', 'My Subject', $table);

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.

Related

Email each line of sql as email

This script looks fine but when i run it, the entire db is just echo'd all over the page. I didn't get any errors so not sure yet but i uploaded a spreadsheet to sql full of tasks and then each line i want to email to myself and if possible delete it after but i can manually do that anyway. The goal is just to array the email across each row.
<?php
$con = mysql_connect("localhost","root","toor");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("db", $con);
$result = mysql_query("SELECT * FROM tasks");
$to = "someone#somewhere.com";
$subject = "Results from query";
$body =
"<table border='1'>
<tr>
<th>CustomerID:</th>
<th>Name:</th>
<th>CompanyCode:</th>
<th>Country:</th>
<th>SalesRep:</th>
<th>City:</th>
<th>PostalCode:</th>
</tr>";
while($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['CustomerID'] . "</td>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['CompanyCode'] . "</td>";
echo "<td>" . $row['Country'] . "</td>";
echo "<td>" . $row['SalesRep'] . "</td>";
echo "<td>" . $row['City'] . "</td>";
echo "<td>" . $row['PostalCode'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
$headers = 'From: someone#example.com';
mail($to,$subject,$body,$headers);
echo 'Mail sent to $to';
?>

HTML table only echo one row instead of all in database into table

I seem to be having an issue with my HTML formatting. I am calling the database to display all information within the table on a webpage. I have got the information coming down but I cannot seem to get all of the information to go into the table.
The formatting will only allow one row to be entered into the table and then the rest of the row go below in normal text format.
How would I go about getting all the information to sit in the table and automatically update when more information is added?
<?php
//connect to the server
$link = mysql_connect('*****', '*****', '****');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('******');
$query = mysql_query("SELECT * FROM tablename");
echo "<table border='1'>
<tr>
<td>Date</td>
<td>Region</td>
<td>Cameraman</td>
<td>Livestream?</td>
<td>Event Title</td>
<td>Lecturer</td>
<td>Time</td>
<td>Speaker</td>
<td>ICE Contact</td>
<td>Venue Address</td>
<td>Venue Contact</td>
<td>Additional Comments</td>
<td>On App?</td>
</tr>";
WHILE($rows = mysql_fetch_array($query)):
$date = $rows['date'];
$region = $rows['region'];
$cameraman = $rows['cameraman'];
$livestream = $rows['livestream'];
$eventtitle = $rows['eventitle'];
$lecturer = $rows['lecturer'];
$time = $rows['time'];
$speaker = $rows['speaker'];
$icecontact = $rows['icecontact'];
$venuecontact = $rows['venueaddress'];
$venuecontact = $rows['venuecontact'];
$additionalcomments = $rows['additionalcomments'];
$onapp = $rows['onapp'];
echo "<tr>";
echo "<td>" . $rows['date'] . "</td>";
echo "<td>" . $rows['region'] . "</td>";
echo "<td>" . $rows['cameraman'] . "</td>";
echo "<td>" . $rows['livestream'] . "</td>";
echo "<td>" . $rows['eventitle'] . "</td>";
echo "<td>" . $rows['lecturer'] . "</td>";
echo "<td>" . $rows['time'] . "</td>";
echo "<td>" . $rows['speaker'] . "</td>";
echo "<td>" . $rows['icecontact'] . "</td>";
echo "<td>" . $rows['venueaddress'] . "</td>";
echo "<td>" . $rows['venuecontact'] . "</td>";
echo "<td>" . $rows['additioncomments'] . "</td>";
echo "<td>" . $rows['onapp'] . "</td>";
echo "</tr>";
echo "</table>";
endwhile;
?>
http://cpdonline.tv/spreadsheet/spreadsheet.php
You made two boo boo's in your code:
1: You are not using these variables, delete them
$date = $rows['date'];
$region = $rows['region'];
$cameraman = $rows['cameraman'];
$livestream = $rows['livestream'];
$eventtitle = $rows['eventitle'];
$lecturer = $rows['lecturer'];
$time = $rows['time'];
$speaker = $rows['speaker'];
$icecontact = $rows['icecontact'];
$venuecontact = $rows['venueaddress'];
$venuecontact = $rows['venuecontact'];
$additionalcomments = $rows['additionalcomments'];
$onapp = $rows['onapp'];
2: You are closing the table inside of the while loop
echo "</table>";
endwhile;
Should be:
endwhile;
echo "</table>";

Change properties of table when I send information from MySQL to webpage

I display the information of MySQL table in web, it's easy but I want to change the background color of a cell when it has certain value. In the example, my table has different fields: id, nombre, apellido1, apellido2, curso, e-mail, and direccion. I want to get that when the field curso = primero, the color of that cell was red.
<?php
$connect = mysql_connect("localhost", "root", "") ;
if (!$connect) {
die ("Can not connect: " . mysql_error () ) ;
}
mysql_select_db("modelobdclase", $connect) ;
$sql = "SELECT * FROM datosalumnado";
$myData = mysql_query($sql, $connect) ;
echo "<table border=1>
<tr>
<th> id_alumnado </th>
<th> nombre </th>
<th> apellido1 </th>
<th> apellido2 </th>
<th> curso </th>
<th> fechadenacimiento </th>
<th> e-mail </th>
<th> direccion </th>
</tr>";
[B]
$valor= "primero";
function dame_color($valor) {
if ($valor == 'primero') return 'red';
else ' ';
}
while ($record = mysql_fetch_array ($myData)) {
$color = dame_color($row->[B] 'curso' );
[B]echo "<td bgcolor=$color>";
echo "<tr>";
echo "<td>" . $record ['id_alumnado'] . "</td>";
echo "<td>" . $record ['nombre'] . "</td>";
echo "<td>" . $record ['apellido1'] . "</td>";
echo "<td>" . $record ['apellido2'] . "</td>";
echo "<td>" . $record ['curso'] . "</td>";
echo "<td>" . $record ['fechanacimiento'] . "</td>";
echo "<td>" . $record ['e-mail'] . "</td>";
echo "<td>" . $record ['direccion'] . "</td>";
}
echo "</table>" ;
mysql_close($connect) ;
?>
Try this
$color = ($record ['curso'] == 'primero') ? "style='background-color:#f00;'" : '';
echo "<td $color>" . $record ['curso'] . "</td>";
Thanks user 36.... it was a fantastic answer. It works!!
The complete code would be:
function dame_color($valor) {
if ($valor == 'primero') return 'red';
else return 'white';
}
while ($record = mysql_fetch_array ($myData)) {
$color = ($record ['curso'] == 'primero') ? "style='background-color:#f00;'" : '';
echo "<tr >";
echo "<td>" . $record ['id_alumnado'] . "</td>";
echo "<td>" . $record ['nombre'] . "</td>";
echo "<td>" . $record ['apellido1'] . "</td>";
echo "<td>" . $record ['apellido2'] . "</td>";
echo "<td $color>" . $record ['curso'] . "</td>";
echo "<td>" . $record ['fechanacimiento'] . "</td>";
echo "<td>" . $record ['e-mail'] . "</td>";
echo "<td>" . $record ['direccion'] . "</td></tr>";
}
echo "</table>" ;

PHP get sum of SQL table

I have MAMP (local hosted SQL,WEB etc server) the database name is :NKTDEBITS the table name is :Insurance and the column on the table is STATECOV. I know I'm close with this but still get a black in the field that should generate the total, anyone got a idea?
<?php
$con=mysqli_connect("localhost","root","root","KNTDebits");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Insurance");
$result2 = mysqli_query($con,"SELECT * FROM Office");
$result3 = mysqli_query($con,"SELECT * FROM RichmondLocation");
$result4 = mysqli_query($con,"SELECT * FROM DanvilleLocation");
$result5 = mysql_query('SELECT SUM(STATECOV) AS STATECOV_sum FROM Insurance');
echo "<table border='1'>
<tr>
<th>Truck Number</th>
<th>VIN</th>
<th>Make</th>
<th>Model</th>
<th>State Coverage</th>
<th>Comprehinsive Coverage</th>
<th>Property Damage/th>
<th>Personal Injury</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['TNUM'] . "</td>";
echo "<td>" . $row['VIN'] . "</td>";
echo "<td>" . $row['MAKE'] . "</td>";
echo "<td>" . $row['MODEL'] . "</td>";
echo "<td>" . $row['STATECOV'] . "</td>";
echo "<td>" . $row['COMPRE'] . "</td>";
echo "<td>" . $row['PROPDMG'] . "</td>";
echo "<td>" . $row['PRSINJ'] . "</td>";
echo "</tr>";
}
echo "</table>";
//Table 2 Start
str_repeat(' ', 5); // adds 5 spaces
echo "<table border='5'>
<tr>
<th>Richmond</th>
<th>Date</th>
<th>Payment</th>
<th>Payer</th>
</tr>";
while($row3 = mysqli_fetch_array($result3))
{
echo "<tr>";
echo "<td>" . $row3[''] . "</td>";
echo "<td>" . $row3['DATE'] . "</td>";
echo "<td>" . $row3['PAYMENT'] . "</td>";
echo "<td>" . $row3['PAYER'] . "</td>";
echo "</tr>";
}
echo "</table>";
//Table 4 Start
str_repeat(' ', 5); // adds 5 spaces
echo "<table border='5'>
<tr>
<th>Danville</th>
<th>Date</th>
<th>Payment</th>
<th>Payer</th>
</tr>";
while($row4 = mysqli_fetch_array($result4))
{
echo "<tr>";
echo "<td>" . $row4[''] . "</td>";
echo "<td>" . $row4['DATE'] . "</td>";
echo "<td>" . $row4['PAYMENT'] . "</td>";
echo "<td>" . $sum . "</td>";
echo "</tr>";
}
echo "</table>";
//Table 5 Start
echo "<table border='5'>
<tr>
<th>Total</th>
</tr>";
$result = mysql_query('SELECT SUM(STATECOV) AS value_sum FROM Insurance');
$row = mysql_fetch_assoc($result);
$sum = $row['value_sum'];
while($row = mysql_fetch_assoc($result));
{
echo "<tr>";
echo "<td>" . $sum . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
You need a GROUP BY to go along with your SUM. I don;t know enough about you table to let you know what column you should use for this.
You should be handling potential error cases whenn you query the database, as you will quickly see when you are getting database/query errors.
You should also look to use mysqli or PDO instead of the deprecated mysql_* functions.

Approve Form submit

I have trouble with creating an approval form as am still php beginner,
the idea is
user submit a form am setting a default value"0" in the approved row at the table..
so behind the scenes the admin shows all members from this table where approved="0"
and this is the code
<code>
<?php
$con = mysql_connect("localhost","ebarea_epic","...");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("ebarea_epic", $con);
$query = "select * from medicalrep where approved='0'";
$result=mysql_query($query);
echo "<table border='1'>
<tr>
<th>User Name</th>
<th>Password</th>
<th>Mobile </th>
<th>Address</th>
<th>Faculty</th>
<th>Graduation Year</th>
<th>Region</th>
<th>Area</th>
<th>Line</th>
<th>Appointment Date</th>
<th>Resign Data</th>
<th>Job Title</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['password'] . "</td>";
echo "<td>" . $row['Mobile'] . "</td>";
echo "<td>" . $row['Address'] . "</td>";
echo "<td>" . $row['Faculty'] . "</td>";
echo "<td>" . $row['Graduation Year'] . "</td>";
echo "<td>" . $row['Region'] . "</td>";
echo "<td>" . $row['Line'] . "</td>";
echo "<td>" . $row['Area'] . "</td>";
echo "<td>" . $row['Appointment'] . "</td>";
echo "<td>" . $row['Resign'] . "</td>";
echo "<td>" . $row['job_title'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
</code>
I just want to add checkbox for every table user and when checked thier status changed to 1 in approved column
thanks all
$con = mysql_connect("localhost","ebarea_epic","...");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("ebarea_epic", $con);
$query = "select * from medicalrep where approved='0'";
$result=mysql_query($query);
$i = 1; //counter for the checkboxes so that each has a unique name
echo "<form action='process.php' method='post'>"; //form started here
echo "<table border='1'>
<tr>
<th>User Name</th>
<th>Password</th>
<th>Mobile </th>
<th>Address</th>
<th>Faculty</th>
<th>Graduation Year</th>
<th>Region</th>
<th>Area</th>
<th>Line</th>
<th>Appointment Date</th>
<th>Resign Data</th>
<th>Job Title</th>
<th>Update</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['password'] . "</td>";
echo "<td>" . $row['Mobile'] . "</td>";
echo "<td>" . $row['Address'] . "</td>";
echo "<td>" . $row['Faculty'] . "</td>";
echo "<td>" . $row['Graduation Year'] . "</td>";
echo "<td>" . $row['Region'] . "</td>";
echo "<td>" . $row['Line'] . "</td>";
echo "<td>" . $row['Area'] . "</td>";
echo "<td>" . $row['Appointment'] . "</td>";
echo "<td>" . $row['Resign'] . "</td>";
echo "<td>" . $row['job_title'] . "</td>";
echo "<td><input type='checkbox' name='check[$i]' value='".$row['ID']."'/>";
echo "</tr>";
$i++;
}
echo "</table>";
echo "<input type='submit' name='approve' value='approve'/>";
echo "</form>";
mysql_close($con);
Now comes process.php
if(isset($_POST['approve'])){
if(isset($_POST['check'])){
foreach ($_POST['check'] as $value){
$sql = "UPDATE post SET post_approved = 1 WHERE ID = $value"; //write this query according to your table schema
mysql_query($sql) or die (mysql_error());
}
}
}
though you are using mysql_* functions here, i recommend you to use PDO
EDIT:
As per your request, this is the update.
Change this code in your admin panel script:
echo "<input type='submit' name='approve' value='approve'/>";
Delete the above line and add this instead:
echo "<input class='action' type='button' name='approve' value='approve' />";
echo "<input class='action' type='button' name='edit' value='edit' />";
echo "<input class='action' type='button' name='delete' value='delete' />";
echo "<input type='hidden' name='action' value='' id='action' />"; //Action (edit, approve or delete) will be set here which will be passed as POST variable on form submission
Now you will need some javascript to do some tricks.
Add the following code preferably head section in your admin panel script
<script type="text/javascript" src="jquery-1.7.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('.action').click(function(){
var action = $(this).attr('name');
$('#action').val(action);
$(this).closest('form').submit();
})
})
</script>
Now comes the modification in process.php file
if (isset($_POST['approve'])) {
if (isset($_POST['check'])) {
foreach ($_POST['check'] as $value) {
$sql = "UPDATE post SET post_approved = 1 WHERE ID = $value"; //write this query according to your table schema
mysql_query($sql) or die(mysql_error());
}
}
} elseif(isset($_POST['edit'])){
//do the edit things here
} elseif(isset($_POST['delete'])){
foreach ($_POST['check'] as $value){
$sql = "DELETE FROM post WHERE ID=$value";//modify it
mysql_query($sql) or die(mysql_error());
}
}
NOTE
You may not want to mutiple checkbox for edit. You just need to tweak the javascript code above a little and it'l send the ID as a post variable on form submission from which you can retreive the details for one entry and then edit functions will come. I'l leave it to you. try it, post your trial code here and i'l give you a solution if it doesn't work.

Categories