If condition in mail body phpmailer - php

I am using Phpmailer and I am currently emailing the data from my database. However, I need to have a color coding depending on the chosen condition. For example, if the remarks is proceed, the data cell should be in green color and red if hold. Im not really sure how to apply if loop inside an echo. Please help
while ($rowwaf = mysqli_fetch_assoc($queryres)){
$mailbody = "
Good Day!<br><br>
Please see details below<br><br>
<strong>Details:</strong><br><br>
<table class=\"table\" cellpadding=\"5\" rules=\"all\" frame=\"box\" border=\"1\">
<tr><td class=\"data\"><strong>REQUEST DATE:</strong></td><td>".$rowwaf["req_date"]."</td></tr>
<tr><td class=\"data\"><strong>ISSUE ENCOUNTERED:</strong></td><td>".$rowwaf["issue"]."</td></tr>
<tr><td class=\"data\"><strong>URGENCY:</strong> </td><td>".$rowwaf["urgency"]."</td></tr>
<tr><td class=\"data\"><strong>REQUESTOR:</strong></td><td>".$rowwaf["requestor"]."</td></tr>
<tr><td class=\"data\"><strong>REMARKS:</strong></td>
-- if condition sample--
if {
$rowwaf["remarks"] == proceed
<td style="color: green">".$rowwaf["remarks"]."</td></tr>
}
else {
$rowwaf["remarks"] == hold
<td style="color: red">".$rowwaf["remarks"]."</td></tr>
}
";

You could just set a variable for your color(also the code you provided won't work):
<?php
if ($rowwaf['remarks'] === 'proceed') {
$color = 'green';
} elseif ($rowwaf['remarks'] === 'hold') {
$color = 'red';
}
?>
Then use the $color variable in your html
I'd also recommend to use heredoc syntax to create your body and use php variables to do something like :
$mailbody = <<<EOF
<td style="color: {$color}">{$rowwaf['remarks']}</td>
EOF;

Related

generate an Excel file using PHP

I'm new with PHP, I'm developping a WEB application that display a table with informations from an SQL server DATA BASE, the problem that I want to add a button to generate an EXCEL file that contains the table displayed? Is that possible??
#Ranjit this is the PHP code that displays the table and generate the excel file
edit 1
<?php
$ch="";
if(isset($_POST['historique']))
{
if ((!empty($_POST['Date_de_debut']))&& (!empty($_POST['Date_de_fin']))&&(!empty($_POST['Heure_de_debut']))&&(!empty($_POST['Heure_de_fin'])))
{
$ch= "(CONVERT(Datetime, '".$_POST['Date_de_debut']." ".$_POST['Heure_de_debut'].".000',120)) and (CONVERT(Datetime, '".$_POST['Date_de_fin']." ".$_POST['Heure_de_fin'].".000',120))";
?>
<table id="tab" border="1">
<tr>
<th><font color="red">Date</font></th>
<th><font color="red">Agent</font></th>
<th><font color="red">numéro</font></th>
</tr>
<?php
$search = " // my query
where operationDate between" .$ch;
$stmt = mssql_query($search);
while ($data = mssql_fetch_assoc($stmt))
{
?>
<tr>
<td><?php echo utf8_encode ($data['operationDate']);?></td>
<td><?php echo utf8_encode ($data['fullName']);?></td>
<td><?php echo utf8_encode ($data['number']);?></td>
</tr>
<?php
} }
?>
</table>
<?php
}
$output ='';
if(isset($_POST['excel']))
{
if ((!empty($_POST['Date_de_debut']))&& (!empty($_POST['Date_de_fin']))&&(!empty($_POST['Heure_de_debut']))&&(!empty($_POST['Heure_de_fin'])))
{
$rq = "// my query
where operationDate between" ."(CONVERT(Datetime, '".$_POST['Date_de_debut']." ".$_POST['Heure_de_debut'].".000',120)) and (CONVERT(Datetime, '".$_POST['Date_de_fin']." ".$_POST['Heure_de_fin'].".000',120))";
$res = mssql_query($rq);
if(mssql_num_rows($res)>0)
{
$output.='<table border=1>
<tr>
<th>Date</th>
<th>Depanneur</th>
<th>numéro</th>
</tr>
';
while ($row=mssql_fetch_array($res))
{
$output .='
<tr>
<td>'.$row["operationDate"].'</td>
<td>'.$row["fullName"].'</td>
<td>'.$row["number"].'</td>
</tr>';
}
$output .='</table>';
header("Content-Type: application/xls;charset=UTF-8");
header("Content-Disposition: attachement; filename=file.xls");
echo $output;
//mssql_close($conn);
}}}
?>
You'll have to select the data manually then insert them into the excel sheet, there's php library called PHPEXCEL you can use it.
See this
http://www.c-sharpcorner.com/article/export-to-excel-in-php-with-my-sql/
There are some nice packages out there to generate excel files such as
Box Spout and PHP Spreadsheet.
The documentation of both packages is very clear any you will be generating excel files within minutes of browsing through documentation.
Yes,
It is possible. You can follow this
1) Connect to database:
2) Define a filename of excel
//define separator (defines columns in excel & tabs in word)
$sep = "\t"; //tabbed character
$fp = fopen('database.xls', "w");
$schema_insert = "";
$schema_insert_rows = "";
//start of printing column names as names of MySQL fields
Sources - http://www.anillabs.com/2010/03/how-to-create-excel-file-with-mysql-data-using-php-code/

