PHP:在服务之前重新解析整个页面?

发布于 2024-07-14 15:08:47 字数 436 浏览 8 评论 0原文

在页面结束时,如果发生了某些情况,需要将其清除,然后在向客户端提供服务之前需要重新解析整个页面。 我本来打算回显 JavaScript 来刷新页面,但这会让他们加载页面,然后重新加载它...我想知道是否有一种方法可以告诉 php 引擎返回到开头并重新加载- 解析整个页面?

谢谢!

我会尝试更清楚地解释这个问题,但它很复杂,而且我的沟通能力很差。 在列出产品的页面上,我为用户提供了选择字段以缩小结果范围的选项。 系统会记住这一点,因此他们不必继续选择它们。 如果他们缩小金属颜色等类别,然后转到与金属颜色无关的类别(如水晶雕像),则不会显示任何结果,因为没有一个与所选的金属颜色匹配。 生成从数据库中提取产品的查询非常复杂,因为不同的类别对于查找正确的产品有不同的要求。 因此,一旦生成查询,我想针对 mysql_num_rows() 对其进行测试,如果没有结果,则清除用户的选择并重新开始。

At the end of a page, if something occurs, it needs to be cleared, then the entire page needs to be re-parsed before serving to the client. I was going to echo out a javascript to refresh the page, but that will make them load the page and then reload it...I was wondering if there was a way to just tell the php engine to go back to the beginning and re-parse the entire page?

Thanks!

I will try to explain the problem more clearly but it is complicated and I am a terrible communicator. I on the page that lists products I am giving users the option to select fields to narrow the results. The system remembers this so they don't have to keep selected them. If they narrow a category like metal color and then go to a category that metal color is irrelevant like crystal figurines it will not show any results because none will match the metal color chosen. To generate the query to pull the products from the data-base is very complicated because different categories have different requirements to find the correct products. so once the query is generated I want to test it against mysql_num_rows() and if there is no results clear out the users choices and start over.

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

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

发布评论

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

评论(6

分开我的手 2024-07-21 15:08:47

你有点含糊,但如果你只是谈论重新解析输出,你可以使用 output 来做到这一点缓冲

You're being a little vague, but if you're merely talking about reparsing the output, you could do that using output buffering.

想你只要分分秒秒 2024-07-21 15:08:47

我不完全清楚问题是什么,但是您不能在创建 HTML 之前决定要显示的内容,然后第一次发送正确的内容吗?

I'm not entirely clear what the issue is, but couldn't you decide what is to be shown before creating the HTML, and then send the right thing the first time?

记忆之渊 2024-07-21 15:08:47

生成查询以从数据库中提取产品非常复杂,因为不同的类别对于查找正确的产品有不同的要求。 因此,一旦生成查询,我想针对 mysql_num_rows() 对其进行测试,如果没有结果,则清除用户的选择并重新开始。

在这种情况下,只需将查询放入返回结果的函数中,检查行数,如果为零,则清除过滤器并再次调用该函数。

To generate the query to pull the products from the data-base is very complicated because different categories have different requirements to find the correct products. so once the query is generated I want to test it against mysql_num_rows() and if there is no results clear out the users choices and start over.

In that case, just put the query inside a function that returns the result, check the row count, and if it's zero clear the filters and call that function a second time.

゛清羽墨安 2024-07-21 15:08:47

输出缓冲(ob_start 和 ob_clean),与将手头的功能分离到一个单独的文件中以及 eval() 相结合,应该可以解决问题。

哦,最近的 PHP 版本实际上有一个 goto 语句...尽管我总是否认提及任何有关它的事情。 :-)

Output buffering (ob_start and ob_clean), combined with separating the functionality at hand into a separate file and eval()'ing that should do the trick.

Oh, and recent PHP versions actually have a goto statement... although I'll always deny mentioning anything about it. :-)

弥枳 2024-07-21 15:08:47

我觉得你的想法有点偏离了。

重新解析页面应该做的是再次将用户重定向到该页面,

header('Location: thepagefile.php');

但是如果您实际上想重新解析文件而不创建新页面,您也可以再次包含该文件:

include thepagefile.php

但是您可能会得到相同的结果。 如果您想实际解析页面的输出,您可以执行以下操作:

ob_start(); // this is at the very top of the code/page
// all the code goes here
$output = ob_get_clean();
eval($output); // WTF?

但这实际上没有意义,但我希望它有所帮助。

我实际上想知道您想要解决的真正问题是什么。

I think you're going about it a little bit off.

What you should do to reparse the page is to redirect the user to the page again, using

header('Location: thepagefile.php');

however if you actually would like to reparse the file without creating a new page, you could also just include the file again:

include thepagefile.php

But you'd probably get the same result. If you want to actually parse the output of the page you'd do something like:

ob_start(); // this is at the very top of the code/page
// all the code goes here
$output = ob_get_clean();
eval($output); // WTF?

but that actually makes no sense, but I hope it helps.

I'd actually like to know what the real problem you're trying to solve really is.

半枫 2024-07-21 15:08:47

我想你正在寻找这样的东西:

<?php
ob_start(); //we start output buffering, this means nothing is send to the browser
//We do some code stuff
$time = microtime();
echo "Hai \n"; //Note taht mixing logic and output in real life
echo $time;  // is terribly practice
echo "\n bai"; //I do it here purely for the example
if(/*some condition */){
 $anErrorHappened = true;
}
if($anEroorHappened === true){
  //Load the output in a var if you need it
  //Otherwise don't
  $output = ob_get_clean();
  //Do other code stuff
  //I.E. send an error page
  include('errorPage.html');
}
else{
 ob_end_flush(); //Send everything the script has echo()'d, print()'ed and send to the browser in any other way (I.E. readfile(), header() etc.)
}
?>

I think your looking for something like this:

<?php
ob_start(); //we start output buffering, this means nothing is send to the browser
//We do some code stuff
$time = microtime();
echo "Hai \n"; //Note taht mixing logic and output in real life
echo $time;  // is terribly practice
echo "\n bai"; //I do it here purely for the example
if(/*some condition */){
 $anErrorHappened = true;
}
if($anEroorHappened === true){
  //Load the output in a var if you need it
  //Otherwise don't
  $output = ob_get_clean();
  //Do other code stuff
  //I.E. send an error page
  include('errorPage.html');
}
else{
 ob_end_flush(); //Send everything the script has echo()'d, print()'ed and send to the browser in any other way (I.E. readfile(), header() etc.)
}
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文