Visualizzazione dei risultati da 1 a 3 su 3
  1. #1
    Utente di HTML.it
    Registrato dal
    Dec 2005
    Messaggi
    654

    Interazione tra Javascript e php

    All'interno di una classe php ho estrapolato due array, uno contenente l'indirizzo dell'immagine e un altro il link a cui si riferisce. Con questi parametri devo costruire un banner a rotazione delle immagini (casuale o in sequenza come è nell'array, non ha importanza).

    $link[$i]=$row["link"]; //link per ogni immagine
    $src[$i]=$row["img"]; //path per ogni immagine

    Non sono pratico di javascript, ma con le funzioni ci lavoro e quindi ho estrapolato da un esempio una funzione di JS che potrebbe servire:

    Codice PHP:
    function rotateIt(){
      
    sponsorctr=(sponsorctr<sponsor_nr-1?sponsorctr+1:0);
      
    document.images[ad].src=im[sponsorctr].src;
      if (
    document.getElementById){
       
    document.getElementById('a').innerHTML=msg[sponsorctr];
       }
      
    tmr=setTimeout("rotateIt()",+timerot);

    dove sponsor_nr è il numero totale dei banner e timerot è definito come:
    var timerot = 5000;

    Come faccio a far interagire questa funzione con gli array php?


    NB: metto qui sotto l'intero esempio da cui ho estrapolato questa funzione JS, nel caso servisse per chiarimenti:

    Codice PHP:
    <script type="text/javascript" language="JavaScript">
    <!--
    var 
    bannerwidth 468;
    var 
    bannerheight 60;
    var 
    sponsor_nr=4;
    var 
    folder="banner/";
    var 
    sfx='.gif';
    var 
    timerot 5000;

    var 
    sponsorctr=0;
    var 
    halt=0;
    im=new Array();
    for (
    i=0;i<sponsor_nr;i++){
     
    im[i]=new Image();
     
    im[i].src=folder+i+sfx;
    }
    var 
    dest=new Array();
    dest[0]="http://corsi.html.it";
    dest[1]="http://freephp.html.it";
    dest[2]="http://newsletter.html.it";
    dest[3]="http://corsi.html.it";

    var 
    msg=new Array();
    msg[0]="Messaggio per Corsi.html.it etc etc etc etc...";
    msg[1]="Messaggio per Freephp.html.it etc etc etc etc...";
    msg[2]="Messaggio per Newsletter.html.it etc etc etc etc...";
    msg[3]="Messaggio per Corsi.html.it etc etc etc etc...";

    function 
    rotateIt(){
     if (
    halt!=1){
      
    sponsorctr=(sponsorctr<sponsor_nr-1?sponsorctr+1:0);
      
    document.images[ad].src=im[sponsorctr].src;
      if (
    document.getElementById){
       
    document.getElementById('a').innerHTML=msg[sponsorctr];
       }
      
    tmr=setTimeout("rotateIt()",+timerot);
      }
    }

    function 
    lightIt(){
     
    halt=0;
     
    window.status="Visitati "+dest[sponsorctr];
     if (
    tmr!=""){
      
    clearTimeout(tmr);
      }
     }

    function 
    dimIt(){
     
    halt=0;
     
    window.status="";
     
    rotateIt();
     }

    function 
    dumpIt(){
     
    halt=1;
     
    sponwin=window.open(dest[sponsorctr],'','');
     }
    function 
    wrtIt(){
     
    document.write('<TABLE BORDER="0" WIDTH="'+bannerwidth+'" align="center"><TR><T'
     
    +'D>[img]+folder+[/img]<A HREF=sponsor'
     
    +'.html onMouseover=lightIt() onMouseOut=dimIt() onClick="dumpIt'
     
    +'();return false;">[img]+folder+[/img]
    <DIV ID=a><ILAYER ID=b><LAYER ID=c><IMG SRC='
     
    +folder+'shim.gif WIDTH='+bannerwidth+' HEIGHT='+bannerheight+'></LAYER></ILAYER></DIV></A></'
     
    +'TD></TR></TABLE>');
     }

    function 
    getImgAdd(){
     for (var 
    i=0;i<20;i++){
      if (
    document.images[i].src.indexOf(folder+'shim.gif')>-1){
       
    ad=i+1;
       
    i=20
       
    }
      }
     }

    //-->
    </script> 

    Le funzioni sono così richiamate:

    Codice PHP:
    <div align="center">

    <
    SCRIPT language=JAVASCRIPT>
    <!--
    wrtIt();
    getImgAdd();
    sponsorctr=Math.floor((sponsor_nr*Math.random()));
    rotateIt();
    // -->
    </SCRIPT>

    </div> 

  2. #2
    Come faccio a far interagire questa funzione con gli array php?
    Una soluzione è far scrivere all'array a php questo può
    essere un modo :

    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();
    }
    ?>
    E' uno spunto guarda se ti può essere utile.




    Ps.
    Nella classe c'è roba in + (in questo caso vedi flock)
    è solo un esempio
    Without faith, nothing is possible. With it, nothing is impossible
    http://ilwebdifabio.it

  3. #3
    Utente di HTML.it
    Registrato dal
    Dec 2005
    Messaggi
    654
    Grazie per avermi risposto, erano giorni che ci stavo dietro e ormai mi davo già per disperato

    Lo script che hai postato è un po' complesso, ma ho qualche reminescenza del Java quando l'ho studiato (si parla ancora di tre anni fa).
    Domani al lavoro me lo studio e faccio esperimenti sul campo, ma in generale credo di aver capito che il fulcro è nella funzione "prepare", è quella che devo manipolare un po', il resto riguarda più che altro le eccezioni... vediamo se viene fuori qualcosa di buono.

    Grazie ancora per l'aiuto

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.