执行文档后将输出插入页面

发布于 2024-12-27 13:17:47 字数 1370 浏览 1 评论 0原文

在 PHP 中,有一种情况,我需要页面大部分被执行,但有一个项目插入到该页面的输出中。

我认为输出缓冲可能会有一些帮助,但我无法弄清楚如何在我的情况下实现它。

我的代码如下所示:

//this document is part of a global functions file

function pageHeader (){

    //I'm using $GLOBALS here because it works, however I would really rather a better method if possible
    $GLOBALS['error_handler'] = new ErrorHandler(); //ErrorHandler class sets a function for set_error_handler, which gets an array of errors from the executed page

    require_once($_SERVER['DOCUMENT_ROOT'].'/sales/global/_header.php');

    //I would like the unordered list from ->displayErrorNotice() to be displayed here, but if I do that the list is empty because the list was output before the rest of the document was executed
}

function pageFooter (){

    $GLOBALS['error_handler'] ->displayErrorNotice(); //this function displays the errors as an html unordered list

    include($_SERVER['DOCUMENT_ROOT']."/sales/global/_footer.php");
}

网站上的大多数页面都包含此文档并使用 pageHeader()pageFooter() 函数。我想要实现的目标是在包含 _header.php 之后将 PHP 生成的错误的无序列表放入 HTML 列表中。如果我将列表放在页脚中(在执行文档之后),我可以让列表按预期工作,但我不希望它在那里。我想我可以用 JS 来移动它,但我认为必须有一个 PHP 解决方案。

更新

我想知道 ob_start() 的回调函数是否可以通过正则表达式搜索缓冲区来放置错误列表,然后将其插入将是解决方案。

更新2我已经解决了这个问题,我的答案如下。如果允许的话,我会在 2 天内接受。

In PHP have a situation where I need the page to be mostly executed, but have an item inserted into the output from that page.

I think output buffering may be of some help, but I can't work out how to implement it in my situation.

My code looks like this:

//this document is part of a global functions file

function pageHeader (){

    //I'm using $GLOBALS here because it works, however I would really rather a better method if possible
    $GLOBALS['error_handler'] = new ErrorHandler(); //ErrorHandler class sets a function for set_error_handler, which gets an array of errors from the executed page

    require_once($_SERVER['DOCUMENT_ROOT'].'/sales/global/_header.php');

    //I would like the unordered list from ->displayErrorNotice() to be displayed here, but if I do that the list is empty because the list was output before the rest of the document was executed
}

function pageFooter (){

    $GLOBALS['error_handler'] ->displayErrorNotice(); //this function displays the errors as an html unordered list

    include($_SERVER['DOCUMENT_ROOT']."/sales/global/_footer.php");
}

Most pages on the site include this document and use the pageHeader() and pageFooter() functions. What I am trying to achieve is to put an unordered list of the PHP generated errors into an HTML list just at a point after _header.php has been included. I can get the list to work as intended if I put it in the footer (after the document has been executed), but I don't want it there. I guess I could move it with JS, but I think there must be a PHP solution.

UPDATE

I'm wondering whether a callback function for ob_start() which searches the buffer by regex where to put the error list, and then inserts it will be the solution.

UPDATE 2 I have solved the problem, my answer is below. I will accept it in 2 days when I am allowed.

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

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

发布评论

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

评论(2

三生一梦 2025-01-03 13:17:47

终于搞定了关键是缓冲输出,并在缓冲区中搜索给定的 html 片段,并将其替换为无序列表。

我的实现是这样的:

function outputBufferCallback($buffer){

    return str_replace("<insert_errors>", $GLOBALS['error_handler']->returnErrorNotice(), $buffer);
}

function pageHeader (){

    ob_start('outputBufferCallback');
    //I'm using $GLOBALS here because it works, however I would really rather a better method if possible
    $GLOBALS['error_handler'] = new ErrorHandler(); //ErrorHandler class sets a function for set_error_handler, which gets an array of errors from the executed page

    require_once($_SERVER['DOCUMENT_ROOT'].'/sales/global/_header.php');

    echo '<insert_errors>'; //this snippet is replaced by the ul at buffer flush
}

function pageFooter (){

    include($_SERVER['DOCUMENT_ROOT']."/sales/global/_footer.php");
    ob_end_flush();
}

Worked it out finally. The key was to buffer the output, and search the buffer for a given snippet of html, and replace it with the unordered list.

My implementation is like this:

function outputBufferCallback($buffer){

    return str_replace("<insert_errors>", $GLOBALS['error_handler']->returnErrorNotice(), $buffer);
}

function pageHeader (){

    ob_start('outputBufferCallback');
    //I'm using $GLOBALS here because it works, however I would really rather a better method if possible
    $GLOBALS['error_handler'] = new ErrorHandler(); //ErrorHandler class sets a function for set_error_handler, which gets an array of errors from the executed page

    require_once($_SERVER['DOCUMENT_ROOT'].'/sales/global/_header.php');

    echo '<insert_errors>'; //this snippet is replaced by the ul at buffer flush
}

function pageFooter (){

    include($_SERVER['DOCUMENT_ROOT']."/sales/global/_footer.php");
    ob_end_flush();
}
硬不硬你别怂 2025-01-03 13:17:47

如果我做对了,您将尝试在页眉和页脚之间插入一些计算的代码/错误。我猜测错误是在页面的最后汇总/总结的,并将在页脚之后完成。

如果这是真的,我想不出用纯 php 来做到这一点。它只能遍历一个页面一次,并且不能返回。您可以做的是在页脚之后创建一个元素,并使用 javascript 将其移动到您想要显示它的区域。这将是我认为最简单的方法。您可以使用 jquery 轻松完成此操作。

如果我走在正确的轨道上,我可以进一步解释,但我还不能 100% 确定你在问什么......

你将使用的 jquery 命令是 .appendTo()。

If I'm getting this right, you're trying to insert some calculated code/errors between the header and footer. I'm guessing that the errors are being totalled/summed up at the very end of the page and would be completed after the page footer.

If this is true, I can't think of anyway to do this with pure php. It can only run through a page once, and cannot double back. What you can do is create an element after the footer and move it using javascript to the area where you want to display it. This would be the easiest way I would think. You can do this easily with jquery.

I can explain further if I am on the right track, but I'm not 100% sure what you're asking yet...

The jquery command you would use is .appendTo().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文