TCPDF table header automatically move to the right - php

I have created a table with TCPDF. The header, which has a black background, has been positioned slightly to the right as though there is some padding on the left hand side. I cannot get it to line up correctly.
The code below is the HTML I have written to form the table.
$tbl ='<style>
th {background-color: black;color: white;float:left;}
.tal {text-align: left;float:left;}
</style>
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<th width="60px" style="border-right: 1px solid white;"><strong>Qty</strong></th>
<th width="1px"></th>
<th class="tal" width="388px" style="padding:10px 0; border-right: 1px solid white;"> <strong>Product or Service</strong></th>
<th width="1px"></th>
<th width="84px" style="border-right: 1px solid white;"><strong>Price Each</strong></th>
<th width="1px"></th>
<th width="84px"><strong>Total</strong></th>
</tr>
<tr>
<td height="267px">';
while($i <= $a) {
$tbl .= '<table height="267px" width="60px"><tr><td>' . $productsArray['product_quantity'][$i] . '</td></tr></table>';
$i++;
}
$tbl .= '</td><td border="1" width="0.5px" height="267px" style="background:url(images/bars-black.jpg) bottom right no-repeat"></td><td height="267px">';
while($j <= $a) {
$tbl .= '<table height="267px" width="388px"><tr><td class="tal"> ' . $productsArray['product_name'][$j] . '</td></tr></table>';
$j++;
}
$tbl .= '</td><td width="0.5px" height="267px" style="background:url(images/bars-black.jpg) bottom right no-repeat"></td><td height="267px">';
while($k <= $a) {
$tbl .= '<table height="267px" width="84px"><tr><td>' . $productsArray['product_price'][$k] . '</td></tr></table>';
$k++;
}
$tbl .= '</td><td border="1" width="0.5px" height="267px" style="background:url(images/bars-black.jpg) bottom right no-repeat"></td><td height="267px">';
while($l <= $a) {
$tbl .= '<table height="267px" width="84px"><tr><td>' . $productsArray['product_sub'][$l] . '</td></tr></table>';
$l++;
}
$tbl .= '</td>
</tr>
</table>';
}
The below code is the PHP I have used to show the table on the page.
$pdf->writeHTMLCell(175, 80, 20, 100, $tbl, 1, 1, 0, true, 'C', true);

Using tables within cells of another table is generally a really bad idea and is likely contributing to the problem you are having. Since it looks like your productsArray already has all of the data you need I would simply loop through it outputting each row as you go.
It's also worth pointing out that the empty header rows that are defined with a width of 1px conflict with your actual data rows which are defined with a width of 0.5px.
<style>
th {background-color: black;color: white;float:left;}
.tal {text-align: left;float:left;}
</style>
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<th width="60px" style="border-right: 1px solid white;"><strong>Qty</strong></th>
<th width="1px"></th>
<th class="tal" width="388px" style="padding:10px 0; border-right: 1px solid white;"> <strong>Product or Service</strong></th>
<th width="1px"></th>
<th width="84px" style="border-right: 1px solid white;"><strong>Price Each</strong></th>
<th width="1px"></th>
<th width="84px"><strong>Total</strong></th>
</tr>
<?php
while($i <= $a) {
?>
<tr>
<td height="267px"><?php echo $productsArray['product_quantity'][$i]; ?></td>
<td border="1" width="0.5px" height="267px" style="background:url(images/bars-black.jpg) bottom right no-repeat"></td>
<td height="267px"><?php echo $productsArray['product_name'][$i]; ?></td>
<td width="0.5px" height="267px" style="background:url(images/bars-black.jpg) bottom right no-repeat"></td>
<td height="267px"><?php echo $productsArray['product_price'][$i]; ?></td>
<td border="1" width="0.5px" height="267px" style="background:url(images/bars-black.jpg) bottom right no-repeat"></td>
<td height="267px"><?php echo $productsArray['product_sub'][$l]; ?></td>
</tr>
<?php
$i++;
}
?>
</table>

Related

How to remove the last column border written in php?

I have the following code for the output:
<?php
function HailstoneNumbers($x){
echo '<tbody>';
static $c=0;
echo '<tr style="border-bottom:1px solid black; border-width: medium;"> <tr:last-child style="border-bottom:0;">';
echo '<td>'.$c.'</td>'.'<td>'.$x.'</td>';
echo '</tr>';
if ($x == 1 && $c == 0){
// N is initially 1.
return $c;
}
else if ($x == 1 && $c != 0){
// N is reduced to 1.
$c++;
}
else if ($x % 2 == 0){
// If N is Even.
$c++;
HailstoneNumbers((int)($x / 2));
}
else if ($x % 2 != 0){
// N is Odd.
$c++;
HailstoneNumbers(3 * $x + 1);
}
return $c;
}
$x = $_GET['x'];
if(isset($_GET['x'])){
echo '<table style="border-collapse:collapse; border-width:medium;">
<thead>
<tr style="border-bottom:1px solid black; border-width:medium;">
<th>Iteration</th><th>x</th>
</tr>
</thead>';
HailstoneNumbers($x);
echo '</tbody>'.'</table>';
}
?>
But I want the table that looks something like:
I want border between columns in header and entries, and bottom borders on all rows except for the last row. However, I am stuck on how to do that in php file without writing a css file.
You can use CSS property "border-style:hidden" on table and instead of applying CSS on TR or TH tag you can apply CSS directly on TD, something like:
https://jsfiddle.net/kht8mnLc/6/
<table style="border-collapse:collapse; border-width:medium; border-style:hidden;">
<thead>
<tr>
<th style="border:1px solid black;">Iteration</th>
<th style="border:1px solid black;">x</th>
</tr>
</thead>
<tbody>
<tr>
<td style="border:1px solid black;">aaa</td>
<td style="border:1px solid black;">bbb</td>
</tr>
<tr>
<td style="border:1px solid black;">aaa</td>
<td style="border:1px solid black;">bbb</td>
</tr>
<tr>
<td style="border:1px solid black;">aaa</td>
<td style="border:1px solid black;">bbb</td>
</tr>
</tbody>
</table>

PHP & Virtuemart: change color of a number if this number is bigger than 1

The code at the virtumart is:
<th align="center" bgcolor="#EEEEEE" style="border: 1px solid #CCCCCC;"><?php echo JText::_('COM_VIRTUEMART_ORDER_PRINT_QTY') ?></th>
What I need is the following:
When the COM_VIRTUEMART_ORDER_PRINT_QTY is bigger than 1, the font color to be somehtinge else, for example red (the format I will do it later, now I just want the "if" code).
Thanks
Please try
<?php
$quantity = JText::_('COM_VIRTUEMART_ORDER_PRINT_QTY');
if($quantity > 1){
echo '<th align="center" bgcolor="#EEEEEE" style="border: 1px solid #CCCCC; color:#FF0000">'.$quantity.'</th>';
}else{
echo '<th align="center" bgcolor="#EEEEEE" style="border: 1px solid #CCCCCC;">'.$quantity.'</th>';
}
?>

How to add contents in new page using TCPDF?

I am using TCPDF stuff, I have created a PDF page with some Information. But i need to show some data to next page deliberately.
pdf -> AddPage();
above code is used to create a new page which i know. But i wanna add specific data to this particular page would be the problem.
Help me to come out on this issue.
I have written my code like this
while($row = mysql_fetch_array($query)){
$inv = $row["INVNUM"];
$weight = $row["WEIGHT"];
if($i<4){
if($row["SIZECODE"] == "02"){
$temp .= '<tr nobr="true">
<td class="size1" style="border:1px solid #000">'.$json_a[$i]["S"].'</td>
<td class="size2" style="border:1px solid #000">'.$json_a[$i]["M"].'</td>
<td class="size3" style="border:1px solid #000">'.$json_a[$i]["L"].'</td>
<td class="size4" style="border:1px solid #000">'.$json_a[$i]["XL"].'</td>
<td class="size5" style="border:1px solid #000">'.$json_a[$i]["2X"].'</td>
<td class="size6" style="border:1px solid #000">'.$json_a[$i]["3X"].'</td>
</tr>';
}
else if($row["SIZECODE"] == "03"){
$temp .= '<tr>
<td style="border:1px solid #000">'.$json_a[$i]["32"].'</td>
<td style="border:1px solid #000">'.$json_a[$i]["34"].'</td>
<td style="border:1px solid #000">'.$json_a[$i]["36"].'</td>
<td style="border:1px solid #000">'.$json_a[$i]["38"].'</td>
<td style="border:1px solid #000">'.$json_a[$i]["40"].'</td>
<td style="border:1px solid #000">'.$json_a[$i]["42"].'</td>
</tr>';
}
$i = $i+1;
}
else{
$pdf ->AddPage();
$i = 0;
$temp.='<tr nobr="true">
<td colspan="18" style="border:1px solid #000">All sales are Final.</td>
<td colspan="2" style="border:1px solid #000">SUB TOTAL:</td>
<td style="border:1px solid #000"></td>
<td style="border:1px solid #000"></td>
</tr>
<tr>
<td colspan="18" rowspan="2" style="border:1px solid #000">Number # : '.$rowFix["Num"].' </td>
</tr>
<tr>
<td colspan="2">TOTAL DUE: $</td>
<td style="border:1px solid #000"></td>
<td style="border:1px solid #000"></td>
</tr>';
}
}
I have to bring the table rows to the next page when the $i value greater than 4.
$pdf->AddPage('P', 'A4');
$pdf->Cell(0, 0, 'A4 PORTRAIT', 1, 1, 'C');
First, output the content for the first page. Then:
pdf -> AddPage();
Then, output for the second page. Anything sent after this command will be outputted onto the second page.

Displaying message outside of while loop

I have some working code where a directory is scanned and the PDF file results placed in a table. However, what I cannot seem to do is if there are no results then display a message in the table outside the while loop. Can someone help with this. Thanks
<?php
// WHAT IS IN THE STORAGE DIRECTORY
$sub = 'destcerts' . '/' . $_SESSION['kt_idcode_usr'];
// READ THE NAMES OF FILES IN THE SUB-DIRECTORY
$fff = new DirectoryIterator($sub);
$sss = array();
foreach ($fff as $filedata)
{
// SKIP THE "DOT" FILES
if ($filedata->isDot()) continue;
// ACTIVATE THIS LINE TO RESTRICT IT TO PDF FILES ONLY
if ($filedata->getExtension() != 'pdf') continue;
// CREATE LINKS TO THESE FILES
$nom = $filedata->getFilename();
$value = substr ($nom, 0, 4);
//$_SESSION['value'] = $value;
if($_SESSION['kt_idcode_usr'] == $value) {
$lnk
= '<img src="destcerts/PDF_icon_100.png" style="margin-bottom: 15px; margin-top:15px;"><br /><a href="'
. $sub
. '/'
. $nom
. '" style="color:#0099FF; text-decoration:none; font-size:12px; font-family: Verdana, Geneva, sans-serif;">'
. $nom
. '</a>'
;
}
// COLLECT THE LINKS HERE
$sss[] = $lnk;
}
// ACCUMULATE THE TABLE ROWS HERE
$trs = NULL;
// COLLECT GROUPS OF FOUR
while (!empty($sss))
{
$td1 = array_shift($sss) or NULL;
$td2 = array_shift($sss) or NULL;
$td3 = array_shift($sss) or NULL;
$td4 = array_shift($sss) or NULL;
// USE HEREDOC TO INSERT THESE INTO A TABLE ROW
$tr = <<<EOD
<tr>
<td align="center" width="20%" style="padding-bottom:20px !important;">$td1</td>
<td align="center" width="20%" style="padding-bottom:20px !important;">$td2</td>
<td align="center" width="20%" style="padding-bottom:20px !important;">$td3</td>
<td align="center" width="20%" style="padding-bottom:20px !important;">$td4</td>
</tr>
EOD;
// APPEND THE TABLE ROW TO THE OTHER ROWS
$trs .= $tr;
}
// USE HEREDOC TO INSERT THE TABLE ROWS INTO THE TABLE
$tab = <<<EOD
<table id="pdfDownload" width="94%" align="center" border="1" cellspacing="10" cellpadding="0" style="border:1px solid grey; padding-bottom: 10px; margin-bottom:20px;">
<th style="text-align:center; padding: 10px !important; padding-bottom: 20px; border:1px solid black; background-color: #3399FF; color: white; font-size: 18px !important;" colspan="4">Destruction Certificates Download</th>
<tr>
<th style="text-align:center; padding-top: 10px !important;"></th>
<th></th>
<th></th>
<th></th>
</tr>
$trs
</table>
EOD;
// SHOW THE WORK PRODUCT
echo $tab;
?>
Try enclosing the WHILE LOOP in a IF statement to verify if there is anything to loop.
If(!empty($sss)){
while (!empty($sss))
{
$td1 = array_shift($sss) or NULL;
$td2 = array_shift($sss) or NULL;
$td3 = array_shift($sss) or NULL;
$td4 = array_shift($sss) or NULL;
// USE HEREDOC TO INSERT THESE INTO A TABLE ROW
$tr = <<<EOD
<tr>
<td align="center" width="20%" style="padding-bottom:20px !important;">$td1</td>
<td align="center" width="20%" style="padding-bottom:20px !important;">$td2</td>
<td align="center" width="20%" style="padding-bottom:20px !important;">$td3</td>
<td align="center" width="20%" style="padding-bottom:20px !important;">$td4</td>
</tr>
EOD;
// APPEND THE TABLE ROW TO THE OTHER ROWS
$trs .= $tr;
}
}
else{
$msg = " Put some message to display that there are no files to display ";
$tr = <<<EOD
<tr>
<td align="center" colspan="4" style="padding-bottom:20px !important;">$msg</td>
</tr>
EOD;
$trs .= $tr;
}

