Results from the table and link - php

I do not know how to add a link to the results of table.
I want to change # to
edit
Thank you in advance for your help.
foreach($results as $row) {
$table = '<tr>';
$table .= '<td>';
$table .= $row['filename'];
$table .= '#';
$table .= '</td>';
$table .= '<td class="col1 nw tar">';
$table .= $row['size'];
$table .= '</td>';
$table .= '<td class="col1 nw tar">';
$table .= $row['type'];
$table .= '</td>';
$table .= '<td class="col1 nw tar">£';
$table .= $row['caption'];
$table .= '</td>';
$table .= '</tr>';
}

In the fifth line of your code, instead of
$table .= '#';
you should write
$table .= 'edit';

foreach($results as $row) {
$table = '<tr>';
$table .= '<td>';
$table .= $row['filename'];
$table .= "<a href='photo_update.php?id={$row['id']}'>edit</a>";
$table .= '</td>';
$table .= '<td class="col1 nw tar">';
$table .= $row['size'];
$table .= '</td>';
$table .= '<td class="col1 nw tar">';
$table .= $row['type'];
$table .= '</td>';
$table .= '<td class="col1 nw tar">£';
$table .= $row['caption'];
$table .= '</td>';
$table .= '</tr>';
}

Related

Mpdf takes too much time to generate PDF

