Registreer FAQ Ledenlijst Berichten van vandaag


Ga terug   Scholieren.com forum / Technologie / Software & Hardware
Reageren
 
Topictools Zoek in deze topic
Oud 05-01-2003, 16:54
Dr_KlawChi
Dr_KlawChi is offline
Ik wil graag een eigen poll maken in PHP.
Ik heb hiervoor het volgende script gebruikt:

PHP-code:
<?

////////////////////////////////////////////////////////////
//
// poll.php - a voting poll
//
////////////////////////////////////////////////////////////
//
// This script adds votes to a poll and displays the poll.
//
// See readme.txt for more information.
//
// 
// Last Modified: 5/01/03
//
// You may freely use, modify, and distribute this script.
// You may remove this notice.
//
////////////////////////////////////////////////////////////

// DEFINE THE VARIABLES //
// title of this poll
$title = "poll1?";
// closing date for this poll in MM/DD/YYYY format
$closingDate = "1/1/2100";
// text file that stores vote choices and totals
$pollFile = "poll.txt";
// text file that stores IP addresses
$ipFile = "ips.txt";
// full path to your CSS style sheet
$styleSheet = "http://yoursite/style.css"; // leave blank if you aren't using this

// DO NOT EDIT BELOW THIS POINT UNLESS YOU KNOW PHP! //

// DEFINE THE FUNCTIONS //
// check if the poll has closed
function is_closed() {
    global $closingDate;

    // split the closing date into month, day, and year
    $closingDate = explode("/", $closingDate);

    // get today's today to test against the closing date
    $today = getdate();

    $message = date("l, F j", mktime(0,0,0,$closingDate[0],$closingDate[1],$today[year]));

    // if today's year is greater than the closing year, return true
    if ($today[year] > $closingDate[2]) {
        return $message;
    }
    // if today's year is equal to the closing year
    elseif ($today[year] == $closingDate[2]) {
        // if today's month is greater than the closing month, return true
        if ($today[mon] > $closingDate[0]) {
            return $message;
        }
        // if today's month is equal to the closing month
        elseif ($today[mon] == $closingDate[0]) {
            // if today is greater than or equal to the closing day, return true
            if ($today[mday] >= $closingDate[1]) {
                return $message;
            }
            // if the poll is still open, return false
            else {
                return false;
            }
        }
        // if the poll is still open, return false
        else {
            return false;
        }
    }
    // if the poll is still open, return false
    else {
        return false;
    }
}

// check if the user has already voted
function has_voted() {
    global $ipFile;
    global $REMOTE_ADDR;

    // open the IP address file
    $ips = fopen($ipFile, "r");

    // compare each entry with the user's IP address
    while (!feof($ips)) {
        $ip = fgets($ips, 20);

        if ($ip == $REMOTE_ADDR . "\r\n") {
            $match = 1;
            break;
        }
    }

    // close the IP address file
    fclose($ips);

    if (!$match) {
        // reopen the IP address file
        $ips = fopen($ipFile, "a");

        // add the user's IP address
        fputs($ips, $REMOTE_ADDR . "\r\n");

        // close the IP address file
        fclose($ips);

        return false;
    }
    else {
        return true;
    }
}

// add the user's vote
function addVote($vote) {
    global $pollFile;

    // get the current votes
    $fp_read = fopen($pollFile, "r");
    $currentVote = fread($fp_read, filesize($pollFile));
    fclose($fp_read);

    // create an array with even numbers containing vote choices
    // and odds containing vote totals
    $votes = split('[|:]', $currentVote);

    // update the vote
    for ($i = 1; $i < count($votes); $i = $i + 2) {
        // get the array index number for the name of this vote
        $name = $i - 1;

        // if this vote choice is this user's selection, increment it
        if ($votes[$name] == $vote) {
            $votes[$i]++;
        }

        // if this vote IS the last choice
        if ($i == (count($votes) - 1)) {
            $updatedVote .= $votes[$name] . ":" . $votes[$i];
        }

        // if this vote is NOT the last choice
        else {
            $updatedVote .= $votes[$name] . ":" . $votes[$i] . "|";
        }
    }

    // save the updated vote
    $fp_write = fopen($pollFile, "w");
    fputs($fp_write, $updatedVote);
    fclose($fp_write);
}

