PHP代码在每n次迭代后分页


php code to break page after every nth iteration

我正在打印条形码在页面上…从数据库…

我只需要在一个页面上显示6个条形码优惠券。

之后,我需要打破页.....并继续……直到最后一行

下面是我的代码…但我不明白如何计数和分页后,每第六行。

请帮帮我……

<?php foreach($data as $row) { ?>
<table  border="1px solid #666" summary="" width="48%" class="pos_fixed1">
<thead>
<tr>
<td>Receipt</td>
<td><?php echo htmlspecialchars($row['receipt_no']); ?></td>
<td>Coupon</td>
<td><?php echo htmlspecialchars($row['coupon']); ?></td>
</tr>
</thead>
</table>
<?php } ?>

就像评论中提到的那样,解决这个问题的一个好方法是使用分页,因为这意味着您为用户提供了一种仍然可以看到这些信息的方法。关于这方面的基本教程,我建议你看这里。

或者,如果你真的需要使用你提到的方法,那么下面的东西可能会帮助你:

$count = count($data); //size of your array
$curr_count = 0; //will be used to increment each turn of loop
$show = 6; //how many pieces of data you want to show
foreach($data as $row) {
  $curr_count++;
  if($curr_count <= $show) {
    //show first 6 pieces of data
  } else if ($curr_count == $count) {
    //show last data
  } else {
    //do your page break
  }
}
<STYLE TYPE="text/css">
 P.breakhere {page-break-before: always}
 .tblBRCD{border="1px solid #666" summary="" width="48%"}
</STYLE> 
<table   class="pos_fixed1 tblBRCD">
<?php 
$count==0;
foreach($data as $row)
{
 echo '<tr><td>Receipt</td><td>'. htmlspecialchars($row['receipt_no']) .'</td>
<td>Coupon</td><td>'. htmlspecialchars($row['coupon']) .'</td></tr>';
$count++;
if($count==6){echo '<P CLASS="breakhere">'; $count=0;}
}
?>
</table>

更多参考请参阅本文