Results 1 to 7 of 7
-
19th Mar 2011, 02:53 PM #1OPMember
Question regarding a survey [PHP]
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,delightedx3 Reviewed by delightedx3 on . Question regarding a survey [PHP] 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. Rating: 5
-
19th Mar 2011, 07:53 PM #2Respected Member
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?
-
19th Mar 2011, 09:23 PM #3MemberPHP Code:
<?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/>';
?>
-
20th Mar 2011, 05:37 PM #4OPMember
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,
-
20th Mar 2011, 05:41 PM #5BannedWebsite's:
TVDDL.comi 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
-
20th Mar 2011, 06:07 PM #6OPMember
I also thought about that.
that would be something like:
PHP Code:<?php
$a1 = 0;
if($_POST["answer"] == "A")
{
$a1++;
fwrite($filename,$a1);
}
?>
SealKing's method is working. But now I want php to count all together and then calculate the percentage.
Regards,
-
20th Mar 2011, 06:59 PM #7Member
file.txt
Code:A B C D A A A C C B
PHP Code:<?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/>';
?>
Code:File: A B C D A A A C C B A: 4 (40%) B: 2 (20%) C: 3 (30%) D: 1 (10%)
Sponsored Links
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Similar Threads
-
more than a question, a survey
By monsterhost in forum General DiscussionReplies: 6Last Post: 3rd May 2012, 04:02 AM -
Can some one remove this survey?
By MediaStar in forum Technical Help Desk SupportReplies: 1Last Post: 2nd Sep 2011, 07:39 AM -
Skip ScriptMafia Survey or Other Survey
By FreeFileZhare in forum Webmaster DiscussionReplies: 4Last Post: 13th May 2011, 09:42 AM -
Do Online Survey Sites Really Pay?
By Rohit in forum General DiscussionReplies: 8Last Post: 27th Apr 2011, 08:12 PM -
[Hiring] Get 0.10$ for do 1 survey
By juordis in forum Completed TransactionsReplies: 18Last Post: 12th Dec 2009, 11:10 PM
themaLeecher - leech and manage...
Version 5.03 released. Open older version (or...