• User Attivo

    Ajax Poller: sondaggio ajax

    Ciao a tutti,
    ho scaricato lo script originale da qui: www dhtmlgoodies com/index.html?whichScript=ajax-poller
    ed avevo intenzione di modificarlo per ottenere una pagina che restituisse i risultati di più sondaggi insieme.

    Soltanto che quando provo a far visualizzare i risultati di 2 sondaggi insieme il primo non riesce ad impostare bene il grafico con le percentuali, mentre il secondo funziona benissimo.

    Questi sono i file che ho modificato:

    ajax-poller.js (qui ho solo duplicato questa funzione):

    function showVoteResults(pollId,ajaxIndex)
    {
        
        document.getElementById('poller_waitMessage' + pollId).style.display='none';
        
        var xml = ajaxObjects[ajaxIndex].response;
        xml = xml.replace(/\n/gi,'');
        
        var reg = new RegExp("^.*?<pollerTitle5>(.*?)<.*$","gi");
        var pollerTitle = xml.replace(reg,'$1');
        
        var resultDiv = document.getElementById('poller_results' + pollId);
        
        var titleP = document.createElement('P');
        titleP.className='result_pollerTitle';
        titleP.innerHTML = pollerTitle;
        resultDiv.appendChild(titleP);    
        
        var options = xml.split(/<option>/gi);
        
        pollVotes[pollId] = new Array();
        totalVotes[pollId] = 0;
        for(var no=1;no<options.length;no++){
            
            var elements = options[no].split(/</gi);
            var currentOptionId = false;
            for(var no2=0;no2<elements.length;no2++){
                if(elements[no2].substring(0,1)!='/'){
                    var key = elements[no2].replace(/^(.*?)>.*$/gi,'$1');
                    var value = elements[no2].replace(/^.*?>(.*)$/gi,'$1');
                
                    if(key.indexOf('optionText5')>=0){
                        var pOption = document.createElement('P');
                        pOption.className='result_pollerOption';
                        pOption.innerHTML = value;
                        resultDiv.appendChild(pOption);                    
                        
                    }
                    
                    if(key.indexOf('optionId5')>=0){
                        currentOptionId = value/1;
                    }
                    
                    if(key.indexOf('votes5')>=0){
                        var voteDiv = document.createElement('DIV');
                        voteDiv.className='result_pollGraph';
                        resultDiv.appendChild(voteDiv);    
                        
                        var leftImage = document.createElement('IMG');
                        leftImage.src = voteLeftImage;
                        voteDiv.appendChild(leftImage);
                        
                        
                        var numberDiv = document.createElement('DIV');
                        numberDiv.style.backgroundImage = 'url(\'' + voteCenterImage + '\')';
                        numberDiv.innerHTML = '0%';
                        numberDiv.id = 'result_voteTxt' + currentOptionId;
                        voteDiv.appendChild(numberDiv);    
                        
                        var rightImage = document.createElement('IMG');
                        rightImage.src = voteRightImage;
                        voteDiv.appendChild(rightImage);                        
                        
                        pollVotes[pollId][currentOptionId] = value;                    
                        totalVotes[pollId] = totalVotes[pollId]/1 + value/1;
                    }
                }
            }
        }
        
        var totalVoteP = document.createElement('P');
        totalVoteP.className = 'result_totalVotes';
        totalVoteP.innerHTML = txt_totalVotes + totalVotes[pollId];
        voteDiv.appendChild(totalVoteP);    
        
        setPercentageVotes(pollId);
        slideVotes(pollId,0);
        
        
        
        
        
        document.getElementById('poller_waitMessage' + pollId).style.display='none';
        
        var xml = ajaxObjects[ajaxIndex].response;
        xml = xml.replace(/\n/gi,'');
        
        var reg = new RegExp("^.*?<pollerTitle6>(.*?)<.*$","gi");
        var pollerTitle = xml.replace(reg,'$1');
        
        var resultDiv = document.getElementById('poller_results' + pollId);
        
        var titleP = document.createElement('P');
        titleP.className='result_pollerTitle';
        titleP.innerHTML = pollerTitle;
        resultDiv.appendChild(titleP);    
        
        var options = xml.split(/<option>/gi);
        
        pollVotes[pollId] = new Array();
        totalVotes[pollId] = 0;
        for(var no=1;no<options.length;no++){
            
            var elements = options[no].split(/</gi);
            var currentOptionId = false;
            for(var no2=0;no2<elements.length;no2++){
                if(elements[no2].substring(0,1)!='/'){
                    var key = elements[no2].replace(/^(.*?)>.*$/gi,'$1');
                    var value = elements[no2].replace(/^.*?>(.*)$/gi,'$1');
                
                    if(key.indexOf('optionText6')>=0){
                        var pOption = document.createElement('P');
                        pOption.className='result_pollerOption';
                        pOption.innerHTML = value;
                        resultDiv.appendChild(pOption);                    
                        
                    }
                    
                    if(key.indexOf('optionId6')>=0){
                        currentOptionId = value/1;
                    }
                    
                    if(key.indexOf('votes6')>=0){
                        var voteDiv = document.createElement('DIV');
                        voteDiv.className='result_pollGraph';
                        resultDiv.appendChild(voteDiv);    
                        
                        var leftImage = document.createElement('IMG');
                        leftImage.src = voteLeftImage;
                        voteDiv.appendChild(leftImage);
                        
                        
                        var numberDiv = document.createElement('DIV');
                        numberDiv.style.backgroundImage = 'url(\'' + voteCenterImage + '\')';
                        numberDiv.innerHTML = '0%';
                        numberDiv.id = 'result_voteTxt' + currentOptionId;
                        voteDiv.appendChild(numberDiv);    
                        
                        var rightImage = document.createElement('IMG');
                        rightImage.src = voteRightImage;
                        voteDiv.appendChild(rightImage);                        
                        
                        pollVotes[pollId][currentOptionId] = value;                    
                        totalVotes[pollId] = totalVotes[pollId]/1 + value/1;
                    }
                }
            }
        }
    ```**ajax-poller-cast-vote-php.php** (qui ho fatto un'altra query al db per estrapolare i dati anche del secondo sondaggio):
    

    $res5 = mysql_query("select ID,pollerTitle from poller where ID='5'");
    if($inf5 = mysql_fetch_array($res5)){
    echo "<pollerTitle5>".$inf5["pollerTitle"]."</pollerTitle5>\n";

        $resOptions5 = mysql_query("select ID,optionText from poller_option where pollerID='".$inf5["ID"]."' order by pollerOrder") or die(mysql_error());
        while($infOptions5 = mysql_fetch_array($resOptions5)){
            echo "<option>\n";
            echo "\t<optionText5>".$infOptions5["optionText"]."</optionText5>\n";                    
            echo "\t<optionId5>".$infOptions5["ID"]."</optionId5>\n";                    
            
            $resVotes5 = mysql_query("select count(ID) from poller_vote where optionID='".$infOptions5["ID"]."'");
            if($infVotes5 = mysql_fetch_array($resVotes5)){
                echo "<votes5>".$infVotes5["count(ID)"]."</votes5>\n";                            
            }                                        
            echo "</option>";                
            
        }                
    }
    
    $res6 = mysql_query("select ID,pollerTitle from poller where ID='6'");
    if($inf6 = mysql_fetch_array($res6)){
        echo "<pollerTitle6>".$inf6["pollerTitle"]."</pollerTitle6>\n";
        
        $resOptions6 = mysql_query("select ID,optionText from poller_option where pollerID='".$inf6["ID"]."' order by pollerOrder") or die(mysql_error());
        while($infOptions6 = mysql_fetch_array($resOptions6)){
            echo "<option>\n";
            echo "\t<optionText6>".$infOptions6["optionText"]."</optionText6>\n";                    
            echo "\t<optionId6>".$infOptions6["ID"]."</optionId6>\n";                    
            
            $resVotes6 = mysql_query("select count(ID) from poller_vote where optionID='".$infOptions6["ID"]."'");
            if($infVotes6 = mysql_fetch_array($resVotes6)){
                echo "<votes6>".$infVotes6["count(ID)"]."</votes6>\n";                            
            }                                        
            echo "</option>";                
            
        }                
    }
    
    Ciao e grazie