在 php 中暂停输出缓冲区

发布于 2024-12-28 16:05:58 字数 878 浏览 0 评论 0原文

这是我的问题。我希望能够仅缓冲表的内容,而不缓冲表头。是否可以暂停 php 中的输出缓冲,以便我可以跳过表标题上的缓冲并在实际内容的开头再次恢复?

<?php ob_start(); ?>
<table>
<tr>
    <th>Account</th>
    <th>Quarter</th>
    <th>Amount</th>
</tr>
    <?php 
    foreach($tc_item as $v){ 


    if($v->dbl_amt != 0){
    ?>
    <tr>
    <!-- Nature of Collection -->
        <td id="nature"><?php echo $v->strDescription; ?></td>
     <!-- Account Code -->     
        <td id="account"><?php echo $v->str_details; ?></td>
     <!-- Amount -->
        <td id="amount"><?php echo number_format($v->dbl_amt,2, '.', ''); ?></td>

    </tr>
    <?php } ?>
    <?php } ?>

</table>
<?php 
$_SESSION['or_details'] = ob_get_contents();
?>

Here's my problem. I want to be able to buffer only the contents of the tables but not the header. Is it possible to pause the output buffering in php so that I could skip the buffering on the table headers and resume again at the beginning of the actual content?

<?php ob_start(); ?>
<table>
<tr>
    <th>Account</th>
    <th>Quarter</th>
    <th>Amount</th>
</tr>
    <?php 
    foreach($tc_item as $v){ 


    if($v->dbl_amt != 0){
    ?>
    <tr>
    <!-- Nature of Collection -->
        <td id="nature"><?php echo $v->strDescription; ?></td>
     <!-- Account Code -->     
        <td id="account"><?php echo $v->str_details; ?></td>
     <!-- Amount -->
        <td id="amount"><?php echo number_format($v->dbl_amt,2, '.', ''); ?></td>

    </tr>
    <?php } ?>
    <?php } ?>

</table>
<?php 
$_SESSION['or_details'] = ob_get_contents();
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自由范儿 2025-01-04 16:05:58

如果你不想缓冲整个表,那么就不要缓冲它:

<table>
  <thead></thead>
  <?php ob_start();?>
  <tbody></tbody>
  <?php $tbody = ob_get_flush(); ?>
</table>

如果你想缓冲整个表,但想要单独缓冲表体,那么添加另一级缓冲:

<?php ob_start();?>
<table>
  <thead></thead>
  <?php ob_start();?>
  <tbody></tbody>
  <?php $tbody = ob_get_flush(); ?>
</table>
<?php $table = ob_get_clean(); ?>

或者,你可以刷新当前缓冲区无需创建新的。我不推荐这样做,因为它会使您的代码更难遵循。这也很愚蠢,因为如果您只是要刷新而不捕获字符串,那么您可能一开始就不缓冲:

<?php ob_start()?>
<table>
  <thead></thead>
  <?php ob_flush();?>
  <tbody></tbody>
  <?php $tbody = ob_get_contents(); // only contains output since last flush ?>
</table>
<?php ob_end_flush(); ?>

If you don't want to buffer the whole table, then don't buffer it:

<table>
  <thead></thead>
  <?php ob_start();?>
  <tbody></tbody>
  <?php $tbody = ob_get_flush(); ?>
</table>

If you want to buffer the whole table, but want the table body separately, then add another level of buffering:

<?php ob_start();?>
<table>
  <thead></thead>
  <?php ob_start();?>
  <tbody></tbody>
  <?php $tbody = ob_get_flush(); ?>
</table>
<?php $table = ob_get_clean(); ?>

Alternatively, you can flush the current buffer without creating a new one. I don't recommend this because it makes your code harder to follow. It's also silly since if you're just going to flush without capturing the string, you might as well not buffer in the first place:

<?php ob_start()?>
<table>
  <thead></thead>
  <?php ob_flush();?>
  <tbody></tbody>
  <?php $tbody = ob_get_contents(); // only contains output since last flush ?>
</table>
<?php ob_end_flush(); ?>
清眉祭 2025-01-04 16:05:58

在 header 之后开始缓冲

<table>
<tr>
    <th>Account</th>
    <th>Quarter</th>
    <th>Amount</th>
</tr>
<?php
ob_start();
echo "table data";
ob_end_flush();

Start buffering after the header

<table>
<tr>
    <th>Account</th>
    <th>Quarter</th>
    <th>Amount</th>
</tr>
<?php
ob_start();
echo "table data";
ob_end_flush();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文