如何将轮询数据写入文本文件?

时间:2014-02-12 18:26:39

标签: php

我试图在php中进行民意调查。我试图通过将信息写入txt文件来收集数据。如何获取将数据写入txt文件的代码?

  1. 这是我处理程序中的所有代码,如何将其写入我的txt文件。底部的大多数东西都没关系。试着看一下代码if($ submit =='submit')及其后的内容。

    <!DOCTYPE html>
    <html>
     <head>
       <meta charset="utf-8"/>
       <title>Poll</title>
     </head>
     <body>
     <?php
        //no need for sport validation is unimportant and doesnt work
        if (isset($_REQUEST['Soda'])) {
            $Soda = $_REQUEST['Soda'];
        } else {
            $Soda = NULL;
            echo '<p class="error">You forgot to select your favorite soda!</p>';
        }
        //This is end of soda validation
        if (!empty($_REQUEST['Book'])) {
            $Book = $_REQUEST['Book'];
        } else {
            $Book = NULL;
            echo '<p class="error">You forgot to write in your favorite book!</p>';
        }
        //End of book validation
        if (isset($_REQUEST['SOTU'])) {
            $SOTU = $_REQUEST['SOTU'];
        } else { 
            $SOTU = NULL;
            echo '<p class="error">You forgot to select the two biggest issues of the                 state of the union address!</p>';
        }
        //End of SOTU validation
        if (isset($_REQUEST['Soda']) && !empty($_REQUEST['Book']) &&                               isset($_REQUEST['SOTU'])) {
            echo' Thank You for filling out the survey!<br> You can see the results of the pole' . "<a href=\"poll_results.php\"> here</a>!<br><br> Your response has been recorded.";
        } else {
            echo '<p class="error">Please go ' . "<a href=\"poll_form.html\">back</a>" . ' and fill out the poll!<p>';
        }
        //End of link responses
        //Define variables and make sure file works
        $submit = $_REQUEST['submit'];
        $filename = 'poll_data.txt';
        $handle = fopen($filename, 'a');
        //next is the stuff that is to be appended
            if ($submit == 'Submit') {
                fopen($filename, 'w');
                $newdata = $Soda . PHP_EOL;
                fwrite($handle, $newdata);
            } else { echo 'You didn\'t click submit';}
        //Now to sort the data and present it
        /*explode('PHP.EOL', $filename);
        $CC = 0;
        $P = 0;
        $MD = 0;
        $SS = 0;
        $BR = 0;
        $DLS = 0;
        $O = 0;
        foreach($filename as $value) {
            if ($value = 'Coca-Cola') {
                $CC = $CC + 1;
            }
            elseif ($value = 'Pepsi') {
                $P = $P + 1;
            }
            elseif ($value = 'MtnDew') {
                $MD = $MD + 1;
            }
            elseif ($value ='Sprite/Sierra-Mist') {
                $SS = $SS + 1;
            }
            elseif ('BigRed') {
                $BR = $BR + 1;
            }
            elseif ('DontLikeSoda') {
                $DLS = $DLS + 1;
            }
            elseif ('Other') {
                $O = $O + 1;
            }
        }*/
    ?>
    

     

2 个答案:

答案 0 :(得分:0)

我想如果你使用

fopen($filename, 'r'); 

应该有用。

http://www.php.net/manual/en/function.fopen.php

答案 1 :(得分:0)

这应该有效:

<!DOCTYPE html>
<html>
 <head>
   <meta charset="utf-8"/>
   <title>Poll</title>
 </head>
 <body>
 <?php
    //no need for sport validation is unimportant and doesnt work
    if (isset($_REQUEST['Soda'])) {
        $Soda = $_REQUEST['Soda'];
    } else {
        $Soda = NULL;
        echo '<p class="error">You forgot to select your favorite soda!</p>';
    }
    //This is end of soda validation
    if (!empty($_REQUEST['Book'])) {
        $Book = $_REQUEST['Book'];
    } else {
        $Book = NULL;
        echo '<p class="error">You forgot to write in your favorite book!</p>';
    }
    //End of book validation
    if (isset($_REQUEST['SOTU'])) {
        $SOTU = $_REQUEST['SOTU'];
    } else { 
        $SOTU = NULL;
        echo '<p class="error">You forgot to select the two biggest issues of the                 state of the union address!</p>';
    }
    //End of SOTU validation
    if (isset($_REQUEST['Soda']) && !empty($_REQUEST['Book']) &&                               isset($_REQUEST['SOTU'])) {
        echo' Thank You for filling out the survey!<br> You can see the results of the pole' . "<a href=\"poll_results.php\"> here</a>!<br><br> Your response has been recorded.";
    } else {
        echo '<p class="error">Please go ' . "<a href=\"poll_form.html\">back</a>" . ' and fill out the poll!<p>';
    }
    //End of link responses
    //Define variables and make sure file works
    $submit = $_REQUEST['submit'];
    $filename = 'poll_data.txt';
    //next is the stuff that is to be appended
        if ($submit == 'Submit') {
            $handle = fopen($filename, 'a');
            fputs($handle, $Soda.PHP_EOL);
            fclose($handle);
        } else { echo 'You didn\'t click submit';}
    //Now to sort the data and present it
    /*explode('PHP.EOL', $filename);
    $CC = 0;
    $P = 0;
    $MD = 0;
    $SS = 0;
    $BR = 0;
    $DLS = 0;
    $O = 0;
    foreach($filename as $value) {
        if ($value = 'Coca-Cola') {
            $CC = $CC + 1;
        }
        elseif ($value = 'Pepsi') {
            $P = $P + 1;
        }
        elseif ($value = 'MtnDew') {
            $MD = $MD + 1;
        }
        elseif ($value ='Sprite/Sierra-Mist') {
            $SS = $SS + 1;
        }
        elseif ('BigRed') {
            $BR = $BR + 1;
        }
        elseif ('DontLikeSoda') {
            $DLS = $DLS + 1;
        }
        elseif ('Other') {
            $O = $O + 1;
        }
    }*/
?>