I'm using mPDF in Yii2 php framwork to generate the pdf as report.
getting the result as expected but it is taking too much time to generate the pdf-report (around 5-10 sec).
Pdf
This is the code for mPdf setup
ini_set('memory_limit', '6144M');
set_time_limit(10000);
$path = \Yii::getAlias("#vendor/MPDF/mpdf.php");
require_once($path);
$mpdf = new \mPDF('en-GB-x', '', '', '', 5, 5, 5, 5, 6, 3);
$mpdf->addPage('P');
$mpdf->packTableData = true;
$mpdf->SetDisplayMode('fullpage');
$mpdf->list_indent_first_level = 0;
$mpdf->showWatermarkText = true;
$mpdf->watermarkTextAlpha = 0.3;
$mpdf->simpleTables = true;
$file_name = '';
And this is the main code to generate PDF
$table = "<table width='100%'>";
$table .= "<thead>";
$table .= "<tr>";
$table .= "<td colspan='1' style='width: 20%;' align='left'></td>";
$table .= "<td colspan='5' style='width: 60%;' align='center'>";
$table .= "<h3 style='margin-top:20px; font-size: 26px;'>" . $ofc_detail['company_name'] . "</h3>";
$table .= "</td>";
$table .= "<td colspan='1' style='width: 40%; font-size: 12px;' align='center'>";
$table .= "<div><img src='" . $photo . "' width='31' height='50' /><br /></div>";
$table .= "</td>";
$table .= "</tr>";
$table .= "<tr >";
$table .= "<th colspan='7'><center>QUOTE</center></th>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<th colspan='7' align='left'>Date :" . $quotation_date . "</th>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<th colspan='7' align='left'>Quotation ID :" . $quotation_id . "</th>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<th colspan='7' align='left'>Created By :" . $created_by . "</th>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<th height='40' colspan='7' align='left'></th>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<th colspan='7' align='left'>To</th>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<th align='left'>Customer Name</th>";
$table .= "<td colspan='6'>" . $cust_name . "</td>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<th align='left'>Mobile </th>";
$table .= "<td colspan='6'>" . $mobile . "</td>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<th align='left'>Company Name</th>";
$table .= "<td colspan='6'>" . $organization . "</td>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<th align='left'>GST No.</th>";
$table .= "<td colspan='6'>" . $gst_no . "</td>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<th align='left'>Address</th>";
$table .= "<td colspan='6'>" . $addr_str . "</td>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<th align='left'></th>";
$table .= "<td colspan='6'>" . $addr_delivery . "</td>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<th height='60' colspan='7' align='left'></th>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<th colspan='7'><center>Sub: - " . $subject . "</center></th>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<td colspan='7'>Dear Sir,</td>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<td colspan='7'>As discussed I am sending you the quotation</td>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<th height='20' colspan='7' align='left'></th>";
$table .= "</tr>";
$table .= "</thead>";
$table .= "</table>";
$table .= "<table border='1' width='100%'>";
$table .= "<tr>";
$table .= "<th>S.No.</th>";
$table .= "<th>Item Name</th>";
$table .= "<th>MRP</th>";
$table .= "<th>Selling Price</th>";
$table .= "<th>Quantity</th>";
$table .= "<th>Discount</th>";
$table .= "<th>Amount</th>";
$table .= "</tr>";
$table .= "<tbody>";
$i = 1;
$tot = 0;
foreach ($query as $k => $v) {
$tot = $tot + $v['amt'];
if ($v['discount'] == '') {
$discount = '0';
} else {
$discount = $v['discount'];
}
$table .= "<tr>"
. "<td align='center'>" . $i++ . "</td>"
. "<td align='left'>" . $v['name'] . "</td>"
. "<td align='center'>" . $v['MRP'] . "</td>"
. "<td align='center'>" . $v['selling_price'] . "</td>"
. "<td align='center'>" . $v['qty'] . "</td>"
. "<td align='center'>" . $discount . "%</td>"
. "<td align='right'>" . $v['amt'] . "</td>"
. "</tr>";
}
$table .= "<tr>";
$table .= "<td colspan='1'></td>";
$table .= "<td colspan='5' align='right'>TOTAL</td>";
$table .= "<td colspan='1' align='right'>" . $tt_amt . "</td>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<td colspan='1'></td>";
$table .= "<td colspan='5' align='right'>DISCOUNT</td>";
$table .= "<td colspan='1' align='right'>" . $dis_amt . "</td>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<td colspan='1'></td>";
$table .= "<td colspan='5' align='right'>SUB TOTAL</td>";
$table .= "<td colspan='1' align='right'>" . $sub_amt . "</td>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<td colspan='1'></td>";
$table .= "<td colspan='5' align='right'>CGST</td>";
$table .= "<td colspan='1' align='right'>" . $cgst_amt . "</td>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<td colspan='1'></td>";
$table .= "<td colspan='5' align='right'>SGST</td>";
$table .= "<td colspan='1' align='right'>" . $sgst_amt . "</td>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<td colspan='1'></td>";
$table .= "<td colspan='5' align='right'>IGST</td>";
$table .= "<td colspan='1' align='right'>" . $igst_amt . "</td>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<td colspan='1'></td>";
$table .= "<td colspan='5' align='right'>CESS</td>";
$table .= "<td colspan='1' align='right'>" . $cess_amt . "</td>";
$table .= "</tr>";
if(!empty($queryToGetOtherItems)){
foreach ($queryToGetOtherItems as $data) {
if ($data['discount_percent'] == '') {
$discount = '0';
} else {
$discount = $data['discount_percent'];
}
$table .= "<tr>"
. "<td colspan='1'></td>"
. "<td colspan='5' align='right'>" . $data['name'] . "</td>"
. "<td colspan='1' align='right'>" . $data['total_amount'] . "</td>"
. "</tr>";
}
}
$table .= "<tr>";
$table .= "<td colspan='1'></td>";
$table .= "<td colspan='5' align='right'>GRAND TOTAL</td>";
$table .= "<td colspan='1' align='right'>" . $tot_amt . "</td>";
$table .= "</tr>";
$table .= "</tbody>";
$table .= "</table>";
$table .= "<table width='100%'>";
$table .= "</tbody>";
$table .= "<tr>";
$table .= "<th height='60' colspan='7' align='left'></th>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<td colspan='7' align='left'><strong>TERMS & CONDITIONS :</strong><br>" . $description . "</td>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<td align='left' colspan='7'><strong>With Regards</strong><br><br><strong>Name :</strong> " . $ofc_detail['company_name'] . "</td>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<td align='left' colspan='7'><strong>Mobile No</strong> :" . $ofc_detail['ofc_mob_one'] . "</td>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<th height='60' colspan='7' align='left'></th>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<td colspan='7'><center>Address : " . $ofc_detail['address'] . "</center></td>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<td colspan='7'><center>GST No : " . $ofc_detail['gst_number'] . "</center></td>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<td colspan='7'><center>E-Mail ID : " . $ofc_detail['ofc_email_one'] . "</center></td>";
$table .= "</tr>";
$table .= "</tbody>";
$table .= "</table>";
}
$file_name = "Quotation_" . $quotation_id . "_" . date('Y_m_d_his') . ".pdf";
$mpdf->WriteHTML($table);
$files = $_SERVER['DOCUMENT_ROOT'] . '/path/' . $file_name;
$content = $mpdf->Output($files, 'F');
To resolve the issue I've checked for the following this
Optimized the main query and was able to get response in just 0.26 to 0.30sec
but still not able to generate in less seconds.

Running executable code within PHPMailer

Please see the code that I have within the BODY of PHPMailer. System reports a Syntax error on the line $mail->Body .= ''. $row['updated_by'] . '';
I have read through documentation of PHPMailer but have not been able to find much about inserting PHP within the BODY part of the email.
$mail->Body = <<
<table class="table table-striped table-bordered sortable">
<thead>
<tr>
<th>Sales Agent</th>
<th>Company</th>
<th>Contact</th>
<th>Contact Medium</th>
<th>Contact Date</th>
<th>Result</th>
</tr>
</thead>
<tbody>
<?php
include 'database.php';
$pdo = Database::connect();
$yesterday = date('Y-m-d 00:00:00',strtotime("-1 days"));
$sql = "SELECT * FROM `customer_history` WHERE `date_contacted` = '$yesterday'";
$sth = $pdo->prepare($sql);
$sth->execute();
foreach ($sth->fetchAll(PDO::FETCH_ASSOC) as $row)
{
$mail->Body .= '<tr>';
$mail->Body .= '<td>'. $row['updated_by'] . '</td>';
$mail->Body .= '<td>'. $row['company_name'] . '</td>';
$mail->Body .= '<td>'. $row['first_name'] . '</td>';
$mail->Body .= '<td>'. $row['contacted_by'] . '</td>';
$mail->Body .= '<td>'. $row['date_contacted'] . '</td>';
$mail->Body .= '<td>'. $row['last_result'] . '</td>';
$mail->Body .= '</td>';
$mail->Body .= '</tr>';
}
Database::disconnect();
?>
</tbody>
</table>
</div>
</div>
END;
Firstly execute db query and after that add result to the body.
<?php
$history = '';
include 'database.php';
$pdo = Database::connect();
$yesterday = date('Y-m-d 00:00:00',strtotime("-1 days"));
$sql = "SELECT * FROM `customer_history` WHERE `date_contacted` = '$yesterday'";
$sth = $pdo->prepare($sql);
$sth->execute();
foreach ($sth->fetchAll(PDO::FETCH_ASSOC) as $row)
{
$history .= '<tr>';
$history .= '<td>'. $row['updated_by'] . '</td>';
$history .= '<td>'. $row['company_name'] . '</td>';
$history .= '<td>'. $row['first_name'] . '</td>';
$history .= '<td>'. $row['contacted_by'] . '</td>';
$history .= '<td>'. $row['date_contacted'] . '</td>';
$history .= '<td>'. $row['last_result'] . '</td>';
$history .= '</td>';
$history .= '</tr>';
}
Database::disconnect();
// Mail body
$mail->Body = <<<EOF
...
<tbody>
{$history}
</tbody>
...
EOF;

PHP - check if the current ID is the same as previous in foreach loop

I have this foreach loop:
foreach($dataSet1 as $data) {
$result .= '<tr>';
$result .= '<td bgcolor="#EBEBEB" width="100" class="veranda">' . $data['CUST_ID'] . '</td>';
$result .= '<td bgcolor="#EBEBEB" width="100" class="veranda">' . $data['LAST_NAME'] . '</td>';
$result .= '<td bgcolor="#EBEBEB" width="100" class="veranda">' . $data['PHONE'] . '</td>';
$result .= '<td bgcolor="#EBEBEB" width="100" class="veranda">' . $data['ORD_COUNT'] . '</td>';
$result .= '<td bgcolor="#EBEBEB" width="100" class="veranda">' . date('m/d/Y' ,strtotime($data['DATE_LAST'])) . '</td>';
$result .= '</tr>';
}
EDIT
I need to separate the users with bottom border, based on $data['CUST_ID'];
Example:
$data['CUST_ID'] = 2;
$data['CUST_ID'] = 2;
$data['CUST_ID'] = 2;
bottom-border;
$data['CUST_ID'] = 25;
$data['CUST_ID'] = 25;
bottom-border;
$data['CUST_ID'] = 2131;
bottom-border;...
Just save the previous ID in a variable and check it, and update it, in each loop iteration:
$previousId = '';
foreach($dataSet1 as $data):
if ($previousId !== '' && $previousId !== $data['CUST_ID']) {
// put a border
}
$previousId = $data['CUST_ID'];
$result .= '<tr>';
$result .= '<td bgcolor="#EBEBEB" width="100" class="veranda">' . $data['CUST_ID'] . '</td>';
$result .= '<td bgcolor="#EBEBEB" width="100" class="veranda">' . $data['LAST_NAME'] . '</td>';
$result .= '<td bgcolor="#EBEBEB" width="100" class="veranda">' . $data['PHONE'] . '</td>';
$result .= '<td bgcolor="#EBEBEB" width="100" class="veranda">' . $data['ORD_COUNT'] . '</td>';
$result .= '<td bgcolor="#EBEBEB" width="100" class="veranda">' . date('m/d/Y' ,strtotime($data['DATE_LAST'])) . '</td>';
$result .= '</tr>';
endforeach;
You need to set a variable outside the loop that will keep this information:
$previous = null;
So inside your loop you can ask:
if ($data['CUST_ID'] == $previous) {
// current customer id same as previous id
} else {
// not the same
}
And in the very end of your loop set the precious variable to the current one:
$previous = $data['CUST_ID'];
/* You have to declare a variable outside of the foreach loop
which will hold your value for previous id as you proceed through the loop.*/
$prev_cust_id = '';
foreach($dataSet1 as $data):
// Then inside the loop, declare a the border styling as empty.
//Make it not empty and as you need it to be if your IDs do not match.
$add_border = '';
if($data['CUST_ID'] != $prev_cust_id && $prev_cust_id != ''){
$add_border = 'style="border-bottom:1px solid black;"';
}
$result .= '<tr>';
// Assign border styling variable to your HTML element.
$result .= '<td '.$add_border.' bgcolor="#EBEBEB" width="100" class="veranda">' . $data['CUST_ID'] . '</td>';
$result .= '<td bgcolor="#EBEBEB" width="100" class="veranda">' . $data['LAST_NAME'] . '</td>';
$result .= '<td bgcolor="#EBEBEB" width="100" class="veranda">' . $data['PHONE'] . '</td>';
$result .= '<td bgcolor="#EBEBEB" width="100" class="veranda">' . $data['ORD_COUNT'] . '</td>';
$result .= '<td bgcolor="#EBEBEB" width="100" class="veranda">' . date('m/d/Y' ,strtotime($data['DATE_LAST'])) . '</td>';
$result .= '</tr>';
// Set current ID as previous ID before ending the loop for current ID.
$prev_cust_id = $data['CUST_ID'];
endforeach;

PHP create HTML table with a while loop from query sql

