• PHP Help
  • Inserting Page Break after certain number of rows

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>&nbsp;</td>';
}
echo '</tr>';
}
echo '</table>';
}
?>
</body>
</html>

    I've done a little work in this vein. Let me say first I like this idea:

    $nbsp = $cols - ($rows % $cols); // Calculate the number of blank columns

    I don't remember ever doing anything like that. 😮

    I'd encourage you to not be lazy with brackets. Down around here:

        if ($i && $i % 60 == 0)
            echo '</tr><tr class="breakAfter">';
        else if ($i)

    Looks to me kind of like trouble waiting to happen; I'm never quite sure about non-bracket if/else tests, especially with "else if" and ambiguous code sections after them.

    I also wonder about $i. You're using it for control up here:

    if ($i && $i % 60 == 0) {

    Then down here you use it again:

    for ($i = 0; $i < $nbsp; $i++) {

    It seems to me it will never be '60' like that. I'd use 'j' or 'x' in this latter loop and see if that helps.

    Also, welcome to PHPBuilder, and you can put code into your posts by prepending three backticks 🙂

    dalecosp Thank you very much. I did recognize those brackets need to be added but thanks for pointing that out.

      Caveat: I did not look closely at your code. (I'm lazy.)

      Something I've done in the past is to take an array of data and split it into "pages" via array_chunk(). Then you can do a foreach() on the chunked array to get each page, looping on each chunk's array to output the table data.

      $pages = array_chunk($your_array, 20, true); // 20 rows max per page
      foreach($pages as $page) {
        // output page start stuff, then...
        foreach($page as $row) {
          // output table row
        }
        // output end of page stuff
      }
        Write a Reply...