Question regarding a survey [PHP]

Status
Not open for further replies.

delightedx3

Active Member
486
2010
0
0
Hey guys,

at school I now got into php in informatics.

Our job is to create homepage with a survey in it. The homepage ain't the problem as I know html and that stuff.


So I have to put a question and let the user choose 4 answers via radio buttons. Let's assume that the Answers are A,B,C and D.

Now the teacher asked me to safe the answers in a file, which ain't a problem, too.

The way I do that is the following:
I create a form with a submit button. On the site reffered in action="" I created an array with the answers A,B, C and D. The radio buttons do have a value which is transmitted.

So 'till now I don't have a problem to write the answer of a user in a file, but my big problem is to evaluate the results.

So my question is:

Is there a way php can browse every line of a file and count how often A appears or B, C or D?

I need to show the results of the survey through the width of a table or a div, but that ain't a big deal once I got the value. The best thing would be percentage.


Is there anyone willing to help me? I would really appreciate it as it won't be a big deal for php-pros out there. ;)

Thanks in advance.

Regards,
 
6 comments
PHP:
<?php
$f = file_get_contents('./file.txt');
echo 'File: '.$f.'</br>';
echo 'A: '.substr_count("$f","A").'<br/>';
echo 'B: '.substr_count("$f","B").'<br/>';
echo 'C: '.substr_count("$f","C").'<br/>';
echo 'D: '.substr_count("$f","D").'<br/>';
?>
 
The answer is yes but are you just writing a single line file for the answers?

What does the layout of the saved file look like?

I wanted to write into the file using fwrite($filename, $_POST["answer"]."\r\n");
so it would look like.

A
B
C
D
A
B

I want php to count all together and then only count how often A, B, C and D was selected.

Is anyone able to help me? Remember, I am a newbie at php, so don't expect me to comprehend the code at once.

Regards,
 
i dont know the codes etc..but my logic would be to use increment operator for a,b,c,d.
for eg:ques 1 options are a,b,c and d.set the values of a1,b1,c1 and d1 to 0..If the user chooses a,then the code should do a++..

my 20cent from my "java" based thinking lol
 
i dont know the codes etc..but my logic would be to use increment operator for a,b,c,d.
for eg:ques 1 options are a,b,c and d.set the values of a1,b1,c1 and d1 to 0..If the user chooses a,then the code should do a++..

my 20cent from my "java" based thinking lol
I also thought about that.
that would be something like:

PHP:
<?php

$a1 = 0;
if($_POST["answer"] == "A")
{
$a1++;
fwrite($filename,$a1);
}

?>

The only problem is that I would have to make many files and the code would be too long.

SealKing's method is working. But now I want php to count all together and then calculate the percentage.

Regards,
 
file.txt
Code:
A
B
C
D
A
A
A
C
C
B

count.php
PHP:
<?php 
$f = file_get_contents('./file.txt'); 
echo 'File: '.$f.'</br>'; 

$countA = substr_count($f,"A"); 
$countB = substr_count($f,"B"); 
$countC = substr_count($f,"C"); 
$countD = substr_count($f,"D"); 

$sum = $countA + $countB + $countC + $countD;
 
$proA = round(($countA * 100)/$sum,2);
$proB = round(($countB * 100)/$sum,2);
$proC = round(($countC * 100)/$sum,2);
$proD = round(($countD * 100)/$sum,2);

echo 'A: '.$countA.' ('.$proA.'%)<br/>'; 
echo 'B: '.$countB.' ('.$proB.'%)<br/>'; 
echo 'C: '.$countC.' ('.$proC.'%)<br/>'; 
echo 'D: '.$countD.' ('.$proD.'%)<br/>'; 
?>

result
Code:
File: A B C D A A A C C B
A: 4 (40%)
B: 2 (20%)
C: 3 (30%)
D: 1 (10%)
 
Status
Not open for further replies.
Back
Top