Una soluzione è far scrivere all'array a php questo puòCome faccio a far interagire questa funzione con gli array php?
essere un modo :
E' uno spunto guarda se ti può essere utile.Codice PHP:<?php
class FileException extends Exception{
public function __construct($message, $code = 0){
parent::__construct($message, $code);
}
public function __toString(){
return __CLASS__ . " Line : [{$this->line}] Code : [{$this->code}] Msg : {$this->message}\n";
}
}
class JsBuilder{
private $jsFile= '';
private $arrayname= '';
private $jsStatement= '';
public function __construct($jsFile,$arrayname='myImages'){
$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=='_link'){
$this->jsStatement.= $this->arrayname.'['.$k.']'."[0]='".$value."';"."\n";
}
if($key=='_src'){
$this->jsStatement.= $this->arrayname.'['.$k.']'."[1]='".$value."';"."\n";
}
}
}
}
}
try{
$js= new JsBuilder('test.js');
$js->prepare(array(array('_link'=>'miolink1','_src'=>'miosrc1'),array('_link'=>'miolink2','_src'=>'miosrc2')));
$js->write();
}
catch(FileException $e){
echo $e;
exit();
}
?>
Ps.
Nella classe c'è roba in + (in questo caso vedi flock)
è solo un esempio



Rispondi quotando