foreach shows 1 file waiting then clears and shows another php

1

I have foreach in PHP and it shows several data of XML , but in this XML has 5 photos and I would like the loop to show the first photo, wait for 10 seconds and then show second photo so on. Here is the code I have:

<?php 
$link = "rss.xml"; 
$xml = simplexml_load_file($link) -> channel; 
foreach($xml -> item as $item){
echo "<div id='foto'>";
echo "<img id=\"foto\" src=".utf8_decode($item -> linkfoto)."><br /></div>";
break;
}

This way with break; it shows only the first photo, I would like it to have a timer or something like that so that the other photos appear after 5 seconds, deleting the first photo.

    
asked by anonymous 16.07.2015 / 19:27

1 answer

1

The break; as mentioned it prevents the continuation of foreach() .

An alternative is to use sleep() , see the manual . That way it would be:

//...
foreach($xml -> item as $item){
echo "<div id='foto'>";
echo "<img id=\"foto\" src=".utf8_decode($item -> linkfoto)."><br /></div>";
sleep(10);
}

The value entered "inside" the function is the time in seconds that you want it to wait, but this will not delete the previous !

In this case it would run, wait 10 seconds, run again, wait 10 seconds (...) to the end of the loop.

However, PHP is rendered on the server side and for this reason the client does not respond until the content is fully processed, ie there would be a 10-second interval but all images would be displayed.

In order for the client to receive an image every 10 seconds you have some alteratives, the "advantages" and "disadvantages" are relative and I just listed what I remember at the moment:

  • Use sleep with flush() and ob_flush() before it.

      

    Advantage: Does not require any other changes, just add flush(); ob_flush(); before sleep(); and is more secure, guaranteed to always be every 10 seconds. b>
    Disadvantage: High page load time for SEO is bad, plus timeout (maybe) needs to be changed, which is not good, if it has limit of processes in progress simultaneous this solution is the worst. Additionally, this may compromise the loading of other page content. This also does not have the ability to delete the old image, however Javascript is required to delete the previous image every 10 seconds.

  • Use " AJAX " to request a new image every 10 seconds.

      

    Advantage: Reduces the loading time considerably, just having to create a specific page to distribute the loop contents, one by one. Disadvantage: You need to create another 'page' to provide the data and changes on the page you receive. It is less secure if there is no monitoring or verification of time. In this case the user can view the contents before 10 seconds. In addition, other services, external to yours, may use such a connection to collect data, if not limiting, more easily and quickly.

  • Use Javascript to display / show content every 10 seconds using CSS in display: none; .

      

    Advantage: Requires small changes to your code, reduces loading time considerably.

    Disadvantage: Very insecure ! Anyone browsing the page HTML (F12) can get and view the hidden content, obviously without waiting for the 10 seconds.

There are other solutions, this will depend on the application thereof.

    
18.04.2016 / 07:36