// display the poll
function displayPoll($message) {
    global $title, $pollFile;

    // get the current votes
    $fp_read = fopen($pollFile, "r");
    $currentVote = fread($fp_read, filesize($pollFile));
    fclose($fp_read);

    // create an array with even numbers containing vote choices
    // and odds containing vote totals
    $votes = split('[|:]', $currentVote);

    // begin printing the poll table
    echo "<html>\n";
    echo "<head>\n";
    echo "<title>$title</title>\n";
    if ($styleSheet != "" && $styleSheet != "http://yoursite/style.css") {
        echo "<link rel=stylesheet type=text/css href=\"$styleSheet\">\n";
    }
    echo "</head>\n\n";
    echo "<body>\n";
    echo "<basefont face=Arial>\n\n";

    // if a message was sent, print it
    if (isset($message)) {
        echo "<p align=center>$message</p>\n\n";
    }

    echo "<table align=center>\n";
    echo "<caption align=top><b>$title</b></caption>\n";

    // print the poll table rows
    // including vote choice, vote total, and percentage of total votes
    for ($i = 1; $i < count($votes); $i = $i + 2)
    {
        // add together each vote total to find the total number of votes cast
        $totalVotes += $votes[$i];
    }

    for ($i = 1; $i < count($votes); $i = $i + 2) {
        // get the array index number for the name of this vote
        $name = $i - 1;

        // calculate the percentage of total votes for this vote
        // rounded to 1 decimal place
        $percentage = $votes[$i] / $totalVotes * 100;
        $percentage = round($percentage, 1);

        echo "<tr>\n";
        echo "\t<td>$votes[$name]</td>\n";
        echo "\t<td>$votes[$i] votes</td>\n";

        // if the percentage is 0, don't print a bar
        if ($percentage == 0) {
            echo "\t<td>$percentage%</td>\n";
        }

        // otherwise, print the bar
        else {
            echo "\t<td><img src=poll.jpg width=$percentage height=15> $percentage%</td>\n";
        }

        echo "</tr>\n";
    }

    // print the total number of votes cast
    echo "<caption align=bottom>Total Votes: $totalVotes</caption>\n";

    // finish printing the poll table
    echo "</table>\n";
    echo "\n</body>\n";
    echo "</html>";
}


// PROGRAM CODE //
// if the poll is closed, display the poll and exit
if ($message = is_closed()) {
    displayPoll("The poll closed on " . $message . ".");
    exit;
}

// if the user is not voting, display the poll and exit
if (!isset($vote)) {
    displayPoll("");
    exit;
}

// if the user has already voted, display the poll and exit
if (has_voted()) {
    displayPoll("You have already voted.");
    exit;
}

// add the user's vote
addVote($vote);

// display the poll
displayPoll("");

?>
Bovenstaande script had standaard al deze vragen:
What is your favorite programming language?
- HTML
- JavaScript
- Perl
- PHP


Deze vragen zijn in het volgende HTML bestand te vinden:

<html>
<head>
<title>What is your favorite programming language?</title>
</head>

<body>
<basefont face=Arial>

<form action=poll.php method=post>
What is your favorite programming language?<br>
<input type=radio name=vote value=HTML> HTML<br>
<input type=radio name=vote value=JavaScript> JavaScript<br>
<input type=radio name=vote value=Perl> Perl<br>
<input type=radio name=vote value=PHP> PHP<br>
<input type=submit value=Vote>
</form>

</body>
</html>

Ik wil nu graag een paar x-tra keuzes toevoegen aan bovenstaande poll, hoe doe ik dat
Met citaat reageren
Advertentie
Oud 05-01-2003, 17:11
Martin
Avatar van Martin
Martin is offline
Ik mis het bestand poll.txt......
Met citaat reageren
Oud 05-01-2003, 17:35
Dr_KlawChi
Dr_KlawChi is offline
Citaat:
chatfreak schreef:
Ik mis het bestand poll.txt......
Hey je hebt helemaal gelijk! Dat was het probleem dus...het werkt nu Thanks
Met citaat reageren
Oud 05-01-2003, 17:39
Lunchbox
Lunchbox is offline
Hehe, hoe kan je dat nou vergeten
__________________
:: } Visit my New Site, www.Lunchtrommel.COM { ::
Met citaat reageren
Oud 05-01-2003, 17:40
Dr_KlawChi
Dr_KlawChi is offline
Citaat:
Lunchbox schreef:
Hehe, hoe kan je dat nou vergeten
kben een n00b
Met citaat reageren
Oud 05-01-2003, 17:54
Lunchbox
Lunchbox is offline
Citaat:
Dr_KlawChi schreef:
kben een n00b
Niet zo erg als ik gegege
__________________
:: } Visit my New Site, www.Lunchtrommel.COM { ::
Met citaat reageren
Advertentie
Reageren


Regels voor berichten
Je mag geen nieuwe topics starten
Je mag niet reageren op berichten
Je mag geen bijlagen versturen
Je mag niet je berichten bewerken

BB code is Aan
Smileys zijn Aan
[IMG]-code is Aan
HTML-code is Uit

Spring naar

Soortgelijke topics
Forum Topic Reacties Laatste bericht
Vrije tijd poll:speler van het seizoen???
Krugdinho
23 14-05-2002 18:05
Vrije tijd Poll:Welke voetballer gaat het Maken op het Wk
Krugdinho
16 22-04-2002 21:13
De Kantine [POLL] Wat vinden jullie het gaafste forum??
Verwijderd
23 20-04-2002 16:59
Drugs & Alcohol * Poll * Wat is je favoriete soort van drugs ?
apekop123
23 09-04-2002 19:32
Vrije tijd POLL : Beste speler in België
- Praga Khan -
34 21-01-2002 13:23
Software & Hardware [poll] Hoe snel is jou processor.
The twe@ker
32 06-12-2001 11:05


Alle tijden zijn GMT +1. Het is nu 00:48.