简体   繁体   English

跟踪选择单选按钮的次数,并将其存储在PHP的文本文件中

[英]Track number of times radio button is selected and store in text file for PHP

Hey this is my first post so I hope I am approaching this properly but anyway, I have a survey in which a user is presented with 8 questions and they are supposed to rate how they feel about each question by clicking the radio buttons valued from 1(strongly disagree)-5(strongly agree). 嘿,这是我的第一篇文章,所以我希望我能正确地进行处理,但是无论如何,我进行了一项调查,向用户显示8个问题,他们应该通过单击从1开始的单选按钮来评估他们对每个问题的看法(强烈不同意)-5(强烈同意)。 So what I need to do is record the frequency in which each radio button is selected. 因此,我需要记录每个单选按钮被选中的频率。 Something like this: 像这样:

$filename = "Results.txt";
$lines = file($filename); 
$q1 = $_POST['q1'];
$q2 = $_POST['q2'];
$q3 = $_POST['q3'];
$q4 = $_POST['q4'];
$q5 = $_POST['q5'];
$q6 = $_POST['q6'];
$q7 = $_POST['q7'];
$q8 = $_POST['q8'];

foreach($lines as $line) {  
        echo $line;
        if (isset($q1)){
            echo $line[$q1];
            echo $lines;
        }
    }

Where q represents question#. 其中q代表问题#。 Lastly this needs to keep a running calc and the results.txt should look something like this: 最后,这需要保持运行的calc,results.txt应该如下所示:
0,0,5,0,0 0,0,5,0,0
0,0,0,0,0 0,0,0,0,0
0,0,0,0,0 0,0,0,0,0
0,0,0,0,0 0,0,0,0,0
0,0,0,0,0 0,0,0,0,0
0,0,0,0,0 0,0,0,0,0
0,0,0,0,0 0,0,0,0,0
0,0,0,0,0 0,0,0,0,0
The 5 in line one means 5 users submitted a neutral radio button option (value 3). 第5行表示5个用户提交了中性单选按钮选项(值3)。 Each line represents the tally for each question. 每行代表每个问题的理货。

Please help! 请帮忙!

Something like this should answer your "question" at hand, but also yes you should move this application to use a database. 这样的事情应该可以回答您的“问题”,但是是的,您应该移动此应用程序以使用数据库。

$filename = "Results.txt";
$lines = file($filename);

$q1 = $_POST['q1']; //stored value between 1-5
$q2 = $_POST['q2'];
$q3 = $_POST['q3'];
$q4 = $_POST['q4'];
$q5 = $_POST['q5'];
$q6 = $_POST['q6'];
$q7 = $_POST['q7'];
$q8 = $_POST['q8'];

$qN = 1;  //question number
$newLines = '';
foreach($lines as $line) {

    echo $line;

    $line = trim($line);  //remove excess newlines etc.
    $lineArr = explode(',',$line);  //split line into array by commas
    $index = ${'q'.$qN}-1; //zero based
    if (isset($lineArr[$index])){
        $lineArr[$index]++;  //add to position by one vote.
    }
    $qN++;
    $newLines .= implode(',',$lineArr) . "\n";
}

//write contents back to file.
file_put_contents($filename, $newLines);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM