1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Remember data MySQL column

Discussion in 'PHP' started by Vistavision, May 10, 2014.

  1. #1
    Hi everybody,

    I have a PHP question and I can't figure it out. I have a simple quiz and based on the answers, I want to show a total score.

    I have a MySQL database with 4 columns, for example:

    Col1 | Col2 | Col3 | Col4
    4 | 2 | 3 | 2
    4 | 2 | 2 | 3
    4 | 2 | 1 | 1

    Now I want to count the columns together based on an if-statement:

    if ($_POST['vraag1'] == "question1") {
    $data = mysql_query("SELECT Col1 FROM test") or die(mysql_error());
    }

    if ($_POST['vraag1'] == "question2") {
    $data = mysql_query("SELECT Col2 FROM test") or die(mysql_error());
    }

    if ($_POST['vraag1'] == "question3") {
    $data = mysql_query("SELECT Col3 FROM test") or die(mysql_error());
    }

    if ($_POST['vraag1'] == "question4") {
    $data = mysql_query("SELECT Col4 FROM test") or die(mysql_error());
    }

    So if the first and third option are valid, then I want to count col1 + col 3 together. How can I remember the first and third statement so I can count them together? Can I do this with a Session?

    Please let me know! I am pretty new to php so I hope you can help me out! Thanks!

    Kind regards,
    Mark
     
    Vistavision, May 10, 2014 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #2
    If the questions are asked on different pages then you have two simple choices
    1. save in a session (as you suggested)
    2. put the answers to the earlier questions in hidden fields on the form
     
    sarahk, May 11, 2014 IP
  3. hip_hop_x

    hip_hop_x Active Member

    Messages:
    522
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    90
    #3
    I'd suggest using the hidden inputs if you can use them, it won't use as many server resources as a session.
     
    hip_hop_x, May 12, 2014 IP
  4. Vistavision

    Vistavision Greenhorn

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #4
    Hi,

    Thanks for all reactions. Cature the data form is not really the problem, I do that as following:

    
    <form action="resultaat.php" method="post">
      <ul class="question1">
         <li><label for="normaal"><input type="radio" id="normaal" name="vraag1" value="Normaal">Normaal</label></li>
         <li><label for="goed"><input type="radio" id="goed" name="vraag1" value="Goed">Goed</label></li>
         <li><label for="zeergoed"><input type="radio" id="zeergoed" name="vraag1" value="Zeer Goed">Zeer Goed</label></li>
       </ul>
    <ul class="question2">
         <li><label for="onbelangrijk"><input type="radio" id="onbelangrijk" name="vraag2" value="Onbelangrijk">Onbelangrijk</label></li>
         <li><label for="belangrijk"><input type="radio" id="belangrijk" name="vraag2" value="Belangrijk">Belangrijk</label></li>
         <li><label for="zeerbelangrijk"><input type="radio" id="zeerbelangrijk" name="vraag2" value="Zeer belangrijk">Zeer belangrijk</label></li>
       </ul>
    <input id="verzenden" type="submit" value="Verzenden">
    
    HTML:
    And capture the data with:

    
    $question1 = $_POST['question1'];
    
    PHP:
    Now if a user choose for example "Normaal" on question1 and "Belangrijk" on question 2 I want to capture the results from my database column 1 and 4 and if they choose "Normaal" on question1 and "Zeer belangrijk" on question 2, I want to capture column 1 and 3.

    With 20 questions and 3 radio buttons per question I get a lot of if-statements, so is there a better way to do this?

    Do I really need to define 60 if-statements or is there an easier way?

    Kind regards,
    Mark
     
    Vistavision, May 12, 2014 IP
  5. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #5
    I'd probably just query the database and then have some logic to decide which items from the row that is returned to use. I'd want to go through the business case and see what the pattern is in working through those 20 questions and what they're pulling out of the database.
     
    sarahk, May 12, 2014 IP