简体   繁体   English

在后台运行PHP脚本时未加载HTML

[英]HTML not loading while PHP script running in background

I am currently creating a stock market simulation and am working on the moment that the user logs into the simulation. 我目前正在创建一个股票市场模拟,并在用户登录模拟的那一刻开始工作。 I have a PHP script that will generate a certain price for a company four times and update it into my MySQL database while running. 我有一个PHP脚本,它将为公司产生四倍的确定价格,并在运行时将其更新到我的MySQL数据库中。 I currently have the following code: 我目前有以下代码:

PHP: PHP:

if (isset($_SESSION['userId']))
{
  $isPlaying = 0;
  while ($isPlaying <= 3)
  {
    $priceTemp = (rand(3300, 3700) / 100);
    $sql = "UPDATE pricestemp SET price = $priceTemp WHERE companyName = 'Bawden';";
    mysqli_query($conn, $sql);
    sleep(1);
    $isPlaying++;
  }
  echo '<h1>Welcome to the simulation</h1>';
}

I am aiming for these updates to happen in the background once the user has logged into the simulation. 我的目标是,一旦用户登录到模拟中,这些更新就会在后台发生。 When refreshing my database every second, the updated prices are shown which is one of my objectives. 每秒刷新数据库时,将显示更新的价格,这是我的目标之一。 However, what I would like it to do is still load the HTML onto the page (to say "Welcome to the simulation") while updating the database with every second with an updated price. 但是,我仍然希望将HTML加载到页面上(说“欢迎使用模拟”),同时以更新的价格每秒更新数据库。

So far, when I log in, I have to wait 4 seconds before the HTML will load. 到目前为止,当我登录时,我必须等待4秒钟才能加载HTML。 In the future, I hope to have it consisently updating until a certain condition is met but when I have set an infinite loop earlier the HTML never loaded. 将来,我希望它能不断更新,直到满足特定条件为止,但是当我较早地设置了无限循环时,HTML永远不会加载。

What do I have to do to allow the HTML to load once logged in and have the prices being generated and updated in the MySQL database in the background with no delay in either of these tasks happening? 我该怎么做才能使HTML登录后即可加载,并在后台在MySQL数据库中生成和更新价格,而不会延迟执行任何一项任务?

You have a fundamental misunderstanding of how web-based requests work. 您对基于Web的请求如何工作有基本的误解。

What you need to understand is that PHP is a server-side language. 您需要了解的是PHP是一种服务器端语言。 PHP generates any combination of HTML, CSS, JavaScript, JSON, or any other forms of data you want and sends it to your web browser when it's finished. PHP生成HTML,CSS,JavaScript,JSON或所需的任何其他形式的数据的任意组合,并在完成后将其发送到Web浏览器。 While it's doing that, it can also manage data within a database or perform any other number of actions, but it will never send what the web browser can make use of until it finishes setting everything up. 在执行此操作的同时,它还可以管理数据库中的数据或执行任何其他数量的操作,但是在完成所有设置之前,它永远不会发送Web浏览器可以利用的内容。 So if you're within an infinite loop, it will never finish and therefore nothing will be sent back to the web browser. 因此,如果您处于无限循环之内,它将永远不会结束,因此不会将任何内容发送回Web浏览器。

To remedy this, you need to use something called " asynchronous JavaScript ", more commonly referred to as " ajax ". 为了解决这个问题,您需要使用“ 异步JavaScript ”(通常称为“ ajax ”)。 Specifically, you first send some initial HTML to the web browser in one request and let the request end immediately. 具体来说,您首先要在一个请求中向Web浏览器发送一些初始HTML,然后立即终止该请求。 This allows the user to see something without waiting around for an indefinite period of time. 这允许用户看到内容而无需无限期地等待。 Then, on the web browser end, you can use JavaScript to automatically send a second request to the server. 然后,在Web浏览器端,您可以使用JavaScript自动将第二个请求发送到服务器。 During this second request to the server, you can perform your data processing and send back some data when you're finished to display to the user. 在对服务器的第二次请求期间,您可以执行数据处理并在完成显示给用户后发送一些数据。

If you want to periodically update what you show the user, then you would repeat that second request to refresh what is shown on the user's webpage. 如果要定期更新显示给用户的内容,则将重复第二个请求以刷新显示在用户网页上的内容。