How can I add a SQL result of a PHP statement (fromMySQL) in an existing HTML file

I have an html file that contains a table with table rows
<table style="width:auto">
<tr>
<td contenteditable="true"><select id="so"></select></td>
</tr>
</table>
I have a PHP that selects from mySQL DB and returns username
if ($results->num_rows > 0) {
// output data of each row
while($row = $results->fetch_assoc()) {
$name=$row["first_name"];
echo "<option>
$name
</option>";
}
} else {
echo "0 results";
}
I am trying to incorporate the result of the php into my index.html file within the table so that the option shows up in the table
How can I get the data from PHP into an already built html table row?
Any help would be appreciated.
Two way to get the job done:-
1)Use a class, that is you should, and using its object you can retrieve the data and use it on the webpage .
ex:- see this class, it needs a lot of improvements and changes, but still can give you some idea . I am also trying to learn PDO well, not a PRO, but can be helpful to you.Comments on this class will help you get the idea .
2)Using AJAX, You can request for this data on loading the document or you can request it via get, post, request.. on any event.If you know ajax, you can use that class and ajax, or if you do not have an idea about AJAX, you can get a lot of resources to learn ajax. after this if you need my assistant, I will try to help you.
for this work you should use ajax to load data in html file ...
you can do it with javascript :
create a function for load your php file :
function loadAjax(address,elementId) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
document.getElementById(elementId).innerHTML = xhttp.responseText;
}
}
xhttp.open("GET", address, true);
xhttp.send();
}
after create it you can use it when you want (for example after load of page). you should pass address of your php file and id of html element that you want the answer load into it .
for more learn :
http://www.w3schools.com/ajax/
There are couple of methods that can be used here. If the HTML and PHP are in the same file you could do this:
<?php
if ($results->num_rows > 0) {
$options = ''; // declare the variable to hold the options
// output data of each row
while($row = $results->fetch_assoc()) {
$name=$row["first_name"];
$options .= "<option>$name</option>"; // add the options to the variable
}
} else {
$options = "0 results";
}
?>
And then add the $options to the HTML:
<table style="width:auto">
<tr>
<td contenteditable="true">
<select id="so">
<?php echo $options; ?>
</select>
</td>
</tr>
</table>
Another option, though not as neat, is to embed the PHP in the HTML:
<table style="width:auto">
<tr>
<td contenteditable="true">
<select id="so">
<?php
if ($results->num_rows > 0) {
// output data of each row
while($row = $results->fetch_assoc()) {
$name=$row["first_name"];
echo "<option>
$name
</option>";
}
} else {
echo "0 results";
}
?>
</select>
</td>
</tr>
</table>
Yet another option would be to use AJAX to call the PHP from the HTML page and return the results to the right location. For instance, if you wanted to use jQuery you could follow a tutorial to learn the basics and then apply that to your code.

