Activity Stream
48,167 MEMBERS
6890 ONLINE
besthostingforums On YouTube Subscribe to our Newsletter besthostingforums On Twitter besthostingforums On Facebook besthostingforums On facebook groups

Results 1 to 7 of 7
  1.     
    #1
    Member

    Question 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

  2.   Sponsored Links

  3.     
    #2
    Respected 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?

  4.     
    #3
    Member
    PHP 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/>';
    ?>

  5.     
    #4
    Member
    Quote Originally Posted by Lock Down View Post
    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,

  6.     
    #5
    Banned
    Website's:
    TVDDL.com
    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

  7.     
    #6
    Member
    Quote Originally Posted by thefboo View Post
    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 Code: 
    <?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,

  8.     
    #7
    Member
    file.txt
    Code: 
    A
    B
    C
    D
    A
    A
    A
    C
    C
    B
    count.php
    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/>'
    ?>
    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%)

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. more than a question, a survey
    By monsterhost in forum General Discussion
    Replies: 6
    Last Post: 3rd May 2012, 04:02 AM
  2. Can some one remove this survey?
    By MediaStar in forum Technical Help Desk Support
    Replies: 1
    Last Post: 2nd Sep 2011, 07:39 AM
  3. Skip ScriptMafia Survey or Other Survey
    By FreeFileZhare in forum Webmaster Discussion
    Replies: 4
    Last Post: 13th May 2011, 09:42 AM
  4. Do Online Survey Sites Really Pay?
    By Rohit in forum General Discussion
    Replies: 8
    Last Post: 27th Apr 2011, 08:12 PM
  5. [Hiring] Get 0.10$ for do 1 survey
    By juordis in forum Completed Transactions
    Replies: 18
    Last Post: 12th Dec 2009, 11:10 PM

Tags for this Thread

BE SOCIAL