2

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 } }?>
ankit ingle
  • 213
  • 1
  • 5
  • 10

3 Answers3

0

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;
        }
    }
}
?>
Marcos Dimitrio
  • 6,651
  • 5
  • 38
  • 62
  • but my table comes from database....i need to know how to count 6th row in my code.... – ankit ingle Jun 08 '15 at 11:03
  • You can do it by tracking how many rows have been displayed. I've updated my answer, check it out. – Marcos Dimitrio Jun 08 '15 at 12:40
  • but sir your code only break 1 page after 6 rows ...but from next page...this display 8 to 10 rows perpage ..... – ankit ingle Jun 08 '15 at 14:52
  • i al;so try this with pagination...but in pagination you need to button to move on to next page...and i dont need to show button......bcoz this is print report..... – ankit ingle Jun 08 '15 at 14:53
  • I'm not sure if I understood your last two comments. My code breaks at every 6 tables printed. The [modulus operator](http://php.net/language.operators.arithmetic) (%) returns the remainder of $lineCounter divided by 6, so when it reaches 6, 12, 18, and so on, the remainder will be zero, so it will print a pageBreak. – Marcos Dimitrio Jun 08 '15 at 15:24
0
<?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++;
 }
?>
-1

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;
            }
        }

    ?>
  • Are you affiliated with the linked website? If so, when linking to your own site or content (or content that you are affiliated with), you [must disclose your affiliation _in the answer_](/help/promotion) in order for it not to be considered spam. Having the same text in your username as the URL or mentioning it in your profile is not considered sufficient disclosure under Stack Exchange policy. – cigien Aug 07 '22 at 02:29
  • The linked page is not in English. – tripleee Jan 05 '23 at 11:09