Count new entries in an updated XML file?
Using xslt and multiple sources I create a XML file with 100 posts. After this I echo the title of them sorted by date (the latest is the first). I show only 100 titles in the page. When a source post a new post, then it goes first on my list and the last (100 leaves the list).Each post I show has a class like p+number
, e.g p1 for the first, p2 for the 2nd.
this is the main part of news.php
$feeds=array('big-file.xml');
$entries = array();
$xml = simplexml_load_file($feed);
$entries = array_merge($entries, $xml->xpath('//item') );
foreach ($entries as $post) {
if(++$i > 100) break;
echo $post->title;
}
I set a cron job that updates that big-file.xml
every 2 mins. Also, in the index.php I have a jQuery script that loads every 3 mins the news.php to show updated news at my visitors.
Notin开发者_开发问答g is saved in a database or somewhere. There are only 100 posts shown.
My question is if there is a way to know how many new posts appeared in my list when the news.php
was updated?
You can set a timestamp when you got the entire list. Then ask for new elements after that timestamp. At server side you take news in the xml recent to older until the news date is before the timestamp. You should update your timestamp at client side. LOOP
You can make this php file write in a text file a row so every $file is written in a row, and make the script read how many rows are in there by fread function, and every 3 minutes it will erase all the content in this text file. hope i helped :)
精彩评论