Grazie del chiarimento.
Ok per JSON
Lo script utilizza file xml quindi il file sarebbeVa bene l'onload, ma l'xml puoi sfruttarlo tramite JS in questo evento oppure non crearlo affatto ed usare json per creare direttamente l'array.
già pronto e utilizza una semplice classe per
il caching che serializza l'array dei vari items.
Questa classe oltre a scrivere ovviamente il
file di caching scrive il feed rss e l'array js
(visto che nella classe recupero l'array dei dati)
(suggerimenti) ad ogni modo hai ragione te
le chiamate al server sarebbero sempre due![]()
Ad ogni modo l'array lo faresti stampare nella pagina
o in un file separato ti posto la classe così mi dici
se ti sembra un procedimento giusto :
Scusate mod
da richiamare nella classe cache in questo modo:Codice PHP:class JsBuilder{
private $jsFile= '';
private $arrayname= '';
private $jsStatement= '';
public function __construct($jsFile,$arrayname='mylinks'){
$this->jsFile= $jsFile;
$this->arrayname= $arrayname;
}
public function write(){
if(!$fp=fopen($this->jsFile,'w')){
throw new FileException('Error opening ['.$this->jsFile.'] file in ['.__CLASS__.']');
}
if(!flock($fp,LOCK_EX)){
throw new FileException('Unable to lock ['.$this->jsFile.'] file in ['.__CLASS__.']');
}
if(!fwrite($fp,$this->jsStatement)){
throw new FileException('Error writing to ['.$this->jsFile.'] file in ['.__CLASS__.']');
}
flock($fp,LOCK_UN);
fclose($fp);
unset($fp);
}
public function prepare($data){
if(!is_array($data)){
throw new InvalidArgException('Error! Expects an array, string given in ['.__CLASS__.']');
}
$this->jsStatement.= 'var '.$this->arrayname."= new Array();\n";
foreach($data as $k => $v){
$this->jsStatement.= $this->arrayname.'['.$k."]= new Array();\n";
foreach($v as $key => $value){
if($key=='id'){
$this->jsStatement.= $this->arrayname.'['.$k.']'."[0]='".$value."';"."\n";
}
if($key=='title'){
$this->jsStatement.= $this->arrayname.'['.$k.']'."[1]='".$value."';"."\n";
}
}
}
}
}
?>
Ti sembra valido come metodo ?Codice PHP:
<?php
class Cache{
private $to= null;
public $rss2Parser= null;
private $jsBuilder= null;
private $expiry= 0; // cache expire time in seconds
private $cacheFile= '';
private $data= array();
public function __construct(Data $to,Rss2Parser $rss2Parser,JsBuilder $jsBuilder,$expiry,$cacheFile){
$this->to= $to;
$this->rss2Parser= $rss2Parser;
$this->jsBuilder= $jsBuilder;
if(!is_int($expiry)||($expiry==0)){
throw new InvalidArgException('Error! Expects an integer, string given in ['.__CLASS__.']');
}
$this->expiry= $expiry;
$this->cacheFile= $cacheFile;
$this->query();
}
private function query(){
if(!$this->isValid()){
/* QUI */
$this->data= $this->to->getData();
$this->rss2Parser->makeRss2($this->data);
$this->jsBuilder->prepare($this->data);
$this->jsBuilder->write();
$this->write();
}
else{
$this->read();
}
}
private function write(){
if(!$fp=fopen($this->cacheFile,'w')){
throw new FileException('Error opening ['.$this->cacheFile.'] file in ['.__CLASS__.']');
}
if(!flock($fp,LOCK_EX)){
throw new FileException('Unable to lock ['.$this->cacheFile.'] file in ['.__CLASS__.']');
}
if(!fwrite($fp,serialize($this->data))){
throw new FileException('Error writing to ['.$this->cacheFile.'] file in ['.__CLASS__.']');
}
flock($fp,LOCK_UN);
fclose($fp);
unset($fp);
}
private function read(){
if(!$this->data=unserialize(file_get_contents($this->cacheFile))){
throw new FileException('Error reading from cache file');
}
}
// determine cache validity based on a time expiry trigger
private function isValid(){
$isValid= false;
if(file_exists($this->cacheFile)&&filemtime($this->cacheFile)>(time()-$this->expiry)){
$isValid= true;
}
return $isValid;
}
public function getData(){
return $this->data;
}
final public function __destruct(){
unset($this->to);
unset($this->expiry);
unset($this->cacheFile);
unset($this->data);
}
}
?>
Con JSON vorrei fare + o - la stessa cosa.
Scusa se approffitto![]()
![]()
PS
Se non ricordo male c'è una tua classe
che trasforma dati php in notazione JSON
ACE no ? o è un'altra cosa ?



Rispondi quotando