Unable to block while loop repeats - php

Working on invoice module which has two different tables master_table and detail_table. Know i want to display master_table's data above the table then in the table structure showing data of detail_table for each three rows header.
My code looks this.
$n = 0;
$htmlpage = 1;
$query = $dbConnection->prepare("SELECT * FROM master WHERE (DocNo BETWEEN ? AND ?) ORDER BY DocNo ASC");
$query->execute(array($fdcu,$tdcu));
while($rows = $query->fetch())
{
$n++;
$noInvs = $rows['No_of_Inv'];
$dno = $rows['DocNo'];
$name = $rows['Customer_Name'];
Step-1: Now i'm pulling client data from another table
$qry = $dbConnection->prepare("SELECT * FROM client WHERE client_name=?");
$qry->execute(array($name));
$row = $qry->fetch();
$add = $row['address'];
$city = $row['city'];
$proj = $rows['Project'];
$projNo = $rows['Project_No'];
$sermnth = $rows['Service_Month'];
$grandtotal = $rows['Total_Amt'];
$rupee = number2word($grandtotal);
$index = 1;
$ratetot = 0;
$subtotal = 0;
$lastPage = ceil($noInvs/3);
$pageCount = 0;
Step-2: Now check the loop iteration
while($index <= $noInvs)
{
$cnt = 1;
$body = "<div class='main-container'><table width='100%' style='padding:0 0 0 0;' align='center' cellpadding='0' cellspacing='0'>
<tr>
<td valign='top' align='center' style='border:1px solid #000;'>
<strong style='font-size:30px;'>Data</strong>
</td>
</tr>
<tr>
<td valign='top' align='left' style='padding: 0 0px 0 0px;border-left: solid 1px #000000;border-right: solid 1px #000000;border-bottom: solid 1px #000000;'>
<table width='100%' align='left' cellpadding='0' cellspacing='0'>
<tr>
<td valign='top' style='width:50%; padding:5px; font-size:13px; border-right: solid 1px #000000;text-align:left;'><strong>CUSTOMER:</strong> <br /><p style='font-size:12px;'>$name<br />$add, $city</p></td>
<td valign='top' style='width:50%; font-size:13px;'>
<table width='100%' cellpadding='0' cellspacing='0'>
<tbody>
<tr>
<td style='border-right: solid 1px #000000;padding:5px;width:40%;font-size:12px;'><br />Invoice No:<br />Invoice Date:</td>
<td align='left' style='padding:5px;font-size:12px;'><br />$ino<br />$invdate</td>
</tr>
</tbody>
</table>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td valign='top' align='left' style='border-left: solid 1px #000000;border-right: solid 1px #000000;border-bottom: solid 1px #000000;'>
<table width=100% cellpadding='0' cellspacing='0' >
<tbody>
<tr style='border-right:solid 1px #000000;border-bottom:solid 1px #000000;'>
<td style='width:5%;text-align:center; border-right:solid 1px #000000;border-bottom:solid 1px #000000;padding:3px 5px 3px 5px;font-size:12px;'>SN.</td>
<td style='width:45%;text-align:center; border-right:solid 1px #000000;border-bottom:solid 1px #000000;padding:3px 5px 3px 5px;font-size:12px;'>Description of Services</td>
<td style='width:13%;text-align:center; border-right:solid 1px #000000;border-bottom:solid 1px #000000;padding:3px 5px 3px 5px;font-size:12px;'>Per</td>
<td style='width:10%;text-align:center; border-right:solid 1px #000000;border-bottom:solid 1px #000000;padding:3px 5px 3px 5px;font-size:12px;'>Quantity</td>
<td style='width:12%;text-align:center; border-right:solid 1px #000000;border-bottom:solid 1px #000000;padding:3px 5px 3px 5px;font-size:12px;'>Rate</td>
<td style='width:15%;text-align:center; border-bottom:solid 1px #000000;padding:3px 5px 3px 5px;font-size:12px;'>Amount in Rs.</td>
</tr>";
Step-4: check the loop condition
while(true)
{
Step-5: checking the iteration count
if($cnt>3)
{
$pageCount++;
break;
}
Step-6: Pulling data from details table
$query1 = $dbConnection->prepare("SELECT * FROM details WHERE DocNo=? AND DocNO_Index=?");
$query1->execute(array($dno,$index));
$rowCnt = $query1->rowCount();
while($rows1 = $query1->fetch())
{
$desg = $rows1['Item'];
$des = $rows1['Description'];
$Quantity = $rows1['Quantity'];
$Rate = $rows1['Rate'];
$per = $rows1['Per'];
$gross = $rows1['Gross'];
$SERVICE_TAX = $rows1['SERVICE'];
$SERVICE_TAX = ($SERVICE_TAX==null)? 0.0 : $SERVICE_TAX;
$SB_Tax = $rows1['SB_Tax'];
$SB_Tax = ($SB_Tax==null)? 0.0 : $SB_Tax;
$Krishi_Kalyan_Cess = $rows1['Krishi_Kalyan_Cess'];
$Krishi_Kalyan_Cess = ($Krishi_Kalyan_Cess==null)? 0.0 : $Krishi_Kalyan_Cess;
$ratetot = $ratetot + $Rate;
$subtotal = $subtotal + $gross;
if($cnt == 1 && $index > 1)
{
$body.= "<tr><td style='width:5%;border-right:solid 1px #000;padding:3px 5px 3px 5px;font-size:12px;'></td><td style='width:45%;border-right:solid 1px #000;padding:3px 5px 3px 5px;font-size:12px;'>Opening Balance</td><td style='width:13%;border-right:solid 1px #000;padding:3px 5px 3px 5px;font-size:12px;'></td><td style='width:10%;border-right:solid 1px #000;padding:3px 5px 3px 5px;font-size:12px;'></td><td style='width:12%;text-align:right;border-right:solid 1px #000;padding:3px 5px 3px 5px;font-size:12px;'>". number_format($ratetot - $Rate, 2)." /-</td><td style='width:15%;text-align:right;padding:3px 5px 3px 5px;font-size:12px;'>". number_format($subtotal-$gross, 2)." /-</td></tr>";
}
$body.= "<tr><td style='width:5%;height:90px;vertical-align:top;border-right:solid 1px #000;padding:3px 5px 3px 5px;font-size:12px;'>$index</td><td style='width:45%;height:90px;vertical-align:top; border-right:solid 1px #000;padding:3px 5px 3px 5px;font-size:12px;'>";
if($proj!=null)
$body.= "$proj<br/>";
if($desg!=null)
$body.= "$desg<br/>";
if($des!=null)
$body.= "$des<br/>";
if($projNo!=null)
$body.= "$projNo<br/>";
if($sermnth!=null)
$body.= "$sermnth<br/>";
$body.= "</td><td style='width:13%;height:90px;vertical-align:top;text-align:center; border-right:solid 1px #000000;padding:3px 5px 3px 5px;font-size:12px;'>$per</td><td style='width:10%;height:90px;vertical-align:top; border-right:solid 1px #000000;padding:3px 5px 3px 5px;text-align:center;;font-size:12px;'>$Quantity</td><td style='width:12%;height:90px;vertical-align:top;text-align:right; border-right:solid 1px #000000;padding:3px 5px 3px 5px;font-size:12px;'>". number_format($Rate,2)."/-</td><td style='width:15%;height:90px;vertical-align:top; padding:3px 5px 3px 5px; text-align:right;;font-size:12px;'>". number_format($gross,2)."/-</td></tr>";
if($cnt == 3 || $index == $noInvs)
{
$body.= "<tr><td style='width:5%;border-right:solid 1px #000;padding:3px 5px 3px 5px;font-size:12px;'></td><td style='width:45%;border-right:solid 1px #000;padding:3px 5px 3px 5px;font-size:12px;'>";
if($index != $noInvs)
{
$body.= "Closing Balance";
}
$body.= "</td> <td style='width:13%;border-right:solid 1px #000;padding:3px 5px 3px 5px;font-size:12px;'></td><td style='width:10%;border-right:solid 1px #000;padding:3px 5px 3px 5px;font-size:12px;'></td><td style='width:12%;border-right:solid 1px #000;border-top:solid 1px #000;text-align:right;padding:3px 5px 3px 5px;font-size:12px;'>". number_format($ratetot,2)."/-</td><td style='width:15%;border-top:solid 1px #000;text-align:right;padding:3px 5px 3px 5px;font-size:12px;'>". number_format($subtotal,2)."/-</td></tr>";
}
}
$index++;
$cnt++;
}
$body .= "</tbody></table></td></tr>";
$stax = round($subtotal*($SERVICE_TAX/100),2);
$sbc = round($subtotal*($SB_Tax/100),2);
$kkc = round($subtotal*($Krishi_Kalyan_Cess/100),2);
$grandtotal = round(($subtotal+$stax+$sbc+$kkc),2);
$rupee = number2word($grandtotal);
$body .="<tr><td valign='top' align='left' style='border-left: solid 1px #000000;border-right: solid 1px #000000;border-bottom: solid 1px #000000;;font-size:12px;'><table cellpadding='0' cellspacing='0' width='100%'><tr><td rowspan='2' style='width:50%;height:50px; border-right:solid 1px #000000;padding:3px 5px 3px 5px;font-size:12px;'>";
if($pageCount == $lastPage)
{
$body .= "<strong style='font-size:12px;'>Narration : </strong><br/>Amount in Words (Rupees):<br/>";
}
$body .= "<br/>$rupee";
$body .= "</td><td style='width:35%; border-bottom:solid 1px #000000;border-right:solid 1px #000000;padding:3px 5px 3px 5px;font-size:12px;'>";
if($pageCount==$lastPage)
{
$body .= "Sub Total : <br />Service Tax # 14% <br />Swachh Bharat Cess # 0.5% <br />Krishi Kalyan Cess # 0.5% <br />";
}
$body .= "</td><td style='width:15%;text-align:right; border-bottom:solid 1px #000000;padding:3px 5px 3px 5px;;font-size:12px;'>";
if($pageCount==$lastPage)
{
$body .= "". number_format($subtotal,2)."/-<br />".number_format($stax,2)."/-<br />".number_format($sbc,2)."/-<br />".number_format($kkc,2)."/-<br />";
}
$body .= "</td></tr><tr><td style='width:35%; border-right:solid 1px #000000;padding:3px 5px 3px 5px;font-size:12px;'>";
if($pageCount==$lastPage)
{
$body .= "Total Amount";
}
$body .= "</td><td style='width:15%;text-align:right; padding:3px 5px 3px 5px;font-size:12px;'>";
if($pageCount == $lastPage)
{
$body .= number_format($grandtotal,2)."/-";
}
$body .= "</td></tr></table></td></tr><tr><td valign='top' align='left' style='border-left: solid 1px #000000;border-right: solid 1px #000000;border-bottom: solid 1px #000000;;font-size:12px;'><table width='100%' cellpadding='0' cellspacing='0'>
<tbody>
<tr>
<td style='width:50%; padding:3px 5px 3px 5px;border-right:1px solid #000;font-size:12px;'>
<strong>Thank you</strong>
</td>
<td valign='bottom' align='center' style='width:50%; text-align:center;font-size:12px;'>
<strong>AUTHORISED SIGNATORY</strong>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</table>
<p> </p></div>";
$body .= "<html><head><title>$ino</title><style type='text/css'>.main-container{display: block;} #media print{.main-container{display: block;}#page{size:portrait;margin:10px auto;padding:0}.main-container{page-break-after:always!important;}}</style><script language='javascript' type='text/javascript'>function PrintPage() { window.print(); } </script></head><body style='font-size: 12px ! important;'>$body</body></html><!-- onload='window.print()'-->";
echo "$body";
}
}
Issue is loop iteration.
My code output like this
Any suggestions on how to do this or to point me in the right direction would be greatly appreciated.

