'php code to break page after 6th row

I try to display 2 table side by side with php code....

but i need to display only 6 table on single page ......and rest on another page......

so plz can any one help me to break page or break loop after 6th iteration..... 7th table display on another page like wise...

plz see my image attached below....i am facing problem on print preview...

page breaks my table during print...like below image...

I attached croped imaged here...

my page actuaally display 8 tables on single page...but i need is only 6 on one page.

enter image description here

below is my code..

<?php if (is_array($data)) {  foreach($data as $row)    {   ?>
<table  border="1px solid #666" summary="" width="48%" class="pos_fixed1">
<thead>
<tr>
<td colspan="4">Dainik Bhaskar Nagpur</td>
</tr>

<tr>
<td>Receipt</td>
<td><?php echo htmlspecialchars($row['receipt_no']); ?></td>

<td>Coupon</td>
<td><?php echo htmlspecialchars($row['coupon']); ?></td>
</tr>

<tr>
<td>Receipt Date</td>
<td><?php echo htmlspecialchars($row['bookingdate']); ?></td>
<td>Coupon Date</td>
<td><?php echo htmlspecialchars($row['coupondate']); ?></td>
</tr>

<tr>
<td>Copy Start Date</td>
<td><?php echo htmlspecialchars($row['startingdate']); ?></td>
<td>HawkerName</td>
<td><?php echo htmlspecialchars($row['hawkername']); ?></td>
</tr>

<tr>
<td>SubagentName</td>
<td><?php echo htmlspecialchars($row['subagentname']); ?></td>
<td>CenterName</td>
<td><?php echo htmlspecialchars($row['ward']); ?></td>
</tr>

<tr>
<td>customer</td>
<td><?php echo htmlspecialchars($row['customer_name']); ?></td>
<td>Address</td>
<td><?php echo htmlspecialchars($row['society']); ?></td>
</tr>
</thead>
</table>

<?php } }?>


Solution 1:[1]

Try using CSS:

<style>
@media print {
    .pageBreak {
        page-break-after: always;
    }
}
</style>

And at every 6 table, add a pageBreak:

<?php
$lineCounter = 0;
if (is_array($data)) {
    foreach($data as $row) {
        $lineCounter++;
?>

<!-- output a table... -->

<?php
        if($lineCounter % 6 == 0) {
            echo '<span class="pageBreak"></span>' . PHP_EOL;
        }
    }
}
?>

Solution 2:[2]

<?php
// get total records
TotalNoOfRecords = count($data);
$Count = 1;
foreach($data as $row)    {
// write here your content

// break page after 6 rows
if($Count % 6 == 0 && $Count != $TotalNoOfRecords)
{
   echo "<p style='page-break-after:always'></p>";
}

$Count++;
 }
?>

Solution 3:[3]

try this is code or visit link

    <?php 
    $q = "SELECT * FROM your_table ";
    $myq = mysqli_query($link, $q);
    $fixtures ='';
    $i=0;
        while($row=mysqli_fetch_assoc($myq)) {
            $r[]=$row;
        }

        foreach ($r as $val) {
            $i++;
    ?>
    <!-- your value from database -->
    <table>
        <tr>
            <td><?php echo $val['your_column']; ?></td>
        </tr>
    </table>
    <!-- your value from database -->
    <?php
            if($i % 6==0){
                echo '<div style="page-break-after: always;">[------ break ------]</div>' . PHP_EOL;
            $i=0;
            }
        }

    ?>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 Enda Tamsing