how to store elements in array after validation with regular expression in php

I have a form in which will be inserted text with the web addresses. And i already have regular expression validation rule t search for web address. This rule will replace web links to another text. But my task is to write a function for saving all of this addresses to array. How can i save all of this addresses to array?
This is my code that i have for now:
<html>
<head>
<meta charset="UTF-8">
<title>Expressions: Find web address in text</title>
</head>
<body>
<?php
/*--------Functions---------*/
function webCheck($webtext){
global $web_check;
$web_check = "/((http:\/\/www\.)|(http:\/\/)|(www\.))([a-z0-9]+([a-z0-9-]*[a-z0-9]+)*\.)+[a-z]+/i";
return preg_replace($web_check, "<b>was weblink here</b>", $webtext);
}
/*--------End of Functions----------*/
if(isset($_POST["webadd"])){
$webtext = $_POST["webtext"];
if (!empty($webtext)){
echo webCheck($webtext);
}else{
echo "Feald cannot be empty. Please enter some text.";
}
} else {
echo "Please enter text in text area.";
}
?>
<form action="expression3.php" method="POST">
<table>
<tr>
<td>Find web address in text</td>
</tr>
<tr>
<td> <textarea name="webtext" cols="50" rows="10"></textarea></td>
</tr>
<tr>
<td><input type="submit" name="webadd" value="Find Web Address!" /></td>
</tr>
</table>
</form>
</body>
You can store anything into an array by just using the push function like so:
$my_links_array = array();
array_push($my_links_array, webCheck($webtext));
or through:
$array[] = webCheck($webtext);
which is practically the same for you but considered a lot faster
You can use preg_replace_callback that allows to execute code in a closure:
$myarr = array();
return preg_replace_callback($web_check,
function ($m) use (&$myarr) {
$myarr[] = $m[0];
return '<b>was weblink here</b>';
}, $webtext);
Note that to change a variable inside the closure, you need to pass it by reference.
Try..
/*--------Functions---------*/
$arrWebAddreses = array();
function webCheck($webtext){
global $web_check;
$web_check = "/((http:\/\/www\.)|(http:\/\/)|(www\.))([a-z0-9]+([a-z0-9-]*[a-z0-9]+)*\.)+[a-z]+/i";
$matches = array();
preg_match($web_check, $webtext, $matches);
if(count($matches)>0)
$arrWebAdresses += $matches[0];
return preg_replace($web_check, "<b>was weblink here</b>", $webtext);
}
/*--------End of Functions----------*/

Session Upload Progress & Multiple files

I am using this function here:
http://www.php.net/manual/en/session.upload-progress.php
And here is my code for the progress uploading:
<?
session_start();
$key = ini_get("session.upload_progress.prefix") . "myForm";
if(!empty($_SESSION[$key])) {
echo "<table width='400' style='font-family: Verdana; font-size: 12px;'>";
foreach($_SESSION[$key]['files'] as $f)
{
// Get percentage done
$current = $f["bytes_processed"];
$total = $f["content_length"];
if($current < $total) {
$done = ceil($current / $total * 100);
} else {
$done = 100;
}
echo " <tr>
<td colspan=2>{$f['name']}</td>
</tr>
<tr>
<td colspan=2><img src='uploading.gif' width='{$done}px' height='13px'></td>
</tr>
<tr>
<td>Started # ".date("H:m:s",$f['start_time'])."</td>
<td>{$done}%</td>
</tr>";
}
echo "</table>";
}
?>
The problem is that the individual files do not have a content_length variable. How can I work the progress for each file that is uploaded?
You can't, unless you're uploading a single file. $_SESSION[$key]['content_length'] is the total content length of the files that are being uploaded and the only indication of content length available. So you have two options: show a global progress of all files or limit the number of uploaded files to 1.
(Not sure if you are, but if you're willing to ditch this method you could opt for one of the numerous upload managers available. In the past I've used Fine Uploader with satisfaction.)