You're trying to re-use an active database connection. That doesn't work. You need two connections.
Specifically, you're in the process of reading a result set from a query on your connection,
$query = $dbConnection->prepare("some query");
$query->execute(array($fdcu,$tdcu));
while($rows = $query->fetch()) {
...
when you issue another query on the same $dbConnection ...
$qry = $dbConnection->prepare("another query");
$qry->execute(array($name));
You Can't Do That™ while the first query is still active. Open up and use a different connection for the queries issued from inside your loop.

Related

PHPMailer and no attachment selected - cannot send email - form disappeared

I'm not a PHP programmer so I'm asking for any help.
I try to send multiple attachments using PHPMailer function from a web form
When I select file or files and click on Send button I can receive email succesfully.
The problem is when I DO NOT SELECT any attachment. When I click on Send button and I do not add any attachment to my form I just get a blank web page - all my web form is not visible.
Can anybody help me and give any solution to my issue? Here is my code:
if(isset($_REQUEST['ownerName']) && (!empty($_REQUEST['ownerName']))){
$mail = new PHPMailer;
$mail->setFrom($ownerEmail);
$mail->addAddress($ownerEmail);
$mail->AddReplyTo($ownerEmail);
$mail->Subject = 'Ubezpieczenia podróży - zapytanie o oferte';
$mail->msgHTML("
<html><body>
<h2>DANE KLIENTA / DANE KONTAKTOWE:</h2>
<table style='border: 1px solid black;'>
<tr style='border: 1px solid black;'>
<th style='border: 1px solid black;'>Imie</th><th style='border: 1px solid
black;'>Nazwisko</th><th style='border: 1px solid black;'>PESEL</th><th style='border: 1px solid
black;'>Telefon</th ><th style='border: 1px solid black;'>Email</th>
</tr style='border: 1px solid black;'>
<tr style='border: 1px solid black;'>
<th style='border: 1px solid black;'>$ownerName</th><th style='border: 1px solid
black;'>$ownerSurname</th><th style='border: 1px solid black;'>$ownerPESEL</th><th style='border: 1px
solid black;'>$ownerPhoneNumber</th><th style='border: 1px solid black;'>$ownerEmail</th>
</tr>
</table>
<h2>DANE PODRÓŻY:</h2>
<table style='border: 1px solid black;'>
<tr style='border: 1px solid black;'>
<th style='border: 1px solid black;'>Strefa wyjazdu</th><th style='border: 1px solid
black;'>Liczba osób poniżej 26 lat</th><th style='border: 1px solid black;'>Liczba osób pomiędzy 26 a
65 lat</th><th style='border: 1px solid black;'>Liczba osób powyżej 65 lat</th><th style='border: 1px
solid black;'>Początek okresu ubezpieczenia</th><th style='border: 1px solid black;'>Koniec okresu
ubezpieczenia</th>
</tr>
<tr style='border: 1px solid black;'>
<th style='border: 1px solid black;'>$TripDirection</th><th style='border: 1px solid
black;'>$amountOfLess26</th><th style='border: 1px solid black;'>$amoountOfBetween26and65</th><th
style='border: 1px solid black;'>$amountOfMoreThan65</th><th style='border: 1px solid
black;'>$insuranceStartDate</th><th style='border: 1px solid black;'>$insuranceEndDate</th>
</tr>
<table>
</body></html>
");
foreach ($_FILES["attachment"]["name"] as $k => $v) {
$mail->AddAttachment( $_FILES["attachment"]["tmp_name"][$k], $_FILES["attachment"]["name"][$k] );
}
$mail->IsHTML(true);
$mail->CharSet = "UTF-8";
if(!$mail->send()){
echo "Błąd w trakcie wysyłania wiadomości: " . $mail->ErrorInfo;
}else{
echo "Dziękujęmy za wysłanie wiadomości!";
}
}
after few hours I found solution.
I needed to add
require_once("src/Exception.php");
And all email works good with and without attachments

dompdf stops unexpectedly after data collection with mysq

I have a file that converts my MySQL data to PDF using dompdf it works fine but sometimes it just throws 500 error and nothing shows up and after a few moments works and sometimes I am supposed to put a clean back up for it to work. All the is collected successfully with mysql and php but at last some errors in converting the PDF dont know is it dompdf bug or problem with my code. Please check the code below and help me make a better world... I greatly appreciate your help :)
<?php
$idswitch = $_GET['id']; // id for viewing
// Below is optional, remove if you have already connected to your database.
$mysqli = mysqli_connect('...', '..', '..', '...');
$sql=mysqli_query($mysqli,"SELECT * FROM si_invoices WHERE id='$idswitch'")or die(mysql_error());
$row=mysqli_fetch_assoc($sql);
$sample .= "<div style='font-family: Arial, Helvetica, sans-serif; background:#000000; width: 100%; color: #ffffff; padding: 4px; text-align: center; font-size: 120%;' >PROFOMA INVOICE: ID:100".$row['id']."</div>";
$sample .= "<div style='width:100%; height: 120px; background: #ffffff;'>";
$sample .= "<img src='logo-si.png' style='width: 250px; padding: 15px'>";
$sample .= "<div style='font-family: Arial, Helvetica, sans-serif; background:#000000; width: 100%; color: #ffffff; padding: 4px; text-align: center; font-size: 75%' >Interior Exterior General Signs LED Architectural & Modular Sign Systems Traffic & Safety Signs MV No. Plates</div><br>";
$sample .= "<div style='font-family: Arial, Helvetica, sans-serif; position:absolute; top: 35px; left: 300px; width: 400px; border-left: 2px solid #000000; padding-left: 10px; background: #ffffff; font-size: 85%'>
UBUNGO BUSINESS PARK-P.O.BOX 1965 - <br>DAR ES SALAAM-TANZANIA- +255 22 2866500 / 2860440 -<br>068444 422
E-MAIL: headsales#signindustries.co.tz - info#signindustries.co.tz - www.signindustries.co.tz
</div>";
$sample .= "</div>";
$sample .= "<div style='width:100%; margin-top: 15px;'>
<div style='float:left; width: 400px; height:margin-right: 10px;'>
<table style='width: 100%; height: 175px; border: 1px solid #000000; padding: 3px; font-size: 90%; font-family: Arial, Helvetica, sans-serif;'>
<tr>
<th align='left'></th>
<th align='left'></th>
</tr>
<tr>
<td width='30%'>Main Client:</td>
<td width='65%' align='left' style='border: 1px solid #000000;'>".$row['main_client']."</td>
</tr>
<tr>
<td width='30%'>Postal Address:</td>
<td width='65%' align='left' style='border: 1px solid #000000;'>".$row['postal_address']."</td>
</tr>
<tr>
<td width='30%'>Contact Person:</td>
<td width='65%' align='left' style='border: 1px solid #000000;'>".$row['contact_person']."</td>
</tr>
<tr>
<td width='30%'>Telephone / Cell:</td>
<td width='65%' align='left' style='border: 1px solid #000000;' >".$row['cell_offc']."</td>
</tr>
<tr>
<td width='30%'>Office Location:</td>
<td width='65%' align='left' style='border: 1px solid #000000;' >".$row['office_loc']."</td>
</tr>
<tr>
<td width='30%'>Email Address:</td>
<td width='65%' align='left' style='border: 1px solid #000000;' >".$row['email_addr']."</td>
</tr>
<tr>
<td width='30%'>Job Title:</td>
<td width='65%' align='left' style='border: 1px solid #000000;' >".$row['job_title']."</td>
</tr>
</table>
</div>
<div style='float:right; width: 40%; border: '>
<table style='width: 100%; height: 175px; border: 1px solid #000000; padding: 3px; font-size: 90%; font-family: Arial, Helvetica, sans-serif;'>
<tr>
<th align='left'></th>
<th align='left'></th>
</tr>
<tr>
<td colspan='2' align='center' style='background: #000000; color: #ffffff;'>Customer Tax Information:</td>
</tr>
<tr>
<td>TIN#:</td>
<td align='left' style='border: 1px solid #000000;' >".$row['clnt_tin']."</td>
</tr>
<tr>
<td>VRN#</td>
<td align='left' style='border: 1px solid #000000;' >".$row['clnt_vrn']."</td>
</tr>
<tr>
<td>VAT Exempted:</td>
<td align='left' style='border: 1px solid #000000;'>".$row['vat_expt']."</td>
</tr>
<tr>
<td>Exemption Cert</td>
<td align='left' style='border: 1px solid #000000;' >".$row['expt_cert']."</td>
</tr>
<tr>
<td colspan='2' align='center' style='background: #000000; color: #ffffff;'>Our Tax Information:</td>
</tr>
<tr>
<td>TIN#:</td>
<td align='left' style='border: 1px solid #000000;'>".$row['our_tin']."</td>
</tr>
<tr>
<td>VRN#:</td>
<td align='left' style='border: 1px solid #000000;' >".$row['our_vrn']."</td>
</tr>
</table>
</div>
<div style='clear:both;'>
</div>
<div>
</div>
</div>";
$sample .= "<table style='width: 100%; font-family: Arial, Helvetica, sans-serif; ' >
<tr>
<th align='center' style='border: 1px solid #000000;' >Requested Date:</th>
<th align='center' style='border: 1px solid #000000;'>Requested By:</th>
<th align='center' style='border: 1px solid #000000;'>Sales Person:</th>
</tr >
<tr>
<td align='center' style='border: 1px solid #000000;'>".$row['date_created']."</td>
<td align='center' style='border: 1px solid #000000;'>".$row['prepared_by']."</td>
<td align='center' style='border: 1px solid #000000;'>".$row['sales_person']."</td>
</tr>
</table>";
$idfk = $row['inv_code']; // id for viewing
$subtotl = $row['sub_totl']; // id for viewing
$vattotl = $row['vat_percent']; // id for viewing
$grandtotl = $row['grand_total']; // id for viewing
$dis_perc_sub = $row['dis_perc_sub']; // id for viewing
$lpouser = $row['lpo_user']; // id for viewing
$lpostatus = $row['lpo_status']; // id for viewing
$servername = "...";
$username = "...";
$password = "...";
$dbname = "....";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM itemsinv WHERE invidfk='$idfk' ORDER BY ln_no ASC";
$result = mysqli_query($conn, $sql);
$count = 0;
$sample .= "<table cellspacing='1' cellpadding='1' style='width:100%; border: 1px solid black; font-family: Arial, Helvetica, sans-serif; margin-top: 20px 0 0 0;
th, td { padding: 5px;}' >
<tr>
<th align='center' style='background: #000000; color: #ffffff; '>No</th>
<th align='center' style='background: #000000; color: #ffffff; '>Product Details</th>
<th align='center' style='background: #000000; color: #ffffff; '>Price</th>
<th align='center' style='background: #000000; color: #ffffff; '>Qty</th>
<th align='center' style='background: #000000; color: #ffffff; '>Total</th>
</tr>";
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
$count++;
$sample .= "<tr>
<td style='border: 1px solid black;'>" .$count. "</td>
<td style='border: 1px solid black;'><b>" . $row['productname']. "</b><br>" . $row['particulars']. "</td>
<td style='border: 1px solid black;'>". $row['price']. "</td>
<td style='border: 1px solid black;'>". $row['qty']. "</td>
<td style='border: 1px solid black;'>". $row['item_total']. "</td>
</tr>";
}
} else {
echo "0 results";
}
$sample .= "<tr><td align='right' colspan='4'><b>Sub Total:</b></td><td align='left' style='border: 1px solid #000000;'>".$subtotl."</td></tr>";
$sample .= "<tr><td align='right' colspan='4'><b>VAT Total:</b></td><td align='left' style='border: 1px solid #000000;'>".$vattotl."</td></tr>";
$sample .= "<tr><td align='right' colspan='4'><b>Discount %:</b></td><td align='left' style='border: 1px solid #000000;'>".$dis_perc_sub."</td></tr>";
$sample .= "<tr ><td align='right' colspan='4'><b>Grand Total:</b></td><td align='left' style='border: 1px solid #000000;'>".$grandtotl."</td></tr>";
$sample .= "</table>";
$sample .= "<br><b style='font-family: Arial, Helvetica, sans-serif; font-size: 70%;'>Please read our terms and conditions by visiting our website. http://signindustries.co.tz</b>";
mysqli_close($conn);
// create pdf of invoice
$invoiceFileName = 'Invoice-'. $row["contact_name"]. '.pdf';
require_once 'dompdf/src/Autoloader.php';
Dompdf\Autoloader::register();
use Dompdf\Dompdf;
$dompdf = new Dompdf();
$dompdf->loadHtml(html_entity_decode($sample));
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
$dompdf->stream($invoiceFileName, array("Attachment" => false));
?>
Error log below
Stack trace:
#0 /home/crmsignindustrie/public_html/crm/admins/dompdf/lib/Cpdf.php(4870): Imagick->writeimage('/tmp/cpdf_img_e...')
#1 /home/crmsignindustrie/public_html/crm/admins/dompdf/lib/Cpdf.php(4984): Cpdf->addImagePngAlpha('/home/crmsignin...', 45.265748031496, 737.92949241794, 187.5, 35.112359550562, 6)
#2 /home/crmsignindustrie/public_html/crm/admins/dompdf/src/Adapter/CPDF.php(864): Cpdf->addPngFromFile('/home/crmsignin...', 45.265748031496, 737.92949241794, 187.5, 35.112359550562)
#3 /home/crmsignindustrie/public_html/crm/admins/dompdf/src/Renderer/Image.php(110): Dompdf\Adapter\CPDF->image('/home/crmsignin...', 45.265748031496, 68.848148031496, 187.5, 35.112359550562, 'normal')
#4 /home/crmsignindustrie/public_html/crm/admins/dompdf/src/Renderer.php(293): Dompdf\Renderer\Image->render(Object(Dompdf\FrameDecorator\I in /home/crmsignindustrie/public_html/crm/admins/dompdf/lib/Cpdf.php on line 4870
[06-Jul-2020 13:58:06 UTC] PHP Warning: Version warning: Imagick was compiled against Image Magick version 1650 but version 1654 is loaded. Imagick will run but may behave surprisingly in Unknown on line 0
[06-Jul-2020 13:58:06 UTC] PHP Fatal error: Uncaught ImagickException: WriteBlob Failed `/tmp/cpdf_img_QpfKj7.png' # error/png.c/MagickPNGErrorHandler/1755 in /home/crmsignindustrie/public_html/crm/admins/dompdf/lib/Cpdf.php:4870
Stack trace:
#0 /home/crmsignindustrie/public_html/crm/admins/dompdf/lib/Cpdf.php(4870): Imagick->writeimage('/tmp/cpdf_img_Q...')
#1 /home/crmsignindustrie/public_html/crm/admins/dompdf/lib/Cpdf.php(4984): Cpdf->addImagePngAlpha('/home/crmsignin...', 45.265748031496, 737.92949241794, 187.5, 35.112359550562, 6)
#2 /home/crmsignindustrie/public_html/crm/admins/dompdf/src/Adapter/CPDF.php(864): Cpdf->addPngFromFile('/home/crmsignin...', 45.265748031496, 737.92949241794, 187.5, 35.112359550562)
#3 /home/crmsignindustrie/public_html/crm/admins/dompdf/src/Renderer/Image.php(110): Dompdf\Adapter\CPDF->image('/home/crmsignin...', 45.265748031496, 68.848148031496, 187.5, 35.112359550562, 'normal')
#4 /home/crmsignindustrie/public_html/crm/admins/dompdf/src/Renderer.php(293): Dompdf\Renderer\Image->render(Object(Dompdf\FrameDecorator\I in /home/crmsignindustrie/public_html/crm/admins/dompdf/lib/Cpdf.php on line 4870
[06-Jul-2020 13:59:05 UTC] PHP Warning: Version warning: Imagick was compiled against Image Magick version 1650 but version 1654 is loaded. Imagick will run but may behave surprisingly in Unknown on line 0
[06-Jul-2020 13:59:05 UTC] PHP Fatal error: Uncaught ImagickException: WriteBlob Failed `/tmp/cpdf_img_0wDRir.png' # error/png.c/MagickPNGErrorHandler/1755 in /home/crmsignindustrie/public_html/crm/admins/dompdf/lib/Cpdf.php:4870
Stack trace:
#0 /home/crmsignindustrie/public_html/crm/admins/dompdf/lib/Cpdf.php(4870): Imagick->writeimage('/tmp/cpdf_img_0...')
#1 /home/crmsignindustrie/public_html/crm/admins/dompdf/lib/Cpdf.php(4984): Cpdf->addImagePngAlpha('/home/crmsignin...', 45.265748031496, 737.92949241794, 187.5, 35.112359550562, 6)
#2 /home/host/public_html/crm/admins/dompdf/src/Adapter/CPDF.php(864): Cpdf->addPngFromFile('/home/crmsignin...', 45.265748031496, 737.92949241794, 187.5, 35.112359550562)
#3 /home/host/public_html/crm/admins/dompdf/src/Renderer/Image.php(110): Dompdf\Adapter\CPDF->image('/home/crmsignin...', 45.265748031496, 68.848148031496, 187.5, 35.112359550562, 'normal')
#4 /home/host/public_html/crm/admins/dompdf/src/Renderer.php(293): Dompdf\Renderer\Image->render(Object(Dompdf\FrameDecorator\I in /home/host/public_html/crm/admins/dompdf/lib/Cpdf.php on line 4870

