1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Want to avoid repeating same code in a Polling script...

Discussion in 'PHP' started by gauharjk, Jun 23, 2009.

  1. #1
    Hi,

    I was trying to get Ajax Poller script to work on my site. I found the original script at http://www.dhtmlgoodies.com/index.html?whichScript=ajax-poller

    The script has been uploaded at site www.informationpile.com

    Now, the problem is, whenever a new poll is created, I have to manually edit the PHP file and add code to it.

    Example:


       <?
            $pollerId = 3;    // Id of poller
            ?>
    Code (markup):
    where the Id of Poller has to be manually edited corresponding to the ID of new poll.

    After this, the following code has to be used verbatim.

    
     
            <!-- START OF POLLER -->
            <div class="poller">
            
                <div class="poller_question" id="poller_question<? echo $pollerId; ?>">
                <?            
                
                
                // Retreving poll from database
                $res = mysql_query("select * from poller where ID='$pollerId'");    
                if($inf = mysql_fetch_array($res)){
                    echo "<p class=\"pollerTitle\">".$inf["pollerTitle"]."</p>";    // Output poller title
                    
                    $resOptions = mysql_query("select * from poller_option where pollerID='$pollerId' order by pollerOrder") or die(mysql_error());    // Find poll options, i.e. radio buttons
                    while($infOptions = mysql_fetch_array($resOptions)){
                        if($infOptions["defaultChecked"])$checked=" checked"; else $checked = "";
                        echo "<p class=\"pollerOption\"><input$checked type=\"radio\" value=\"".$infOptions["ID"]."\" name=\"vote[".$inf["ID"]."]\" id=\"pollerOption".$infOptions["ID"]."\"><label for=\"pollerOption".$infOptions["ID"]."\" id=\"optionLabel".$infOptions["ID"]."\">".$infOptions["optionText"]."</label></p>";    
                
                    }
                }            
                ?>            
                <a href="#" onclick="castMyVote(<? echo $pollerId; ?>,document.forms[0])"><img src="images/vote_button.gif"></a>
                </div>
                <div class="poller_waitMessage" id="poller_waitMessage<? echo $pollerId; ?>">
                    Getting poll results. Please wait...
                </div>
                <div class="poller_results" id="poller_results<? echo $pollerId; ?>">
                <!-- This div will be filled from Ajax, so leave it empty --></div>
            </div>
            <!-- END OF POLLER -->
    
    Code (markup):
    Is there any way I could avoid repeating the code and just add a line like:
    <script language="javascript" src="pollscript.js"></script>
    Code (markup):
    ?

    Is there any way to avoid repeating the same piece code to reduce the bandwidth usage?

    I'd appreciate any help. :)
    SEMrush
     
    gauharjk, Jun 23, 2009 IP
    Toopac likes this.
    SEMrush
  2. zulugrid

    zulugrid Well-Known Member

    Messages:
    48
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    108
    #2
    I don't think you'd be able to reduce the bandwidth usage much (if at all). The code that you included is basic PHP code for spitting out the HTML needed to render the poll. No matter what you do, you'll need that HTML for the poll to show up.

    If you only ever want to display the most recent poll, you can change your MySQL queries to something like:
    select * from poller order by id desc limit 1
    Code (markup):
    That would pull the most recent poll, and you'd never have to update your PHP file. You'd also have to update the other queries in that snippet you posted.
     
    zulugrid, Jun 23, 2009 IP
    gauharjk likes this.
  3. www.amagit.com

    www.amagit.com Peon

    Messages:
    87
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Put the code between <!-- START OF POLLER --> and <!-- END OF POLLER --> in a file called pollPHP.php. Then add the following code to wherever you want the specific poll to show up:

    <?php
    $pollerId = 3; // Id of poller
    include("pollPHP.php"); //or the path from where you are to the pollPHP file
    ?>

    Also, PHP code does not translate to bandwidth directly. The output from PHP code does.
     
    www.amagit.com, Jun 23, 2009 IP
    gauharjk likes this.
  4. gauharjk

    gauharjk Well-Known Member

    Messages:
    2,430
    Likes Received:
    135
    Best Answers:
    0
    Trophy Points:
    180
    #4
    Thank you zulugrid for that interesting tip. :)

    And Thank you www.amagit.com for helping me solve the problem. :)

    I appreciate all the help...
     
    gauharjk, Jun 24, 2009 IP