Any time you see some kind of "real-time" updating on a website, it's not coming from a single, persistently open connection to the web server--it's actually a series of repeated, broken up requests that periodically refresh what you see. 每当您在网站上看到某种“实时”更新时,它就不会来自与Web服务器的单一持久连接,而是一系列重复的,分散的请求,这些请求会定期刷新您所看到的内容。


Broken down, standard web request workflows look something like this: 细分后,标准的Web请求工作流程如下所示:

  1. Web browser asks the web server for the webpage. Web浏览器向Web服务器询问该网页。 Web browser waits for a reply. Web浏览器等待答复。
  2. Web server generates the webpage and sends the webpage to the web browser. Web服务器生成网页并将该网页发送到Web浏览器。 Web server is done. Web服务器已完成。
  3. Web browser receives the webpage and shows it to the user. Web浏览器接收该网页并将其显示给用户。 Web browser stops waiting for a reply. Web浏览器停止等待回复。
  4. Web browser runs any JavaScript it needs to run and requests data from the web server. Web浏览器运行它需要运行的所有JavaScript,并从Web服务器请求数据。 Web browsers waits for a reply. Web浏览器等待答复。
  5. Web server processes the request and sends the requested data back to the web browser. Web服务器处理该请求,并将请求的数据发送回Web浏览器。 Web server is done. Web服务器已完成。
  6. Web browser receives the requested data and updates the HTML on the webpage so the user can see it. Web浏览器接收请求的数据并更新网页上的HTML,以便用户可以看到它。 Web browser stops waiting for a reply. Web浏览器停止等待回复。

As you can see, each series of requests is 1) initiated by the web browser, 2) processed by the web server, and 3) any replies from the web server are then handled by the web browser after the web server is finished up. 如您所见,每个系列的请求都是:1)由Web浏览器发起,2)由Web服务器处理,以及3)Web服务器完成后,然后由Web浏览器处理来自Web服务器的任何答复。 So each and every request goes browser -> server -> browser . 因此,每个请求都通过browser -> server -> browser If we add steps 7. , 8. , and 9. to the above, we will see them repeat the exact same pattern. 如果在上面添加步骤7.8. .和9. ,我们将看到它们重复完全相同的模式。

If you want to avoid adding JavaScript into the mix, preferring to refresh the entire page every time, then keep your data processing short. 如果要避免将JavaScript添加到组合中,而是希望每次都刷新整个页面,则可以缩短数据处理时间。 Optimize your database calls, fix your infrastructure (make sure your server and database have a LAN connection, that your hardware is good enough, etc.), make your code more efficient... do whatever you need to do to keep the processing time to a minimum. 优化数据库调用,修复基础结构(确保服务器和数据库具有LAN连接,硬件足够好,等等),使代码更高效...执行所需的所有操作以保持处理时间最小。


This is all incredibly simplified and not 100% accurate, but should hopefully help you with your specific problem. 所有这些都经过简化,并非100%准确,但是希望可以帮助您解决特定问题。 The short version of all of this is: you can't show your HTML and process your data at the same time the way you're doing things now. 所有这些的简短版本是:您不能以现在的处理方式同时显示HTML 处理数据。 You need to fundamentally change your workflow. 您需要从根本上改变您的工作流程。

You have to do this in 2 network calls. 您必须在2个网络呼叫中执行此操作。 The first network call should fetch the html. 第一个网络调用应获取html。 Then you have to use Javascript to fire another call to update your data. 然后,您必须使用Javascript触发另一个调用来更新您的数据。 Once that api call returns it will update the html. 一旦该api调用返回,它将更新html。

The scheduling model to manage the frequency of a background operation based on the frequency of requests at the front end is a very difficult problem. 基于前端的请求频率来管理后台操作的频率的调度模型是一个非常困难的问题。 It's also a problem you don't need to solve. 这也是您不需要解决的问题。 The data doesn't need to be changed when nobody is looking at it. 当没有人看的时候,不需要更改数据。 You just need to store when the data was last looked at and apply greater deltas to older data. 您只需要存储上次查看数据的时间,并将更大的增量应用于较早的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM