1

I have a multiple php xpath codes that takes around 5 to 10 seconds to load.

Here is the code if that will make any difference:

<?php

$html_string = file_get_contents('https://website.com/');
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($html_string);
libxml_clear_errors();
$xpath = new DOMXpath($dom);
$values = array();
$row = $xpath->query('//*[@id="wsod_forecasts"]/div[2]/p/strong');
foreach($row as $value) {
   print($value->nodeValue);
}

?>

My question is, is it possible to create a simple loading image or a text, while the code is loading?

Like this:

<image loading script>
<?php ?>
</image loading script end>

Any ideas? thanks.

1
  • 1
    With JavaScript you can show and hide elements. Could you tell more? PHP lives on the server and doesn't really concern itself with the client. How are you loading that PHP script and where should the loader appear? Commented Feb 13, 2021 at 14:33

2 Answers 2

2

is it possible to create a simple loading image or a text, while the code is loading?

Yes, but not in the way you're thinking. Consider that the PHP code runs on the server, to completion, before the page is rendered on the client. One request, PHP code runs, then one response, then the page renders.

So you want to break up your logic into two requests. So the order of operations would be something like:

  1. User requests the page.
  2. Minimal (even no) PHP code returns a page immediately.
  3. Client-side (JavaScript) code displays a "loading" indication of some kind. (Or it's displayed by default, doesn't matter.)
  4. Client-side code makes an AJAX request to a different PHP file.
  5. That PHP file performs the logic you have now, which takes a few moments, and returns data in JSON format.
  6. Client-side code receives the AJAX response, updates the page content, and removes the "loading" indicator.

This is a very common approach, and there are many tutorials to get you started with AJAX calling PHP. But the main point here is to separate your "page loading" logic from your "content loading" logic into two different requests, providing an overall better user experience.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, that makes sense.
0

The short answer is no, not with PHP.

The reason for this is PHP is a server-side language. When a client (user) makes a request to the webpage, the server receives the request, which is then routed to the appropriate place (in this case, the PHP file). PHP then builds the html to send back to the client so the browser can render it.

While all of this was happening, the client must wait, there is no way to know the progress of what's happened on the server.

The best way to get a loading icon while waiting would be to return a simple page that does not have much information. This page should contain the loading icon as well as JavaScript to make another request to the server. This would be an AJAX request that the javascript would need to interpret to render the data returned.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.