Stuck on placing Html code within some PHP Code

Hello there stack members,
I currently have a error report I wish to show - And Id like a static piece of html to be available for my GET errors.
Currently the way I have it
apicheck.php?key=dfdf - Displays a nice footer
apicheck.php?url=dfdf - Does not display footer as its currently referenced within the $_GET['url'] section.
What my ultimate goal is to have the html code somewhere around where the die function is as id like to have all 3 get error messages be able to display the HTML footer
Ive added the die function in so that I can keep the code separate from whats underneath
Im still quite new and this is my first type of adventure into anything like this
I wasnt too sure how to add the html anywhere else as it wouldnt be within one of the IF sections - id be grateful if someone could explain how to add it in other areas
<?php
echo "<html><head><title>Error Report</title><style>
<!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}.style1 {font-size: 9px}
-->
</style> </head><body>
<h1>API Authentication System 1.0.1 GPX</h1>
<HR size='1' noshade='noshade'>";
if(empty($_GET)){
echo "<p><b>Error Name:</b> <u>VAR_M</u><br>";
echo "<p><b>Description:</b> <u>No Variables Sent</u><br><br>";
}
if(empty($_GET['key'])){
echo "<p><b>Error Name:</b> <u>API_KEY</u><br>";
echo "<p><b>Description:</b> <u>Missing API-Key</u><br><br>";
}
if(empty($_GET['url'])){
echo "<p><b>Error Name:</b> <u>URL_M</u><br>";
echo "<p><b>Description:</b> <u>Missing URL</u><br>";
echo "</u></p><HR size='1' noshade='noshade'>
<h3 align='center' class='style1'>X Auth /1.0.1.GPX</h3>
</body>
</html>";
die();
}
else
?>
If you want to make reusing the same html structure easy, you could use a function to echo it.
function echoError($name, $description) {
echo "<p><b>Error Name:</b> <u>$name</u><br>";
echo "<p><b>Description:</b> <u>$description</u><br><br>";
}
Making your entire code look something like this:
<html>
<head>
<title>Error Report</title>
<style>
<!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}.style1 {font-size: 9px}
-->
</style>
</head>
<body>
<h1>API Authentication System 1.0.1 GPX</h1>
<HR size='1' noshade='noshade'>";
<?php
$error_found = false;
if(empty($_GET)){
echoError("VAR_M", "No Variables Sent");
$error_found = true;
}
if(empty($_GET['key'])){
echoError("API_KEY", "Missing API-Key");
$error_found = true;
}
if(empty($_GET['url'])){
echoError("URL_M", "Missing URL");
$error_found = true;
}
if ($error_found) {
echo "<HR size='1' noshade='noshade'><h3 align='center' class='style1'>X Auth /1.0.1.GPX</h3>";
}
?>
</body>
</html>
Is the footer you're referring to this text?
echo "</u></p><HR size='1' noshade='noshade'>
<h3 align='center' class='style1'>X Auth /1.0.1.GPX</h3>
</body>
</html>";
If so, just put it in a separate if statement that evaluates to true if any error condition is applicable:
if(empty($_GET) or empty($_GET['key']) or empty($_GET['url']) {
echo "</u></p><HR size='1' noshade='noshade'>
<h3 align='center' class='style1'>X Auth /1.0.1.GPX</h3>
</body>
</html>"
die();
}
Better yet, you could include a line like $error_found = 1; inside each of your other error message conditional blocks, and then just test for $error_found when printing the footer and the die() statement. That way, if you add additional error checks you don't have to remember to also add that conditional to the final if statement.
You could build a string (start with an empty string and concatenate the error messages to it as you get them) and then print the string wherever you want.
$errorString = "";
if(empty($_GET)) {
$errorString .= "<p><b>Error Name:</b> <u>VAR_M</u><br>";
...
And at the end, or wherever you want,
echo $errorString;
You could clean this up by putting the html sections in their own files and then including them using include "file.html"; You could alternativly simplify those echo statements by using Heredoc

Categories