2 issues on scroll bar in table

Got 2 questions regarding a table which output is below:
Question 1:
The table height is 500px, yet what I don't understand is that it looks like the content underneath which doesn't belong to the table is displayed within the table because of the scroll bar that goes so that down that it looks like the content underneath the table rows are in the table. So my question is how can I get the scroll bar to appear only if the 500px is met, rather than showing it straight away as it is doing now? How do I get the content (select box, heading, etc) be displayed underneath the table rather than in the table as it is doing in the screenshot
Question 2:
How can I get the scroll bar to be clipped on the side of the table? At the moment it is clipped just underneath the last column meaning it is taking up some of the last column's space and hence why table columns are not matching.
Below is html code of table:
<table id="tableqanda" align="center" cellpadding="0" cellspacing="0">
<thead>
<tr>
<th width="30%" class="question">Question</th>
<th width="8%" class="option">Option Type</th>
<th width="6%" class="noofanswers">Number of Answers</th>
<th width="8%" class="answer">Answer</th>
<th width="6%" class="noofreplies">Number of Replies</th>
<th width="6%" class="noofmarks">Number of Marks</th>
<th width="12%" class="images">Images</th>
</tr>
</thead>
</table>
<div id="tableqanda_onthefly_container">
<table id="tableqanda_onthefly" align="center" cellpadding="0" cellspacing="0">
<tbody>
<?php
foreach ($arrQuestionContent as $key=>$question) {
echo '<tr class="tableqandarow">'.PHP_EOL;
echo '<td width="30%" class="question">'.htmlspecialchars($question).'</td>' . PHP_EOL;
echo '<td width="8%" class="option">'.htmlspecialchars($arrOptionType[$key]).'</td>' . PHP_EOL;
echo '<td width="6%" class="noofanswers">'.htmlspecialchars($arrNoofAnswers[$key]).'</td>' . PHP_EOL;
echo '<td width="8%" class="answers">'.htmlspecialchars($arrAnswer[$key]).'</td>' . PHP_EOL;
echo '<td width="6%" class="noofreplies">'.htmlspecialchars($arrReplyType[$key]).'</td>' . PHP_EOL;
echo '<td width="6%" class="noofmarks">'.htmlspecialchars($arrQuestionMarks[$key]).'</td>' . PHP_EOL;
echo '<td width="12%" class="images">'.htmlspecialchars($arrImageFile[$key]).'</td>' . PHP_EOL;
echo '</tr>'.PHP_EOL;
}
?>
</tbody>
</table>
<h4>PARTICIPATING STUDENTS</h4>
<p>
<strong>Number of Participating Students:</strong> <?php echo $studentnum; ?>
</p>
<p>
<strong>Current Participating Students:</strong>
<br/>
<tr>
<td>
<select name="students" id="studentslist" size="10">
<?php
if($studentnum == 0){
echo "<option disabled='disabled' class='red' value=''>No Students currently in this Assessment</option>";
}else{
while ( $currentstudentstmt->fetch() ) {
echo "<option disabled='disabled' value='$dbStudentId'>" . $dbStudentAlias . " - " . $dbStudentForename . " " . $dbStudentSurname . "</option>" . PHP_EOL;
}
}
?>
</select>
</p>
Below is CSS:
#tableqanda_onthefly_container
{
width:100%;
overflow-y: scroll;
overflow-x: hidden;
max-height:500px;
}
#tableqanda_onthefly
{
width:100%;
overflow:auto;
clear:both;
}
#tableqanda, #tableqanda_onthefly{
border:1px black solid;
border-collapse:collapse;
table-layout:fixed;
}
#tableqanda{
width:100%;
margin-left:0;
float:left;
}
#tableqanda td {
vertical-align: middle;
border:1px black solid;
border-collapse:collapse;
}
#tableqanda th{
border:1px black solid;
border-collapse:collapse;
text-align:center;
}
I want my table to remain as a scrolling table with fixed headers
UPDATE
For question 1, try setting the overflow-y property to auto. This way the scrollbar will only be displayed once the table exceeds 500px in height.
So something like:
overflow-y:auto;
instead of
overflow-y:scroll;
ORIGINAL POST:
To answer your second question, you can just add the heads of the table as another row:
<div id="tableqanda_onthefly_container">
<table id="tableqanda_onthefly" align="center" cellpadding="0" cellspacing="0">
<tr>
<th width="30%" class="question">Question</th>
<th width="8%" class="option">Option Type</th>
<th width="6%" class="noofanswers">Number of Answers</th>
<th width="8%" class="answer">Answer</th>
<th width="6%" class="noofreplies">Number of Replies</th>
<th width="6%" class="noofmarks">Number of Marks</th>
<th width="12%" class="images">Images</th>
</tr>
<?php
foreach ($arrQuestionContent as $key=>$question) {
echo '<tr class="tableqandarow">'.PHP_EOL;
echo '<td width="30%" class="question">'.htmlspecialchars($question).'</td>' . PHP_EOL;
echo '<td width="8%" class="option">'.htmlspecialchars($arrOptionType[$key]).'</td>' . PHP_EOL;
echo '<td width="6%" class="noofanswers">'.htmlspecialchars($arrNoofAnswers[$key]).'</td>' . PHP_EOL;
echo '<td width="8%" class="answers">'.htmlspecialchars($arrAnswer[$key]).'</td>' . PHP_EOL;
echo '<td width="6%" class="noofreplies">'.htmlspecialchars($arrReplyType[$key]).'</td>' . PHP_EOL;
echo '<td width="6%" class="noofmarks">'.htmlspecialchars($arrQuestionMarks[$key]).'</td>' . PHP_EOL;
echo '<td width="12%" class="images">'.htmlspecialchars($arrImageFile[$key]).'</td>' . PHP_EOL;
echo '</tr>'.PHP_EOL;
}
?>
</table>
</div>
This way the scroll bar should be put at the very right, not underneath anything. The reason it wasn't before was because you had two different tables with independent widths.

Categories