SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict brain's Avatar
    Join Date
    Sep 2001
    Location
    Amsterdam
    Posts
    249
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    what structure do I need?

    After a written exam, I want my visitors to be able to insert their answers. So something like: ABDDBCDAADAB.... etc. With this anwers I want an overview with percentages per question:
    Question nr. 1
    ---------------
    A 73%
    B 10%
    C 12%
    D 5%

    Question nr. 2
    ---------------
    etc, etc

    I don't want to fill a field per answer because most exams have 180 answers and that way my dbase fills too fast. So I think it would be better to have the 180 answers in just one (varchar) field. Keeping that in mind, how can I achieve the calculated percentages above??

    Can anyone help me out here?

  2. #2
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If the answers are stored in a varchar column, you can easily find a specific answer using string functions, for example MID(answers, 22, 1) returns the 22nd answer. The above table can be put together in two steps. First, find the total number of rows (= exams) in a table:

    PHP Code:
    $total query("SELECT COUNT(*) FROM exams"); 

    Second, use sql "group by" clause and that $total variable to calculate percents:

    PHP Code:
    SELECT
       MID
    (answer221) AS letter,
       (
    COUNT(*) /$total)*100 AS percent
    FROM exams
    GROUP BY letter 


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •