I am trying to create a page break in mPDF, but not succeeding, regardless of following every possible manual instruction, with the current attempt using this info:
https://mpdf.github.io/reference/html-control-tags/pagebreak.html
I am using mPDF version 8.0.2.
My current attempt looks like this:
function pdf_generate_table_html($data)
{
$page_length = 40;
$pages = ceil(count($data) / $page_length);
$keys = array_keys($data[0]);
$html = '';
$counter = 0;
for ($i = 1; $i <= $pages; $i++) {
$slice = array_slice($data, $counter, $page_length, true);
$html .= '<table style="width: 100%; border: 1px solid #dddddd;">';
$html .= '<thead>';
$html .= '<tr>';
foreach($keys as $key) {
$html .= '<th class="content" style="padding: 8px; background-color: #333333; color: #ffffff;">' . $key . '</th>';
}
$html .= '</tr>';
$html .= '</thead>';
$html .= '<tbody>';
foreach($slice as $key => $entry) {
$html .= '<tr>';
foreach($entry as $value) {
$html .= '<td class="content" style="padding: 8px;">' . $value . '</td>';
}
$html .= '</tr>';
}
$html .= '</tbody>';
$html .= '</table>';
$html .= '<pagebreak>';
$counter += $page_length;
}
return $html;
}
The following happens:
Without the page break and the code that slices the data into separate tables, the first page contains the header, second page contains ENTIRE table (and I want it split...).
With the page break, first page is blank, second page contains the header, third page contains 3 tables, correctly split, but resized and fit onto one page, fourth page is blank.
Important: Using <div> tags does not appear to be feasible for me, because I am using tabular data that may contain any number of columns, and I cannot find a way to force all columns to be the width of the widest item in a column.
Any idea what I am doing wrong, please?
Related
I have a little code that combines two arrays into a table with 2 columns. This works. But now I have a new array with values from $file3. I understand how to make a table with two columns, but how can I add a third column with the content of $file3?
<?php
$file1 = "c:/presencetool/ramfile1.txt";
$file2 = "c:/presencetool/ramfile2.txt";
$file3 = "c:/presencetool/ramfile3.txt"; //new file
if(file_exists($file1) && file_exists($file2))
{
$line1 = file($file1, FILE_IGNORE_NEW_LINES);
$line2 = file($file2, FILE_IGNORE_NEW_LINES);
$line3 = file($file3, FILE_IGNORE_NEW_LINES); //new array
$combine = array_combine($line1, $line2); //I want to also combine $line3 here
$html = '<table align="center">';
$html .= '<tr><td width="350px";></td><td></td></tr>';
$i = 1;
foreach ($combine as $key => $value):
$html .= '<tr class="'.$value.'">';
$html .= '<td font-size:"90pt">'.$key.'</td>';
$html .= '<td font-size:"90pt">'.$value.'</td>';
//here something like $html .= '<td font-size:"90pt">'.$value2.'</td>';
$html .= '</tr>';
$i++;
endforeach;
$html .= '</table>';
echo $html;
}
In $file1, $file2 and $file3 are names, addresses and emails of some users.(one value per line)
if your array looks like this :
$line[] = ['name' => 'name1','email' => 'email#mail.com'];
$line[] = ['name' => 'name2','email' => 'email2#mail.com'];
$line[] = ['name' => 'name3','email' => 'email3#mail.com'];
Then try the below, hope will help you
$html = '<table align="center" border="1" width="50%">';
$html .= '<tr><th>Name</th><th>Email</th><th>Address</th></tr>';
for($i=0;$i<count($line1);$i++){
$html .= '<tr class="'.$line1[$i].'">';
$html .= '<td font-size:"90pt">'.$line1[$i].'</td>';
$html .= '<td font-size:"90pt">'.$line2[$i].'</td>';
$html .= '<td font-size:"90pt">'.$line3[$i].'</td>';
$html .= '</tr>';
}
$html .= '</table>';
echo $html;
So I am trying to make a multiplication table and have run into this issue :
<?php
$table = '';
if ($_POST) {
$table .= '<table border="1">';
for ($i = 0; $i < $_POST['qty_line']; $i++) {
$table .= '<tr>';
for ($j = 0; $j < $_POST['qty_column']; $j++) {
$val = $i * $j;
$table .= '<td width="50"> VARIABLE GOES HERE </td>';
}
$table .= '</tr>';
}
$table .= '</table>';
}
?>
Sorry if this is a very simple question but I can't find a solution after searching. Basically i want to insert the $val variable into where it says "VARIABLE GOES HERE" on that specific table data cell. How would I go about this?
You could use
$table .= '<td width="50">' . $val . '</td>';
See the documentation for more examples.
I have an array that i pull from a database that looks like this
id - name - shortlink - downloadurl - count - date
Now I want to display that array as a table so the administartor of the site can se the content of the database. For that, I'm using this code:
function build_table($array){
// start table
$html = '<table id="usertable">';
// header row
$html .= '<tr class="header">';
foreach($array[0] as $key=>$value){
$html .= '<th>' . $key . '</th>';
}
$html .= '</tr>';
// data rows
foreach( $array as $key=>$value){
$html .= '<tr>';
foreach($value as $key2=>$value2){
$html .= '<td>' . $value2 . '</td>';
}
$html .= '</tr>';
}
// finish table and return it
$html .= '</table>';
return $html;
And it works great.
The problem is that i wan't do display some of the columns with a different code. E.g. 'downloadurl' which is an webadress, i want to make clickable. I just can't figure out how to split up the function so that i can write the code for the individual columns.
You could try something similar to the following:
using conditionals
This is probably the most straight forward way to go about handling different attributes per column - not without its drawbacks (noted below)
// data rows
foreach( $array as $key => $value) {
$html .= '<tr>';
foreach($value as $key2 => $value2) {
if ($key2 == 'downloadurl') {
$html .= '<td>Download</td>';
} else {
$html .= '<td>' . $value2 . '</td>';
}
}
$html .= '</tr>';
}
using switch statement
If you decide on this route, it may be a bit easier to manage in the long run as if () elseif () else() can become difficult to read over time.
foreach($value as $key2 => $value2) {
switch($key2) {
case 'downloadurl':
$html .= '<td>Download</td>';
break;
default:
$html .= '<td>' . $value2 . '</td>';
}
}
use this function to generate urls:
function getURL($downloadURL)
{
$html = "<a href = '$downloadURL'>Download</a>";
return $html;
}
And your function build_table():
function build_table($array){
// start table
$html = '<table id = "usertable">';
// header row
$html .= '<tr class = "header">';
foreach($array[0] as $key => $value){
$html .= '<th>' . $key . '</th>';
}
$html .= '</tr>';
// data rows
foreach( $array as $key => $value){
$html .= '<tr>';
foreach($value as $key2 => $value2){
$value2 = ($key2 == 'downloadurl') ? getURL($value2) : $value2;
$html .= '<td>' . $value2 . '</td>';
}
$html .= '</tr>';
}
// finish table and return it
$html .= '</table>';
return $html;
}
I found this handy code in order to print the alphabet horizontally across the page. I would like to put a few spaces in between the letters since they are bunched really close together. Thanks for any help!
$alphabet = range('A', 'Z');
$table = '<table>';
for ($i = 0; $i < count($alphabet); $i++) {
$table .= '<td>' . $alphabet[$i] . '</td>';
}
$table .= '</table>';
echo $table;
If you don't want to use CSS, try adding a cellspacing attribute to your table.
$alphabet = range('A', 'Z');
$table = '<table cellspacing='10'><tr>';
for ($i = 0; $i < count($alphabet); $i++) {
$table .= '<td>' . $alphabet[$i] . '</td>';
}
$table .= '</tr></table>';
echo $table;
This adds 10 pixels between the cells.
You could also do this with CSS like so..
table tr td { margin-left: 5px; margin-right: 5px; }
Also, your code is not currently out putting a valid HTML table. I fixed that too ;)
You can do it across CSS
td { padding-bottom : 20px; }
I wonder how can i find out last in table with PHP, aka if that is last then i want to apply different style and content to it.
This is part of my code generating table content.
$content .= '</tr>';
$totalcols = count ($columns);
if (is_array ($tabledata))
{
foreach ($tabledata as $tablevalues)
{
if ($tablevalues[0] == 'dividingline')
{
$content .= '<tr><td colspan="' . $totalcols . '" style="background-color:#efefef;"><div align="left"><b>' . $tablevalues[1] . '</b></div></td></tr>';
continue;
}
else
{
$content .= '<tr>';
foreach ($tablevalues as $tablevalue)
{
$content .= '<td>' . $tablevalue . '</td>';
}
$content .= '</tr>';
continue;
}
}
}
else
{
$content .= '<tr><td align="center" style="border-bottom: none;" colspan="' . $totalcols . '">No Records Found</td></tr>';
}
I know there is option to do something like that with jQuery later on, but I don't want to complicate it and just solve it with php at time when i generate table.
Keep a count:
$count = 1;
foreach ($tablevalues as $tablevalue)
{
$count++;
if( $count > count( $tablevalues)) {
echo "Last td tag is up next! ";
}
$content .= '<td>' . $tablevalue . '</td>';
}
you can execute any code in last loop:
for($i=0;$i<count($tablevalues);$i++)
{
$tablevalue=$tablevalues[$i];
$content .= '<td>' . $tablevalue . '</td>';
if($i==count($tablevalues)-1){
// last loop execution
}
}
here is a somehow trick
foreach ($tablevalues as $k => $tablevalue)
{
if ($k==count($tablevalues)-1)
{
// last td of current row
$content .= '<td>' . $tablevalue . '</td>';
}
else
{
$content .= '<td>' . $tablevalue . '</td>';
}
}
I just hope that your keys of your $tablevalues set match this code.
You should use a for($i = 0; i < count($tablevalues); $i++) {} loop and consider count($tablevalues)-1 to be the last key of your array.
So that $tablevalues[count($tablevalues)-1] is your last <td>.
Why dont you use jQuery?It has provisions to do something like that.