I have a page that displays employee names in a responsive table. I will need to produce a print out of the data and I only want to print 60 names per page. I need to insert a page break (using css @meda print page-break-after: always). How can I count the records then insert the page break after 60 records so I only see 60 records per page after the print preview. No errors. PHP data displays as expected but no page breaks after a specified number for records.
Here is the code I have so far:
.........
<html>
<head>
<style type="text/css" media="print">
.breakAfter {
page-break-after: always
}
</style>
</head>
<?php
//database connection
$db = app('db');
$sql = $db->prepare('SELECT * FROM employees');
$sql->execute();
$rows = $sql->rowCount(); // Find total rows returned by database
if($rows > 0) {
if ( in_array($rows, range(1,20)) ) {
$cols = 1; // Define number of columns
}
elseif( in_array($rows, range(21,40)) ) {
$cols = 2; // Define number of columns
}
else {
$cols = 3; // Define number of columns
}
$counter = 1; // Counter used to identify if we need to start or end a row
$nbsp = $cols - ($rows % $cols); // Calculate the number of blank columns
echo '<table width="100%" align="center" cellpadding="4" cellspacing="1">';
$i=0;
while ($row = $sql->fetch(PDO::FETCH_ASSOC)) {
// count records for page break
if ($i && $i % 60 == 0)
echo '</tr><tr class="breakAfter">';
else if ($i)
// counter for columns
if(($counter % $cols) == 1) { // Check if it's new row
echo '<tr>';
++$i; //increase counter for page break
}
echo '<td align="center">'.$row['donor_fname']. ' ' .$row['donor_lname'].'</td>'; // cell with content
if(($counter % $cols) == 0) { // If it's last column in each row then counter remainder will be zero
echo '</tr>';
}
$counter++; // Increase the counter
}
if($nbsp > 0) { // Add unused column in last row
for ($i = 0; $i < $nbsp; $i++) {
echo '<td> </td>';
}
echo '</tr>';
}
echo '</table>';
}
?>
</body>
</html>