In my application I have two pages. Index and engine. Index has mostly jQuery scripts and the basic two of them is an autoload function for the engine.php file that happens every 3 mins, and the other one is a “show more” button that shows 10 more posts when it is clicked.
$feeds = array('','');
$entries = array();
foreach ($feeds as $feed) {
$xml = simplexml_load_file($feed);
$entries = array_merge($entries, $xml->xpath('/rss/channel//item'));
}
What I am looking for is a way to separate the $feeds = array('',''); part that is loaded with the engine.php on every 3 mins or click.
What is your suggestions about that ?
I’m not sure I understand your question, but global variables are defined in php if they’re defined outside all functions. To use them inside, you have to reference it like this before using it:
If what you meant, is that the value for your variable need to be persisted between requests, then I would suggest you use the $_SESSION array (link).
If this does not answer your question let me know.
Edit for updated answer
You’ll have to persist the $feeds array in some way. I think the easiest way would be to use the session, but that won’t make the array available to all users like you mentioned. So I’d suggest you persist the list of feed on a file, that way you can access the same information every time for all users.
Doing that you can create a new script that will updated the file whenever you want (i.e every 10 minutes), and the engine.php will only have to work directly with the file.
Do you need some specific help with code for this proposed solution? Does it help you solve your problem?
Edit2
To save your $feeds array to a file, simply do the following (I guess this part you already know):
And to load that same file, you can do this:
If you want to serialize the $entries array, I would suggest taking a look at serialize & unserialize functions