Excel Export not accepting thousands separator - php

I have this build up HTML table that I want to convert into Excel spreadsheet but it happens to show the amount thousands separator and the 2 decimal as the PHP function number_format specifies. in short like for 100.00 in Excel I am having 100
$OutputExcel = "
<h3 align='center'>$COMPANYNAME</h3>
<table class='tblNemoList' border='0' cellpadding='2' cellspacing='0' width='100%' style='-fs-table-paginate: paginate;'>
<caption align='left'><b>Detailed Horse Charges</b></caption>
<thead>
<tr>
<th align='left'>#</th>
<th align='left'>No</th>
<th align='left'>Horse Name</th>
<th align='left'>Description</th>
<th align='left'>Date</th>
<th align='left'>Amount</th>
</tr>
</thead>
";
$i = 0;
while($row = $xdb->fetch_object($rowHorseCharges))
{
//ini
$i++;
$OutputPDF .= "
<tr >
<td>$i</td>
<td>".$row->No."</td>
<td>".$row->strHorse."</td>
<td>".$row->Description."</td>
<td>".$row->Date."</td>
<td align='right'>".number_format($row->Amount,2,'.',',')."</td>
</tr>
";
$OutputExcel .= "
<tr >
<td align='left' >$i</td>
<td align='left' >".$row->No."</td>
<td align='left' >".$row->strHorse."</td>
<td align='left' >".$row->Description."</td>
<td align='left' >".$row->Date."</td>
<td align='right' >".number_format($row->Amount,2,'.',',')."</td>
</tr>
";
}
$OutputPDF .= "
<tr><td colspan='6' style='border-bottom:1px solid black;'></td></tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td align='right' ><b>Tota: ".number_format($rowTotal->Total,2,'.',',')."</b></td>
</tr>
</table>";
$OutputExcel .= "
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td align='right' ><b>Total: ".number_format($rowTotal->Total,2,'.',',')."</b></td>
</tr>
</table>
";
I am converting it using this
switch($Action){
case "Clear":
break;
case "Export Excel":
include_once("reports/". $_REQUEST[radReport] .".php");
header("Content-type: application/x-msdownload");
header("Content-Disposition: attachment; filename=". $strFilename .".xls"); //gets set inside the report.rpt.php
header("Pragma: no-cache");
header("Expires: 0");
echo $OutputExcel;
die;
break;
case "ExportPDF":
include_once("reports/". $_REQUEST[radReport] .".php");
ini_set('display_errors', '0');
header("Content-type: application/pdf");
require_once('includes/mpdf/mpdf.php');
$mpdf = new mPDF('utf-8', $format ,9,'Arial');
$mpdf->setHTMLHeader(getPDFHeader($COMPANYNAME));
$mpdf->setHTMLFooter(getPDFFooter());
if($orientation == 'P'){$mpdf->AddPage($orientation,'', '' ,'','',15,15,20,16,7,9);}else{$mpdf->AddPage($orientation,'', '' ,'','',30,30,30,30,18,12);}
$mpdf->WriteHTML($OutputPDF);
$mpdf->Output($strFilename."pdf", "I");
die;
break;
case "OpenPDF":
default:
$page->Content .= $Output
. js("d('". $_POST[radReport] ."').click(function())");
break;
}

You need to add CSS to your table page. Like so:
<style type='text/css'>
.num2d {
mso-number-format:"0\.00";
}
</style>
and then use that as a class to the <td> like so:
<td class="num2d">34</td>
I didn't make this up myself, but found it here:
http://cosicimiento.blogspot.nl/2008/11/styling-excel-cells-with-mso-number.html

Related

Using include inside loop to send mail

I am trying to have my loop insert into the database 6 records automatically, the only thing that changes is the dates of these records, and whenever it enters it has to send an email with the always updated data, but the only one thing I got was to make it send only 1 email, and this email only has the last loop information.
The idea is to send an email whenever you insert it into the DB
Here is the code test:
<?php
$replace1="27-02-2019 00:00";
$replace2="27-02-2029 00:00";
$strStart = '2019-09-10';
$strEnd = '2029-09-10';
$dteStart = new DateTime($strStart);
$dteEnd = new DateTime($strEnd);
$dteDiff = $dteStart->diff($dteEnd);
$anos=$dteDiff->format("%Y");
$dias=$dteDiff->format("%d");
$meses=$dteDiff->format("%m");
$repete = ($anos * 12);
$rotina="6";
if($rotina == "6"){
$assunto = "Rotina Semestral";
$repete = ($repete / 6);
for($i = 0; $i <= $repete; $i++){
echo $i."==".$replace1." - ";
echo $replace2."<br>";
$replace1 = date('d-m-Y H:m',strtotime($replace1." +6 Months"));
include('email1.php');
};
}elseif($rotina == "3"){
//code...
};
}elseif($rotina == "1"){
$repete = $anos;
for($i = 0; $i <= $repete; $i++){
//code...
};
};
Here is email1.php:
<?php
$path = $_SERVER['DOCUMENT_ROOT'] . '\dist\PHPMailer_5.2.0\\';
$file = $path . 'class.phpmailer.php';
include($file);
$mail = new PHPMailer();
$mail->CharSet = 'UTF8';
$mail->IsSMTP();
$mail->Host = "$smtp";
$mail->SMTPAuth = false;
$mail->From = "$emailhost";
$mail->FromName = "Nova tarefa SIGMA ";
$mail->AddAddress ($email_post);
$mail->WordWrap = 99999;
$mail->IsHTML(true);
$mail->Subject = "Nova tarefa SIGMA WO/NF/ID Cliente: ".$won1;
$mail->AddEmbeddedImage('img/baixo.png', 'baixo');
$mail->Body = "<div style='margin-left:0px; padding:0px;width:800px;height:772px;style='color:#3366FF;'>
<h1 style='text-align: center; background-color:#0B95ED; color:#FFFFFF;font-family: Arial, Helvetica, sans-serif;line-height:2.5em;'>Informação do Pedido - Novo Pedido</h1>
<hr><br>
<!-- texto entre linhas -->
<table align='center' border='0' cellpadding='0' cellspacing='0' width='100%' id='templateColumns'>
<tr>
<td align='center' valign='top' width='50%' class=' '>
<table border='0' cellpadding='10' cellspacing='0' width='100%'>
<tr>
<td class='leftColumnContent' bgcolor='#B0C4DE'>
<b>WO/NF/ID Cliente:
</td>
</tr>
<tr>
<td class='leftColumnContent' bgcolor=''>
<!-- WO/NF/ID Cliente: -->
$won
</td>
</tr>
<tr>
<td class='leftColumnContent' bgcolor='#B0C4DE'>
<b>Responsável do pedido:
</td>
</tr>
<tr>
<td class='leftColumnContent' bgcolor=''>
<!-- Responsável do pedido: -->
$res_2
</td>
</tr>
<tr>
<td class='leftColumnContent' bgcolor='#B0C4DE'>
<b>Técnico 1:
</td>
</tr>
<tr>
<td class='leftColumnContent' bgcolor=''>
<!-- Técnico 2: -->
$tec1_1
</td>
</tr>
</table>
</td>
<td align='center' valign='top' width='50%' class=' '>
<table border='0' cellpadding='10' cellspacing='0' width='100%'>
<tr>
<td class='rightColumnContent' bgcolor='#B0C4DE'>
<b> Cliente:
</td>
</tr>
<tr>
<td class='rightColumnContent' bgcolor=''>
$cli
</td>
</tr>
<tr>
<td class='rightColumnContent' bgcolor='#B0C4DE'>
<font color='#B0C4DE'>Vazio</font>
</td>
</tr>
<tr>
<td class='rightColumnContent' bgcolor=''>
<font color='#FFFFFF'>Vazio</font>
</td>
</tr>
<tr>
<td class='rightColumnContent' bgcolor='#B0C4DE'>
<b>Técnico 2:
</td>
</tr>
<tr>
<td class='rightColumnContent' bgcolor=''>
$tec2_2
</td>
</tr>
</table>
</td>
</table>
<table border='0' cellpadding='0' cellspacing='0' width='100%' id='templateColumns'>
<tr>
<td align='center' valign='top' width='100%' class=' '>
<table border='0' cellpadding='10' cellspacing='0' width='100%'>
<tr>
<td class='leftColumnContent' bgcolor='#B0C4DE'>
<b>Descrição da Tarefa:
</td>
</tr>
</table>
</td>
</tr>
</table>
<table border='0' cellpadding='0' cellspacing='0' width='100%' id='templateColumns'>
<tr>
<td align='center' valign='top' width='100%' class=' '>
<table border='0' cellpadding='10' cellspacing='0' width='100%'>
<tr>
<td class='leftColumnContent' bgcolor=''>
<div class='b'>
$descre
</div>
</td>
</tr>
</table>
</td>
</tr>
</table>
<div>
</div>
<hr>
<table width='100%' border='0' cellspacing='0' cellpadding='0'>
<tr>
<td><center>
<div>
<!--[if mso]>
<v:roundrect xmlns:v='urn:schemas-microsoft-com:vml' xmlns:w='urn:schemas-microsoft-com:office:word' href='http://sigma.eda.pt' style='height:36px;v-text-anchor:middle;width:150px;' arcsize='5%' strokecolor='#EB7035' fillcolor='#EB7035'>
<w:anchorlock/>
<center style='color:#ffffff;font-family:Helvetica, Arial,sans-serif;font-size:16px;'>Bom Trabalho! →</center>
</v:roundrect>
<![endif]-->
<a href='http://sigma.eda.pt' style='background-color:#EB7035;border:1px solid #EB7035;border-radius:3px;color:#ffffff;display:inline-block;font-family:sans-serif;font-size:16px;line-height:44px;text-align:center;text-decoration:none;width:150px;-webkit-text-size-adjust:none;mso-hide:all;'>Bom Trabalho! →</a>
</div>
</td>
</tr>
</table>
";
$mail->Send();
I cant't test it for the moment but the new file will be something like this
<?php
$path = $_SERVER['DOCUMENT_ROOT'] . '\dist\PHPMailer_5.2.0\\';
$file = $path . 'class.phpmailer.php';
**include_once($file);**
function my_send_mail($smtp,$emailhost,$email_post,$won1,$won,$res_2,$tec1_1,$cli,$tec2_2,$descre){
$mail = new PHPMailer();
$mail->CharSet = 'UTF8';
$mail->IsSMTP();
$mail->Host = "$smtp";
$mail->SMTPAuth = false;
$mail->From = "$emailhost";
$mail->FromName = "Nova tarefa SIGMA ";
$mail->AddAddress ($email_post);
$mail->WordWrap = 99999;
$mail->IsHTML(true);
$mail->Subject = "Nova tarefa SIGMA WO/NF/ID Cliente: ".$won1;
$mail->AddEmbeddedImage('img/baixo.png', 'baixo');
$mail->Body = "<div style='margin-left:0px; padding:0px;width:800px;height:772px;style='color:#3366FF;'>
<h1 style='text-align: center; background-color:#0B95ED; color:#FFFFFF;font-family: Arial, Helvetica, sans-serif;line-height:2.5em;'>Informação do Pedido - Novo Pedido</h1>
<hr><br>
<!-- texto entre linhas -->
<table align='center' border='0' cellpadding='0' cellspacing='0' width='100%' id='templateColumns'>
<tr>
<td align='center' valign='top' width='50%' class=' '>
<table border='0' cellpadding='10' cellspacing='0' width='100%'>
<tr>
<td class='leftColumnContent' bgcolor='#B0C4DE'>
<b>WO/NF/ID Cliente:
</td>
</tr>
<tr>
<td class='leftColumnContent' bgcolor=''>
<!-- WO/NF/ID Cliente: -->
$won
</td>
</tr>
<tr>
<td class='leftColumnContent' bgcolor='#B0C4DE'>
<b>Responsável do pedido:
</td>
</tr>
<tr>
<td class='leftColumnContent' bgcolor=''>
<!-- Responsável do pedido: -->
$res_2
</td>
</tr>
<tr>
<td class='leftColumnContent' bgcolor='#B0C4DE'>
<b>Técnico 1:
</td>
</tr>
<tr>
<td class='leftColumnContent' bgcolor=''>
<!-- Técnico 2: -->
$tec1_1
</td>
</tr>
</table>
</td>
<td align='center' valign='top' width='50%' class=' '>
<table border='0' cellpadding='10' cellspacing='0' width='100%'>
<tr>
<td class='rightColumnContent' bgcolor='#B0C4DE'>
<b> Cliente:
</td>
</tr>
<tr>
<td class='rightColumnContent' bgcolor=''>
$cli
</td>
</tr>
<tr>
<td class='rightColumnContent' bgcolor='#B0C4DE'>
<font color='#B0C4DE'>Vazio</font>
</td>
</tr>
<tr>
<td class='rightColumnContent' bgcolor=''>
<font color='#FFFFFF'>Vazio</font>
</td>
</tr>
<tr>
<td class='rightColumnContent' bgcolor='#B0C4DE'>
<b>Técnico 2:
</td>
</tr>
<tr>
<td class='rightColumnContent' bgcolor=''>
$tec2_2
</td>
</tr>
</table>
</td>
</table>
<table border='0' cellpadding='0' cellspacing='0' width='100%' id='templateColumns'>
<tr>
<td align='center' valign='top' width='100%' class=' '>
<table border='0' cellpadding='10' cellspacing='0' width='100%'>
<tr>
<td class='leftColumnContent' bgcolor='#B0C4DE'>
<b>Descrição da Tarefa:
</td>
</tr>
</table>
</td>
</tr>
</table>
<table border='0' cellpadding='0' cellspacing='0' width='100%' id='templateColumns'>
<tr>
<td align='center' valign='top' width='100%' class=' '>
<table border='0' cellpadding='10' cellspacing='0' width='100%'>
<tr>
<td class='leftColumnContent' bgcolor=''>
<div class='b'>
$descre
</div>
</td>
</tr>
</table>
</td>
</tr>
</table>
<div>
</div>
<hr>
<table width='100%' border='0' cellspacing='0' cellpadding='0'>
<tr>
<td><center>
<div>
<!--[if mso]>
<v:roundrect xmlns:v='urn:schemas-microsoft-com:vml' xmlns:w='urn:schemas-microsoft-com:office:word' href='http://sigma.eda.pt' style='height:36px;v-text-anchor:middle;width:150px;' arcsize='5%' strokecolor='#EB7035' fillcolor='#EB7035'>
<w:anchorlock/>
<center style='color:#ffffff;font-family:Helvetica, Arial,sans-serif;font-size:16px;'>Bom Trabalho! →</center>
</v:roundrect>
<![endif]-->
<a href='http://sigma.eda.pt' style='background-color:#EB7035;border:1px solid #EB7035;border-radius:3px;color:#ffffff;display:inline-block;font-family:sans-serif;font-size:16px;line-height:44px;text-align:center;text-decoration:none;width:150px;-webkit-text-size-adjust:none;mso-hide:all;'>Bom Trabalho! →</a>
</div>
</td>
</tr>
</table>
";
$mail->Send();
}
and your default file will be
<?php
include('email1.php');
$replace1="27-02-2019 00:00";
$replace2="27-02-2029 00:00";
$strStart = '2019-09-10';
$strEnd = '2029-09-10';
$dteStart = new DateTime($strStart);
$dteEnd = new DateTime($strEnd);
$dteDiff = $dteStart->diff($dteEnd);
$anos=$dteDiff->format("%Y");
$dias=$dteDiff->format("%d");
$meses=$dteDiff->format("%m");
$repete = ($anos * 12);
$rotina="6";
switch($rotina){
case "6":
$assunto = "Rotina Semestral";
$repete = ($repete / 6);
for($i = 0; $i <= $repete; $i++){
echo $i."==".$replace1." - ";
echo $replace2."<br>";
$replace1 = date('d-m-Y H:m',strtotime($replace1." +6 Months"));
my_send_mail($smtp,$emailhost,$email_post,$won1,$won,$res_2,$tec1_1,$cli,$tec2_2,$descre);
};
break;
case "3":
//code...
break;
case "1":
$repete = $anos;
for($i = 0; $i <= $repete; $i++){
//code...
};
break;
}
But i don't know where you take the variable used inside the file mail1.php
I don't know what php lib you are using for sendmail but implemente exceptions in mail send http://php.net/manual/en/language.exceptions.php
Sorry for my bad english
Replace
$mail->Send()
whit this
try {
if ( !$mail->Send() ) {
$error = "Unable to send to: " . $to . "<br />";
throw new phpmailerAppException($error);
} else {
echo 'Message has been sent using <?php echo strtoupper($_POST['test_type']); ?><br /><br />';
}
} catch (phpmailerAppException $e) {
$errorMsg[] = $e->errorMessage();
}
if ( count($errorMsg) > 0 ) {
foreach ($errorMsg as $key => $value) {
$thisError = $key + 1;
echo $thisError . ': ' . $value;
}
}

