Survey help (PHP)

Discussion in 'PHP' started by onlyican.com, May 25, 2006.

  1. #1
    Hey Guys, Long time no posting for me.

    I am going brain dead here.

    This is whats going on

    I have a survey on my website

    then I have a MySQL tbl to add the results

    the table is set out
    FIELDNAME
    q1
    q2
    q3
    q4
    ......


    Now, I am creating this so the results are automatic

    What is the best way to do this.

    I was going to do

    $q1option1 = mysql_result(mysql_query("SELECT COUNT(*) FROM tbl WHERE q1 = 'option1' "),0);

    $q1option2 = mysql_result(mysql_query("SELECT COUNT(*) FROM tbl WHERE q1 = 'option2' "),0);

    But this seems a bit long winded, baring in mind, there are about 20 questions, and 4 options to most questions.

    thats 80 Queries, ouch sorry Mysql.

    Any ideas.
     
    onlyican.com, May 25, 2006 IP
  2. Slapyo

    Slapyo Well-Known Member

    Messages:
    266
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #2
    What are you trying to achieve in the results? An overall score, averages?
     
    Slapyo, May 25, 2006 IP
  3. onlyican.com

    onlyican.com Peon

    Messages:
    206
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    The results are not always numbers

    I am using this code now

    $queryq10c = "SELECT COUNT(q10c) AS quantity10c, q10c from surveys group by q10c order by q10c";

    $resultq10c = mysql_query($queryq10c);

    $options = array();
    $quantity = array();

    echo"</tr><tr>\n";



    while ($rowq10c = mysql_fetch_assoc($resultq10c)){
    $options[] = $rowq10c["q10c"];
    $quantity[] = $rowq10c["quantity10c"];
    }

    At least it is only 1 query per question
     
    onlyican.com, May 25, 2006 IP
  4. Slapyo

    Slapyo Well-Known Member

    Messages:
    266
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #4
    why not add all of them in.

    $queryq10c = "SELECT COUNT(q1) as quantityq1, q1, COUNT(q2) as quantityq2, q2, COUNT(q10c) AS quantity10c, q10c from surveys group by q10c order by q10c";
    Code (markup):
     
    Slapyo, May 25, 2006 IP
  5. onlyican.com

    onlyican.com Peon

    Messages:
    206
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    cos each one is manipulated differently
     
    onlyican.com, May 25, 2006 IP
  6. Slapyo

    Slapyo Well-Known Member

    Messages:
    266
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #6
    I'll keep thinking, but right now I'm sure. :/
     
    Slapyo, May 25, 2006 IP
  7. Bartbos

    Bartbos Peon

    Messages:
    29
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Having both a questions and answers table (separately) would maybe be better.
     
    Bartbos, May 27, 2006 IP