I have to do a query to get the total of product filtred by payment type
My table:
product_id, paymentType price
87 E 1
87 E 3
87 C 5
87 V 30
100 C 1
359 E 12
359 C 32
My query:
$query = select count(*) as qte, paymentType as PT, product_id as PID, sum(price) as PR from mytable
group by product_id, paymentType
order by product_id
the result of the query is :
product_id paymentType qte price
87 E 2 4
87 C 1 5
87 V 1 30
100 C 1 1
359 E 1 12
359 C 1 32
I need to create table on php as :
$output .= '<table border="1" >';
$output .= '<thead>';
$output .= '<tr>';
$output .= '<th><div style="width:150px" >product</div></th>';
$output .= '<th>paymentType E</th>';
$output .= '<th>Qte E</th>';
$output .= '<th>total price E</th>';
$output .= '<th>paymentType C </th>';
$output .= '<th>Qte C</th>';
$output .= '<th>total price C</th>';
$output .= '<th>paymentType v </th>';
$output .= '<th>Qte V</th>';
$output .= '<th>total price V</th>';
$output .= '</tr>';
$output .= '</thead>';
$output .= '<tbody>';
if ($row){
$old_product_id = -1;
do {
// ID du produit
$product_id = $row->PID;
/* switch($row->PT){
case 'E': {
$output .= ' <td class="center" >'.$row->PT.'</td>';
$output .= ' <td class="center" >'.$row->qte.' </td>';
$output .= ' <td class="center" >'.$row->PR.'</td>';
break;
}
case 'C: {
$output .= ' <td class="center" >'.$row->PT.'</td>';
$output .= ' <td class="center" >'.$row->qte.' </td>';
$output .= ' <td class="center" >'.$row->PR.'</td>';
break;
}
case 'V': {
$output .= ' <td class="center" >'.$row->PT.'</td>';
$output .= ' <td class="center" >'.$row->qte.' </td>';
$output .= ' <td class="center" >'.$row->PR.'</td>';
break;
}
}
*/
if ($old_product_id == $product_id){
$output .= '<tr>';
$output .= ' <td style="width:200px;">'.$row->PID.'</td>';
// payment type E
$output .= ' <td class="center" >'.$row->PT.'</td>';
$output .= ' <td class="center" >'.$row->qte.' </td>';
$output .= ' <td class="center" >'.$row->PR.'</td>';
// payment type C
$output .= ' <td class="center" >'.$row->PT.'</td>';
$output .= ' <td class="center" >'.$row->qte.' </td>';
$output .= ' <td class="center" >'.$row->PR.'</td>';
// payment type V
$output .= ' <td class="center" >'.$row->PT.'</td>';
$output .= ' <td class="center" >'.$row->qte.' </td>';
$output .= ' <td class="center" >'.$row->PR.'</td>';
$output .= '</tr>';
}else{
}
$old_product_id = $product_id;
}while ($row = sqlsrv_fetch_object($result));
$output .= '</tr>';
$output .= '</tbody>';
$output .= '</table>';
$this->db->free();
echo $output;
}else {
echo 'no data found ';
}
Any solution to get this worK ? thanks if advance :)
my script doesn't work , I can't show the row for the same product with different payment type in the same row
I created a script that would do the headers for you aswell:
Get data:
<?php
require('connect.php');
$tsql = "SELECT * FROM BLAH";
$stmt = sqlsrv_query($conn, $tsql);
## Get Results ##
$data = array(); ## Variable to hold data.
while (($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) !== false) {
$data[] = $row;
}
sqlsrv_free_stmt($stmt);
sqlsrv_close($conn);
?>
Display data:
<thead>
<tr>
<?php
foreach(array_keys($data[0]) as $key) {
echo "<th>" . $key . "</th>";
}
?>
</tr>
</thead>
<tbody>
<?php
foreach($data as $key) {
echo "<tr>";
foreach($key as $vals) {
echo "<td>" . $vals . "</td>";
}
echo "</tr>";
}
?>
</tbody>
This is a function which helps you to create a table not thinking about design. Just pass the SQL.
Call As like this -
$fieldname=array("name","Roll");
$query = "Select name,roll from Student";
create_table($query,$fieldname,$id='mytable');
function create_table($query,$fieldname,$id='mytable'){
print' <table id="'.$id.'" class="table table-striped">';
print'<tr>';
for($i=0;$i<sizeof($fieldname);$i++){
print'<th>'.$fieldname[$i].'</th>';
}
print'</tr>';
$result = mysql_query($query);
while($row= mysql_fetch_array($result)){
print ' <tr>';
for($i=0;$i<sizeof($row)/2;$i++){
print'<td>'.$row[$i].'</td>';
}
print' </tr>';
}
print'</table>';}

How to output a PHP array within a particular column of an html table

I want the product(s) column to list down all the products inside it's column, same with the Quantity column, it should print out the quantity against each product in the product(s) column. Closest I've gotten is shown in the picture at the bottom.
$cartOutput = "";
// Dynamic table row assembly.
$cartOutput .= '<tr>';
$cartOutput .= '<td>' .$order_id. '</td>';
$cartOutput .= '<td>'."Pending.". '</td>';
$cartOutput .= '<td>'.$payment_type. '</td>';
foreach($result_array as $listitem)
{
$cartOutput .= '<td>'.$listitem. '</td>';
$cartOutput .= '<tr>'.'</tr>';
//echo '</tr>';
}
$i=0;
foreach($quantity_array as $listitem)
{
$cartOutput .= '<td>'.$listitem. '</td>';
$cartOutput .= '<tr>'.'</tr>';
//echo '<td></td>';
//echo '</tr>';
}
$cartOutput .= '</tr>';
If you want all the items in a single cell, then why are you surrounding each item with <td> and </td>? This would be more appropriate:
echo '<td>';
foreach(...) {
echo $listitem . '<br />';
}
echo '</td>'
<?php
$cartOutput = "";
$cartOutput .= '<tr>';
$cartOutput .= '<td>' .$order_id. '</td>';
$cartOutput .= '<td>'."Pending.". '</td>';
$cartOutput .= '<td>'.$payment_type. '</td>';
$cartOutput .= '<td>';
foreach($result_array as $listitem) {
echo $listitem.PHP_EOL;
}
$cartOutput .= '</td>';
$cartOutput .= '<td>';
foreach($quantity_array as $itemquntity) {
echo $itemquntity.PHP_EOL;
}
$cartOutput .= '</td>';
$cartOutput .= '</tr>';
?>

Categories