Create table with different PHP variables including if statement

I have multiple quires retrieving data from mysql database (multiple tables)
SELECT description FROM table1 As descr WHERE type='MYTYPE'; //this shows description of each product type
SELECT count(id) FROM table2 As tp1 WHERE ACTIVE='Y' AND type=1"); //this is to count records that has this product type =1
SELECT count(id) FROM table2 As tp2 WHERE ACTIVE='Y' AND type=2");
SELECT count(id) FROM table2 As tp3 WHERE ACTIVE='Y' AND type=3");
SELECT count(id) FROM table2 As tp4 WHERE ACTIVE='Y' AND type=4");
What I'm trying to have is a table of each type description, along with count of active products has this type: ignoring the count if the count is 0
Here is modest try, I know it isn't perfect, therefore seeking help here, perhaps different approach will be also helpful, but this doesn't work as it have defining variable which has if statement and also logic is not correct?
$tbl_header = '<tr>
<th width=220px align=left>Type description</th>
<th width=10px align=left>Active products of this type</th>
</tr>';
$tbl_data =
if (tp1>0) {
'<tr>
<td width=220px align=left><font color=#000000>'. {descr[0][0]}.'</td></font>
<td width=10px align=left><font color=#FF0000>.'$tp1.'</td></font>
</tr>';}
if (tp2>0) {
'<tr>
<td width=220px align=left><font color=#000000>'. {descr[1][0]}.'</td></font>
<td width=10px align=left><font color=#FF0000>.'$tp2.'</td></font>
</tr>';}
;
echo "<table> $tbl_header $tbl_data </table>";
Try this method. It should work and you don't have to worry about if statements being in the middle of variable
<table>
<tr>
<th width=220px align=left>Type description</th>
<th width=10px align=left>Active products of this type</th>
</tr>
<?php
if ($tp1 > 0) {
?>
<tr>
<td width=2 20 px align=l eft>
< font color=# 000000>
<?php echo descr[0][0]; ?>
</font>
</td>
<td width=10px align=left>
<font color=#FF0000>
<?php echo $tp1; ?>
</font>
</td>
</tr>
<?php
}
?>
<?php
if ($tp2 > 0) {
?>
<tr>
<td width=2 20 px align=l eft>
< font color=# 000000>
<?php echo descr[1][0]; ?>
</font>
</td>
<td width=10px align=left>
<font color=#FF0000>
<?php echo $tp2; ?>
</font>
</td>
</tr>
<?php
}
?>
</table>
How about this?
<?php
$table_head = "<tr>
<th width=220px align=left>Type description</th>
<th width=10px align=left>Active products of this type</th>
</tr>";
if ($tp1 > 0) {
$table_data_1 = "<tr>
<td width=2 20 px align=l eft>
< font color=# 000000>". descr[0][0] . "</font>
</td>
<td width=10px align=left>
<font color=#FF0000>". $tp1. "</font>
</td>
</tr>";
}
if ($tp2 > 0) {
$table_data_2 = "<tr>
<td width=2 20 px align=l eft>
< font color=# 000000>". descr[1][0] . "</font>
</td>
<td width=10px align=left>
<font color=#FF0000>". $tp2. "</font>
</td>
</tr>";
}
echo "<table> $table_ head $table_data_1 $table_data_2 </table>";
?>
<?php
$table_data = "";
$table_head = "<tr>
<th width=220px align=left>Type description</th>
<th width=10px align=left>Active products of this type</th>
</tr>";
if ($tp1 > 0) {
$table_data = "<tr>
<td width=2 20 px align=l eft>
< font color=# 000000>". descr[0][0] . "</font>
</td>
<td width=10px align=left>
<font color=#FF0000>". $tp1. "</font>
</td>
</tr>";
}
if ($tp2 > 0) {
$table_data .= "<tr>
<td width=2 20 px align=l eft>
< font color=# 000000>". descr[1][0] . "</font>
</td>
<td width=10px align=left>
<font color=#FF0000>". $tp2. "</font>
</td>
</tr>";
}
echo "<table> $table_ head $table_data </table>";
?>

How can if statement be used within echo

I have to check the results that are coming from fetch array, I am using else if or if to get the remarks. But I do not know how to use it with in echo, When I run this code all the remarks statements come in same form as written in code.
In this I have to check the results that is coming from fetch array, so for this I am using else if or if to get the remarks..but i am do not know how to use it with in echo. When I run this code all the remarks statements come in same form as written in code
Code:
<?php
$answer = '';
include('config.php');
if(isset($_GET["results"]))
$answer = $_GET["results"];
$id = $_GET["idk"];
switch ($answer)
{
case 'Nursery':
$qqqs = mysql_query("select * from result where u_id='$id' AND sc='Nursery' ");
$rows=mysql_fetch_assoc($qqqs);
$a=$rows['u_id'];
$b=$rows['name'];
$c=$rows['fname'];
$d=$rows['reg'];
$e=$rows['sc'];
$f=$rows['ss'];
$g=$rows['se'];
$h=$rows['e1'];
$i=$rows['u1'];
$j=$rows['m1'];
$k=$rows['s1'];
$l=$rows['ss1'];
$m=$rows['i1'];
$n=$rows['e2'];
$o=$rows['u2'];
$p=$rows['e3'];
$pp=$rows['u3'];
$pic=$rows['picture'];
$total=$h+$i+$j+$k+$l+$m+$n+$o+$p;
$totals=230;
$res = ( $totals / $total) * 100;
// 0 digit after the decimal point
$res = round($res); // 67
// 1 digit after the decimal point
$res = round($res, 1); // 66.7
// 2 digits after the decimal point
$res = round($res, 2); // 66.67
echo "<html>
<head>
</head>
<body>
<table align='center' border='4' bgcolor='white' width='500' class='table table-bordered'>
<tr>
<td bgcolor='orange' colspan='4'><h2 align='center'>Leads Grammar School</h2><p align='center'>Babar Road, Kirri Jamandan, Multan</p></td>
</tr>
<tr>
<td bgcolor='yellow' colspan='4'><h2 align='center'>Monthly Test Feb-March</h2></td>
</tr>
<tr>
<td colspan='2'><img src='images/$pic' width='100px' height='100px' align='right'></td>
</tr>
<tr>
<td align='right'>Student's Name</td>
<td colspan='3'>$b</td>
</tr>
<tr>
<td align='right'>Father's Name</td>
<td colspan='3'> $c</td>
</tr>
<tr>
<td align='right'>Registration</td>
<td colspan='3'>$d</td>
</tr>
<tr>
<td align='right'>Student Class</td>
<td colspan='3'>$e</td>
</tr>
<tr>
<td align='right'>Exams</td>
<td colspan='3'>$f</td>
</tr>
<tr bgcolor='gray'>
<th>Subject Name</th>
<th>Marks</th>
<th>Obtained Marks</th>
<th>Grades</th>
</tr>
<tr>
<tr><td>English(Writing)</td>
<td>50</td>
<td> $h</td>
</tr>
<tr>
<tr ><td>Urdu(Writing)</td>
<td>50</td>
<td>$i</td>
</tr>
<tr>
<tr ><td>Math(Writing)</td>
<td>50</td>
<td> $j</td>
</tr>
<tr><th align='center' colspan='3' bgcolor='orange'>General Knowledge</th></tr>
<tr>
<tr ><td>Science</td>
<td>10</td>
<td> $k</td>
</tr>
<tr>
<tr ><td>S.Studies</td>
<td>10</td>
<td>$l</td>
</tr>
<tr>
<tr ><td>Islamiat</td>
<td>10</td>
<td>$m</td>
</tr>
<tr><th align='center' colspan='3' bgcolor='orange'>Poems</th></tr>
<tr>
<tr ><td>English </td>
<td>15</td>
<td> $n</td>
</tr>
<tr>
<tr ><td>Urdu</td>
<td>15</td>
<td>$o</td>
</tr>
<tr><th align='center' colspan='3' bgcolor='orange'>Book Reading</th></tr>
<tr>
<tr><td>English </td>
<td>10</td>
<td>$p</td>
</tr>
<tr>
<tr >
<td>Urdu</td>
<td>10</td>
<td>$pp</td>
</tr>
<tr>
<td><h1>Total Marks</h1></td>
<td>230</td>
<td>$total</td>
<td>$res</td>
</tr>
<tr><td><h2>Remarks</h2></td>
<td colspan='3'>
if(91=<$res<=100)
{
echo 'Exceptional';
}
else if(81=<$res<=90)
{
echo 'Excellent';
}
else if(71=<$res<=80)
{
echo 'Very Good';
}
else if(61=<$res<=70)
{
echo 'Good';
}
else if(51=<$res<=60)
{
echo 'Fair';
}
else if(40=<$res<=50)
{
echo 'Pass';
}
else if(01=<$res<=39)
{
echo ' Needs Improvement';
}
else if($res==0)
{
echo ' Needs Improvement';
}
</td>
</tr>
";
break;
}
?>
Try this:
<?php
$answer = '';
include('config.php');
if(isset($_GET["results"]))
$answer = $_GET["results"];
$id = $_GET["idk"];
switch ($answer)
{
case 'Nursery':
$qqqs = mysql_query("select * from result where u_id='$id' AND sc='Nursery' ");
$rows=mysql_fetch_assoc($qqqs);
$a=$rows['u_id'];
$b=$rows['name'];
$c=$rows['fname'];
$d=$rows['reg'];
$e=$rows['sc'];
$f=$rows['ss'];
$g=$rows['se'];
$h=$rows['e1'];
$i=$rows['u1'];
$j=$rows['m1'];
$k=$rows['s1'];
$l=$rows['ss1'];
$m=$rows['i1'];
$n=$rows['e2'];
$o=$rows['u2'];
$p=$rows['e3'];
$pp=$rows['u3'];
$pic=$rows['picture'];
$total=$h+$i+$j+$k+$l+$m+$n+$o+$p;
$totals=230;
$res = ( $totals / $total) * 100;
// 0 digit after the decimal point
$res = round($res); // 67
// 1 digit after the decimal point
$res = round($res, 1); // 66.7
// 2 digits after the decimal point
$res = round($res, 2); // 66.67
$out = "<html>
<head>
</head>
<body>
<table align='center' border='4' bgcolor='white' width='500' class='table table-bordered'>
<tr>
<td bgcolor='orange' colspan='4'><h2 align='center'>Leads Grammar School</h2><p align='center'>Babar Road, Kirri Jamandan, Multan</p></td>
</tr>
<tr>
<td bgcolor='yellow' colspan='4'><h2 align='center'>Monthly Test Feb-March</h2></td>
</tr>
<tr>
<td colspan='2'><img src='images/$pic' width='100px' height='100px' align='right'></td>
</tr>
<tr>
<td align='right'>Student's Name</td>
<td colspan='3'>$b</td>
</tr>
<tr>
<td align='right'>Father's Name</td>
<td colspan='3'> $c</td>
</tr>
<tr>
<td align='right'>Registration</td>
<td colspan='3'>$d</td>
</tr>
<tr>
<td align='right'>Student Class</td>
<td colspan='3'>$e</td>
</tr>
<tr>
<td align='right'>Exams</td>
<td colspan='3'>$f</td>
</tr>
<tr bgcolor='gray'>
<th>Subject Name</th>
<th>Marks</th>
<th>Obtained Marks</th>
<th>Grades</th>
</tr>
<tr>
<tr><td>English(Writing)</td>
<td>50</td>
<td> $h</td>
</tr>
<tr>
<tr ><td>Urdu(Writing)</td>
<td>50</td>
<td>$i</td>
</tr>
<tr>
<tr ><td>Math(Writing)</td>
<td>50</td>
<td> $j</td>
</tr>
<tr><th align='center' colspan='3' bgcolor='orange'>General Knowledge</th></tr>
<tr>
<tr ><td>Science</td>
<td>10</td>
<td> $k</td>
</tr>
<tr>
<tr ><td>S.Studies</td>
<td>10</td>
<td>$l</td>
</tr>
<tr>
<tr ><td>Islamiat</td>
<td>10</td>
<td>$m</td>
</tr>
<tr><th align='center' colspan='3' bgcolor='orange'>Poems</th></tr>
<tr>
<tr ><td>English </td>
<td>15</td>
<td> $n</td>
</tr>
<tr>
<tr ><td>Urdu</td>
<td>15</td>
<td>$o</td>
</tr>
<tr><th align='center' colspan='3' bgcolor='orange'>Book Reading</th></tr>
<tr>
<tr><td>English </td>
<td>10</td>
<td>$p</td>
</tr>
<tr>
<tr >
<td>Urdu</td>
<td>10</td>
<td>$pp</td>
</tr>
<tr>
<td><h1>Total Marks</h1></td>
<td>230</td>
<td>$total</td>
<td>$res</td>
</tr>
<tr><td><h2>Remarks</h2></td>
<td colspan='3'>";
if(91=<$res<=100)
{
$out .= 'Exceptional';
}
else if(81=<$res<=90)
{
$out .= 'Excellent';
}
else if(71=<$res<=80)
{
$out .= 'Very Good';
}
else if(61=<$res<=70)
{
$out .= 'Good';
}
else if(51=<$res<=60)
{
$out .= 'Fair';
}
else if(40=<$res<=50)
{
$out .= 'Pass';
}
else if(01=<$res<=39)
{
$out .= ' Needs Improvement';
}
else if($res==0)
{
$out .= ' Needs Improvement';
}
$out .="</td></tr>";
echo $out;
break;
}
?>
PHP does not have support for this syntax:
if(01=<$res<=39)
Instead you'll have to do this:
if(1 <= $res && $res <= 39)
Also, don't start your numbers with a 0 or they'll be interpreted as octal numbers (base 8) which will have some strange consequences.
EDIT upon closer inspection
Oh... additionally you indeed cannot have conditional statements inside an echo. You'll have to build the whole string in code first, and then echo it.
Simply end your first echo and then use your if statement to echo the remaining of the table.
<?php
$answer = '';
include('config.php');
if(isset($_GET["results"]))
$answer = $_GET["results"];
$id = $_GET["idk"];
switch ($answer)
{
case 'Nursery':
$qqqs = mysql_query("select * from result where u_id='$id' AND sc='Nursery' ");
$rows=mysql_fetch_assoc($qqqs);
$a=$rows['u_id'];
$b=$rows['name'];
$c=$rows['fname'];
$d=$rows['reg'];
$e=$rows['sc'];
$f=$rows['ss'];
$g=$rows['se'];
$h=$rows['e1'];
$i=$rows['u1'];
$j=$rows['m1'];
$k=$rows['s1'];
$l=$rows['ss1'];
$m=$rows['i1'];
$n=$rows['e2'];
$o=$rows['u2'];
$p=$rows['e3'];
$pp=$rows['u3'];
$pic=$rows['picture'];
$total=$h+$i+$j+$k+$l+$m+$n+$o+$p;
$totals=230;
$res = ( $totals / $total) * 100;
// 0 digit after the decimal point
$res = round($res); // 67
// 1 digit after the decimal point
$res = round($res, 1); // 66.7
// 2 digits after the decimal point
$res = round($res, 2); // 66.67
echo "<html>
<head>
</head>
<body>
<table align='center' border='4' bgcolor='white' width='500' class='table table-bordered'>
<tr>
<td bgcolor='orange' colspan='4'><h2 align='center'>Leads Grammar School</h2><p align='center'>Babar Road, Kirri Jamandan, Multan</p></td>
</tr>
<tr>
<td bgcolor='yellow' colspan='4'><h2 align='center'>Monthly Test Feb-March</h2></td>
</tr>
<tr>
<td colspan='2'><img src='images/$pic' width='100px' height='100px' align='right'></td>
</tr>
<tr>
<td align='right'>Student's Name</td>
<td colspan='3'>$b</td>
</tr>
<tr>
<td align='right'>Father's Name</td>
<td colspan='3'> $c</td>
</tr>
<tr>
<td align='right'>Registration</td>
<td colspan='3'>$d</td>
</tr>
<tr>
<td align='right'>Student Class</td>
<td colspan='3'>$e</td>
</tr>
<tr>
<td align='right'>Exams</td>
<td colspan='3'>$f</td>
</tr>
<tr bgcolor='gray'>
<th>Subject Name</th>
<th>Marks</th>
<th>Obtained Marks</th>
<th>Grades</th>
</tr>
<tr>
<tr><td>English(Writing)</td>
<td>50</td>
<td> $h</td>
</tr>
<tr>
<tr ><td>Urdu(Writing)</td>
<td>50</td>
<td>$i</td>
</tr>
<tr>
<tr ><td>Math(Writing)</td>
<td>50</td>
<td> $j</td>
</tr>
<tr><th align='center' colspan='3' bgcolor='orange'>General Knowledge</th></tr>
<tr>
<tr ><td>Science</td>
<td>10</td>
<td> $k</td>
</tr>
<tr>
<tr ><td>S.Studies</td>
<td>10</td>
<td>$l</td>
</tr>
<tr>
<tr ><td>Islamiat</td>
<td>10</td>
<td>$m</td>
</tr>
<tr><th align='center' colspan='3' bgcolor='orange'>Poems</th></tr>
<tr>
<tr ><td>English </td>
<td>15</td>
<td> $n</td>
</tr>
<tr>
<tr ><td>Urdu</td>
<td>15</td>
<td>$o</td>
</tr>
<tr><th align='center' colspan='3' bgcolor='orange'>Book Reading</th></tr>
<tr>
<tr><td>English </td>
<td>10</td>
<td>$p</td>
</tr>
<tr>
<tr >
<td>Urdu</td>
<td>10</td>
<td>$pp</td>
</tr>
<tr>
<td><h1>Total Marks</h1></td>
<td>230</td>
<td>$total</td>
<td>$res</td>
</tr>
<tr><td><h2>Remarks</h2></td>
<td colspan='3'>";
if(91=<$res<=100)
{
echo 'Exceptional';
}
elseif(81=<$res<=90)
{
echo 'Excellent';
}
else if(71=<$res<=80)
{
echo 'Very Good';
}
elseif(61=<$res<=70)
{
echo 'Good';
}
elseif(51=<$res<=60)
{
echo 'Fair';
}
elseif(40=<$res<=50)
{
echo 'Pass';
}
else if(01=<$res<=39)
{
echo ' Needs Improvement';
}
elseif($res==0)
{
echo ' Needs Improvement';
}
echo "</td>";
echo "</tr>";
break;
}
?>
You can use the ternary operator for conditions within echo:
echo "start of string....".(bool_condition1 ? "condition is true" : "condition is false").".... more string";
Yes, you can have if/else conditions in an echo by concatenating:
$x = 10;
echo "X is " . ($x==10? "equal" : "not equal") . " to 10";
I would also recommend that you create a function that return the string evaluating the score.
Try to mix as little logic as possible with the presentation.
You can't have an if statement inside echo. What you can do is to move all your if's outside the echo, and store the result in a variable. Eg:
if(91 <= $res && $res <= 100) // Note how the condition *should* be written.
{
$remark = 'Exceptional';
}
else if(...
and then use the variable in your table output:
<tr><td><h2>Remarks</h2></td>
<td colspan='3'>$remark
You could also use ternary operators, in combination with a custom function:
echo "fixed part".(inbetween($res,91,100)?'Exceptional':(inbetween($res,81,90)?'Excellent':'SOMETHING ELSE'));
function inbetween($int,$min,$max)
{
return ($int>=$min && $int<=$max);
}
The syntax of this ternary operator can be seen in the following example:
echo "This is the result: " . ( $testiftrue ? 'TRUE' : 'FALSE' );
This will test the condition $testiftrue, and then display 'TRUE' or 'FALSE'

Image on PDF not showing properly

I am, once again, having problems with PHP.
I working on a PDF where i have to show a image that also appears in the webpage.
The problem is that the image doesn't show on the PDF.
In fact it is a different kind of image, it is actually more like a chart.
Everything you do in the webpage is shown in the chart and i have to print it in the PDF, but the PDF simply ignores the print command.
Example of the code in HTML(which works just fine):
<html>
<h3>
you can take files copies in xls or Pdf format
</h3>
<br> Generate excel file <br> <BR>
<form method="post" action="" target="_blank">
<input type="submit" name="pdf" value="generate PDF " />
<input type="hidden" id="hide" name="hide">
</form><br>
<center><div id="image" name="image"><img src="../useful/createchart.php"/></div> </center>
</html>
as you can see it is used a PHP file to create the chart(which is not exactly a class, it just a PHP file with some methods).
So i am kind of clueless on what i have t do to the image be displayed in the PDF.
BTW I'm working with mPDF.
And here i have part of hte code to print the PDF, thats the part that matters. it prints the tables just fine but it simply doesn't print the image.
<?php // create PDF report ...
if (isset($_POST['pdf'])){
$mpdf = new mPDF();
//foreach ($showmore as $items){
$array = explode('-', $store);
//$Result = array();
foreach ($array as $member){
if($member != '')
$res= $dao->getinfobynumber ('jc', $member);
//var_dump($res);
foreach ($res as $jcvalue){
$mpdf->SetHeader("Tabela");
$mpdf->SetFooter(date('l jS \of F Y h:i:s A'));
$htmlpdf2 .= "
<table border='1'>
<tr>
<th width='150'>GE</th>
<td width='200'>". $jcvalue->getsg()."</td>
<td width='200'><strong>Nome Curto</strong></td>
<td width='200'>". $jcvalue->getnomecurto()."</td>
</tr>
</table>
<table border='1'>
<tr>
<th width='150'>Valor</th>
<td width='200'>". $jcvalue->getpvalor() ."</td>
<td width='200'><strong>FC</strong></td>
<td width='200'>". $jcvalue->getfc() ."</td>
</tr>
</table>
<table border='1'>
<tr>
<th width='150'> Jugular </th>
<td width='117'>". $jcvalue->getjugular1() ."</td>
<td width='117'>". $jcvalue->getjugular2() ."</td>
<td width='117'>". $jcvalue->getjugular3() ."</td>
<td width='118'>". $jcvalue->getjugular4() ."</td>
<td width='119'>". $jcvalue->getjugular5() ."</td>
</tr>
</table>
<table border='1'>
<tr>
<th width='150'>EXP3D</th>
<td width='200'>". $jcvalue->getd3_1() ."</td>
<td width='200'>". $jcvalue->getd3_2() ."</td>
<td width='200'>". $jcvalue->getd3_3() ."</td>
</tr>
</table>
<table border='1'>
<tr>
<th width='150'>EXP28D</th>
<td width='200'>". $jcvalue->getd28_1() ."</td>
<td width='200'>". $jcvalue->getd28_2() ."</td>
<td width='200'>". $jcvalue->getd28_3() ."</td>
</tr>
</table>
<table border='1'>
<tr>
<th width='150'>Carotida</th>
<td width='303'>". $jcvalue->getcarotida_1() ."</td>
<td width='303'>". $jcvalue->getcarotida_2() ."</td>
</tr>
</table>
<br><br>";
}
}
$PDFimage = "<center><div id='image' name='image'><img src='../useful/createchart.php'/></div></center>";
$mpdf->WriteHTML($htmlpdf2);
$mpdf->WriteHTML($PDFimage);
$mpdf->Output();
}
here is the code to use the web page to select what you want to see and the apge chows you all the data it has avaiable from what you selected + the chart based on the data:
<?php session_start();
//this file contains the full information for the sample
// will include in this file simple table contains all informations
// also will calling function from creat table and also will call function create chart and export to excel
require_once '../useful/createtable.php';
require_once '../dao/bancojc_424_dao.php';
require_once '../model/jcdrow.php';
require_once '../model/jc.php';
include("../useful/MPDF57/MPDF57/mpdf.php");
require_once '../dao/meta.php';
?>
<?php
$table = new createtable();
$dao = new daobancojc();
$meta = new metachart();
$meta->resetmeta();
$showmore =explode('-', $_GET['data']);
$showtable =explode('-', $_GET['table']);
$t='';
$htmlpdf='';
foreach($showmore as $item ){
if ($item !=''){
foreach($showtable as $tables)
if($tables == 'jc')
{
$result=$dao->getinfobynumber ('jc', $item);
$t='jc';
}elseif ($tables == 'j3d'){
$result=$dao->getinfobynumber ('j3d', $item);
$t='j3d';
}elseif($tables == 'j28d'){
$result=$dao->getinfobynumber ('j28d', $item);
$t='j28d';
}
$store .= '';
foreach ($result as $return){
$meta->insertmeta($t,$return->getsg());
$htmlpdf= 'No:'.$return->getsg(). '<br>';
$htmlpdf.= 'Nome Curto: '.$return->getnomecurto().' Nome Longo : ';
$htmlpdf.= $return->getnomelongo();
echo $htmlpdf;
?>
<table class="tabela_registros1" border=1 width=80% ><?php
$htmlpdf.= $table->morejc($return);
echo $htmlpdf;
?>
</table> <?php $store .= '-'.$return->getsg() ;
}

How to Export data in Excel including images in php

i am exporting data from database (MySQL) in excel sheet. i also want to export images in Excel using PHP.if any demo link is available, please share it here
You have to use one of the followig library to do that.
URLS:
https://github.com/PHPOffice/PHPExcel
https://phpexcel.codeplex.com/
https://code.google.com/p/php-excel/
What you're looking for is PHPExcel.
Here are some examples.
This is how I solved mine. Hope this helps!
public function export_items_to_excel(){
$items = $this->transaction->view_all_items();
$output = '';
$output .= "<table class='table' border='1'>
<thead>
<th style='background-color:#c7c7c7;'>NAME</th>
<th style='background-color:#c7c7c7;'>DESCRIPTION</th>
<th style='background-color:#c7c7c7;'>QUANTITY</th>
<th style='background-color:#c7c7c7;'>WEIGHT (KG)</th>
<th style='background-color:#c7c7c7;'>HS CODE</th>
<th style='background-color:#c7c7c7;'>SERIAL NO.</th>
<th style='background-color:#c7c7c7;'>UNIT VALUE</th>
<th style='background-color:#c7c7c7;'>CURRENCY</th>
<th style='width:220px !important;background-color:#c7c7c7;'>PICTURE</th>
</thead>
<tbody>
";
foreach($items as $item){
$output .= "
<tr>
<td style='text-align:center;'>".$item->item_name."</td>
<td style='text-align:center;'>".$item->item_description."</td>
<td style='text-align:center;'>".$item->item_quantity."</td>
<td style='text-align:center;'>".number_format($item->item_weight, 2)."</td>
<td style='text-align:center;'>".$item->item_hs_code."</td>
<td style='text-align:center;'>".$item->item_serial_number."</td>
<td style='text-align:center;'>".number_format($item->item_unit_value, 2)."</td>
<td style='text-align:center;'>".$item->item_currency."</td>
<td style='text-align:center;width:220px !important;height:220px !important;'><img src='".base_url()."assets/uploads/".$item->item_picture."' style='width:200px !important;height:152px !important;'> </td>
</tr>
";
}
$output .= "</tbody>
</table>
";
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header("Content-Disposition: attachment; filename=items.xls");
header("Cache-Control: max-age=0");
echo $output;
}

Categories