2014-02-12 2 views
-2

나는 PHP에서 설문 조사를 만들기 위해 노력하고 있습니다. 정보를 txt 파일에 쓰는 방법으로 데이터를 수집하려고합니다. 데이터를 txt 파일에 쓰도록 코드를 가져 오려면 어떻게해야합니까?폴링 데이터를 텍스트 파일에 쓰는 방법은 무엇입니까?

  1. 내 처리기에있는이 코드는 모두 내 txt 파일에 쓰게하려면 어떻게해야합니까? 바닥에있는 것들의 대부분은 아직 중요하지 않습니다. if ($ submit == 'submit') 코드와 그 뒤에 오는 코드를 살펴보십시오. 당신이 작동합니다

    fopen($filename, 'r'); 
    

    를 사용하는 경우

    <!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; 
         } 
        }*/ 
    ?> 
    

답변

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; 
     } 
    }*/ 
?> 
0
+1

감사는 나를 트립되었다 fopen을 권한이었다 밝혀졌다. 내가 runniing하고 있었던 서버 wouldnt는 내가 fopen를 사용할 것을 허락한다. 주위에 방법을 찾았습니다. 이제 작동합니다. – user3302973

관련 문제