HTML table not align in phpmail function

I am taking data from database and put it in html table that html table looks fine but when i press the send button for email send some times the html table is not aligned in email,it looks like below the date is not align in the specific coloumn
also like this
i don't why it showing "!". can any one guide why this happens,thanks
my code:
$text_mail.= "<table style='border:1px solid black;border-collapse:collapse;'>
<tr >
<th style='border:1px solid black; width:130px; padding:0 0 0 5px'>Country</th>
<th style='border:1px solid black; width:130px; padding:0 0 0 5px'>Network Name </th>
<th style='border:1px solid black; width:50px; padding:0 0 0 5px'>MCC</th>
<th style='border:1px solid black; width:50px; padding:0 0 0 5px'>MNC</th>
<th style='border:1px solid black; width:130px; padding:0 0 0 5px'>Old Price (Euro)</th>
<th style='border:1px solid black; width:130px; padding:0 0 0 5px'>New Price (Euro)</th>
<th style='border:1px solid black; width:130px; padding:0 0 0 5px'>Change</th>
<th style='border:1px solid black; width:130px; padding:0 0 0 5px'>Valid From (DD.MM.YYYY) </th>
</tr>";
while($row = mysql_fetch_array($queryRes))
{
if($row['pricefrom']==0){
$pricefrom="NA";
$change="New Network";
}else{
$pricefrom= $row['pricefrom'];
$change= $row['statusto'];
}
if($row['priceto']==0){
$priceto="NA";
$change="Not Covered";
}else{
$priceto= $row['priceto'];
$change= $row['statusto'];
}
$text_mail.= "<tr>";
$text_mail.= "<td style='border:1px solid black; width:130px; padding:0 0 0 5px;'><span></span><span></span><span></span><span></span>" . $row['country'] . "</td>";
$text_mail.= "<td style='border:1px solid black; width:130px; padding:0 0 0 5px;'><span></span><span></span><span></span><span></span>" . $row['networkname'] . "</td>";
$text_mail.= "<td style='border:1px solid black; width:50px; padding:0 0 0 5px;'><span></span><span></span><span></span><span></span>" . $row['mcc'] . "</td>";
$text_mail.= "<td style='border:1px solid black; width:50px; padding:0 0 0 5px;'><span></span><span></span><span></span><span></span>" . $row['mnc'] . "</td>";
$text_mail.= "<td style='border:1px solid black; width:130px; padding:0 0 0 5px;'><span></span><span></span><span></span><span></span>" . $pricefrom . "</td>";
$text_mail.= "<td style='border:1px solid black; width:130px; padding:0 0 0 5px;'><span></span><span></span><span></span><span></span>" . $priceto . "</td>";
$text_mail.= "<td style='border:1px solid black; width:130px; padding:0 0 0 5px;'><span></span><span></span><span></span><span></span>" . $change . "</td>";
$text_mail.= "<td style='border:1px solid black; width:130px; padding:0 0 0 5px;'><span></span><span></span><span></span><span></span>" .date('d.m.Y', $row['datetime']) . "</td>";
$text_mail.= "</tr>";
}
$text_mail.= "</table> <br><br>";
You should try like this:
$text_mail.= "<tr>
<td style='border:1px solid black; width:130px; padding:0 0 0 5px;'><span></span><span></span><span></span><span></span>" . $row['country'] . "</td>
<td style='border:1px solid black; width:130px; padding:0 0 0 5px;'><span></span><span></span><span></span><span></span>" . $row['networkname'] . "</td>
<td style='border:1px solid black; width:50px; padding:0 0 0 5px;'><span></span><span></span><span></span><span></span>" . $row['mcc'] . "</td>
<td style='border:1px solid black; width:50px; padding:0 0 0 5px;'><span></span><span></span><span></span><span></span>" . $row['mnc'] . "</td>
<td style='border:1px solid black; width:130px; padding:0 0 0 5px;'><span></span><span></span><span></span><span></span>" . $pricefrom . "</td>
<td style='border:1px solid black; width:130px; padding:0 0 0 5px;'><span></span><span></span><span></span><span></span>" . $priceto . "</td>
<td style='border:1px solid black; width:130px; padding:0 0 0 5px;'><span></span><span></span><span></span><span></span>" . $change . "</td>
<td style='border:1px solid black; width:130px; padding:0 0 0 5px;'><span></span><span></span><span></span><span></span>" .date('d.m.Y', $row['datetime']) . "</td>
</tr>";

Run foreach loop inside a msgHTML in PHPMailer

I want to run a foreach loop inside a msgHTML on PHP Mailer to make the table td dynamic from array. Below is the latest code I have. I' am working on dreamweaver and it show that this code has errors, I don't know how to solve this issue.
<?php
$mail->Subject = "Vacancy No: " . $vacancy_no . " - " . $vacancy_title;
$mail->MsgHTML(
"
Some html and text goes here....
Below is the main part of this topic.
<u><strong>Referees & Contact</strong></u><br/>
<table width='100%' border='0' cellpadding='0' cellspacing='0' style='border: 1px solid #E2E2E2;'>
<tr style='background: #003e82; color: #FFF; font-weight: bold;'>
<td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Name</td>
<td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Position & Organization</td>
<td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Phone Contact</td>
<td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Email</td>
</tr>
"
?>
<?php
foreach($variable as $var){
?>
<tr>
<td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Name</td>
<td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Position & Organization</td>
<td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Phone Contact</td>
<td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Email</td>
</tr>
<?php
}
?>
<?php "
</table>
");
?>
How about this code?
$mail->Subject = "Vacancy No: " . $vacancy_no . " - " . $vacancy_title;
$msg = "Some html and text goes here....
Below is the main part of this topic.
<u><strong>Referees & Contact</strong></u><br/>
<table width='100%' border='0' cellpadding='0' cellspacing='0' style='border: 1px solid #E2E2E2;'>
<tr style='background: #003e82; color: #FFF; font-weight: bold;'>
<td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Name</td>
<td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Position & Organization</td>
<td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Phone Contact</td>
<td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Email</td>
</tr>";
foreach($variable as $var){
$msg .= "<tr>
<td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Name</td>
<td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Position & Organization</td>
<td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Phone Contact</td>
<td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Email</td>
</tr>";
} // foreach
$msg .= "</table>";
$mail->MsgHTML( $msg );
try this
$mail->Subject = "Vacancy No: " . $vacancy_no . " - " . $vacancy_title;
$message =
"
Some html and text goes here....
Below is the main part of this topic.
<u><strong>Referees & Contact</strong></u><br/>
<table width='100%' border='0' cellpadding='0' cellspacing='0' style='border: 1px solid #E2E2E2;'>
<tr style='background: #003e82; color: #FFF; font-weight: bold;'>
<td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Name</td>
<td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Position & Organization</td>
<td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Phone Contact</td>
<td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Email</td>
</tr>
";
foreach ($variable as $var) {
$message .= "<tr>
<td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Name</td>
<td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Position & Organization</td>
<td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Phone Contact</td>
<td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Email</td>
</tr>";
}
$message .= "
</table>
";
$mail->MsgHTML($message);

PHP alternating colors

$dbc = mysql_connect('localhost','root','') or die (mysql_error());
mysql_select_db('payroll') or die (mysql_error());
$sql = "SELECT * FROM employee ORDER BY employee_id DESC";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)) {
echo "
<tr>
<td style=\"padding-left: 20px; border-bottom: 1px solid #999; border-right: 1px solid #999;\">".$row['first_name']." ".$row['last_name']."</td>
<td style=\"text-align: center; border-bottom: 1px solid #999; border-right: 1px solid #999; padding-top: 2px ; padding-bottom: 3px ;\"><input type=\"button\" name=\"edit\" value=\"Edit\" class=\"selbtn\"> <input type=\"button\" name=\"delete\" value=\"Delete\" class=\"selbtn\"></td>
</tr>
";
}
I wanted to add an alternating color in the loop. what code should I add?
$rowCount = 0;
$colorOne = '#ffffff';
$colorTwo = '#f3f3f3';
while($row = mysql_fetch_array($result)){
$rowColor = ($rowCount % 2) ? $colorOne : $colorTwo;
echo "<element bgcolor='$rowColor'></element>";
$rowCount++;
}
Edited after #Jayrox comment.
Use CSS classes instead of inline styles. They're easier to manipulate. Then style the .myclass_row_0 and .myclass_row_1 in your CSS for the alternate row color as well as .col1 and .col2 for the column styles.
$c=1; // or 0 (added after deceze's comment)
while($row = mysql_fetch_array($result)) {
$c=1-$c; // magic!
echo "
<tr class=\"myclass_row_$c\">
<td class=\"col1\">".$row['first_name']." ".$row['last_name']."</td>
<td class=\"col2\"><input type=\"button\" name=\"edit\" value=\"Edit\" class=\"selbtn\"> <input type=\"button\" name=\"delete\" value=\"Delete\" class=\"selbtn\"></td>
</tr>
";
}
Or using inline styles:
$colors = array('#ffffff','#f3f3f3');
$c=1;
while($row = mysql_fetch_array($result)) {
$c=1-$c;
echo "
<tr style=\"background-color:{$colors[$c]};\">
<!-- tds here -->
</tr>
";
}
Or using preset classes:
$styles = array('odd','even');
$c=1;
while($row = mysql_fetch_array($result)) {
$c=1-$c;
echo "
<tr class=\"{$styles[$c]}\">
<!-- ... -->
</tr>
";
}
There are already a lot of solutions, but this one use only two lines of css.
You can set specific css on even and odd rows:
tr:nth-child(even) {background: #CCC}
tr:nth-child(odd) {background: #FFF}
Usually, I will declare an index(int) value and increment for each time through the loop. Then do a check to see if index mod 2 = 1. If so, then output a tr with the style that you want to apply to show an alternating row.
$color = 0;
while($row = mysql_fetch_array($result)) {
if($color % 2 == 1){
echo "<tr>";
}else{
echo "<tr class=\"altRow\">";
}
echo "
<td style=\"padding-left: 20px; border-bottom: 1px solid #999; border-right: 1px solid #999;\">".$row['first_name']." ".$row['last_name']."</td>
<td style=\"text-align: center; border-bottom: 1px solid #999; border-right: 1px solid #999; padding-top: 2px ; padding-bottom: 3px ;\"><input type=\"button\" name=\"edit\" value=\"Edit\" class=\"selbtn\"> <input type=\"button\" name=\"delete\" value=\"Delete\" class=\"selbtn\"></td>
</tr>
";
$color++;
}
As it's bumped already, here is my 5 cents.
7 answers and not a single one using templates.
We can write a thousand articles of the templates necessity, but such an examples will always win.
So, based on the OP's code and stagas' answer:
business logic part:
$c = 1;
$DATA = array();
$sql = "SELECT * FROM employee ORDER BY employee_id DESC";
$result = mysql_query($sql) or trigger_error(mysql_error().$sql);
while($row = mysql_fetch_array($result)) {
$row['c'] = $c=1-$c;
$DATA[] = $row;
}
and template part:
<tr class="myclass_row_<?=$row['$c']?>">
<td class="col1"><?=$row['first_name']?> <?=$row['last_name']?></td>
<td class="col2">
<input type="button" name="edit" value="Edit" class="selbtn"> <input type="button" name="delete" value="Delete" class="selbtn">
</td>
</tr>
$color = 1;
while($row = mysql_fetch_array($result)) {
if($color == 1){
echo "
<tr>
<td style=\"padding-left: 20px; border-bottom: 1px solid #999; border-right: 1px solid #999;\">".$row['first_name']." ".$row['last_name']."</td>
<td style=\"text-align: center; border-bottom: 1px solid #999; border-right: 1px solid #999; padding-top: 2px ; padding-bottom: 3px ;\"><input type=\"button\" name=\"edit\" value=\"Edit\" class=\"selbtn\"> <input type=\"button\" name=\"delete\" value=\"Delete\" class=\"selbtn\"></td>
</tr>
";
$color = 2;
}
else
{
echo "
<tr>
<td style=\"padding-left: 20px; border-bottom: 1px solid #555; border-right: 1px solid #555;\">".$row['first_name']." ".$row['last_name']."</td>
<td style=\"text-align: center; border-bottom: 1px solid #555; border-right: 1px solid #555; padding-top: 2px ; padding-bottom: 3px ;\"><input type=\"button\" name=\"edit\" value=\"Edit\" class=\"selbtn\"> <input type=\"button\" name=\"delete\" value=\"Delete\" class=\"selbtn\"></td>
</tr>
";
}
$color = 1;
}
// here it is in less code
<?php
$dbc = mysql_connect('localhost','root','') or die (mysql_error());
mysql_select_db('payroll') or die (mysql_error());
$sql = "SELECT * FROM employee ORDER BY employee_id DESC";
$result = mysql_query($sql);
$colors = array('FFF', '000000'); // valid hex colors
$numOfColors = sizeOf($colors); $i = 0
while($row = mysql_fetch_array($result)) {
$i++;if($i>$numOfColors){$i=0;}
?>
<tr>
<td style="padding-left: 20px; border-bottom: 1px solid #<?=$colors[$i]?>; border-right: 1px solid #<?=$colors[$i]?>;"> <?=$row['first_name']?> <?=$row['last_name']?> </td>
<td style="text-align: center; border-bottom: 1px solid #<?=$colors[$i]?>; border-right: 1px solid #<?=$colors[$i]?>; padding-top: 2px ; padding-bottom: 3px ;"><input type="button" name="edit" value="Edit" class="selbtn"> <input type="button" name="delete" value="Delete" class="selbtn"></td>
</tr>
<?php
}
?>
Just keep track whether it is an alternating row with a Boolean. Initialize it to false before your loop, not it for each iteration, then you can set the row style based on its value. Something like:
...
$isAlternatingRow = false;
while($row = mysql_fetch_array($result)) {
echo "
<tr class=\"" . $isAlternatingRow ? "defaultRow" : "alternatingRow" . "\">
<td style=\"padding-left: 20px; border-bottom: 1px solid #999; border-right: 1px solid #999;\">".$row['first_name']." ".$row['last_name']."</td>
<td style=\"text-align: center; border-bottom: 1px solid #999; border-right: 1px solid #999; padding-top: 2px ; padding-bottom: 3px ;\"><input type=\"button\" name=\"edit\" value=\"Edit\" class=\"selbtn\"> <input type=\"button\" name=\"delete\" value=\"Delete\" class=\"selbtn\"></td>
</tr>
";
$isAlternatingRow = !($isAlternatingRow);
}
Then just define styles for tr.defaultRow td and tr.alternatingRow td.
$class="even"
while($row = mysql_fetch_array($result))
{
if($class == "even")
{
echo "<tr class='$class'>";
$class="odd"
}
else
{
echo "<tr class='$class'>";
$class="even";
}
...
}
I did it like this:
Remember to add a CSS class called "even", with styling of course.
<?php
include 'connect.php';
echo "<table id='hor-zebra'>";
$i = 0;
while($row = mysql_fetch_array($result))
{
if($i % 2 == 0)
{
echo "<tr class='even'>";
echo "<td>" . $row['something'] . "</td>";
echo "</tr>";
}
else
{
echo "<tr>";
echo "<td>" . $row['something'] . "</td>";
echo "</tr>";
}
$i++;
}
echo "</table>";
mysql_close($con);
?>
Using predefined color outside the loop.
$rowCount = 0;
$color = array('#ffffff','#f3f3f3');
while($row = mysql_fetch_array($result)){
$i = ($rowCount % 2);
echo "<element bgcolor='".$color["$i"]."'></element>";
$rowCount++;
}

Categories