Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

php/mysql + html POST arrays

  • 27-01-2008 11:51am
    #1
    Closed Accounts Posts: 20


    Hi,

    Having a bit of a mental block over one particular problem developing a web app.

    Basically, the php script is given a category id, and fetches a list of questions, question types (e.g text, textarea, radio, checkbox, etc..), and possible answers (for multiple choice questions) from a database.

    I'm having problems with questions containing multiple answers

    Here's the relevant section of the "ask question" function
    [php]
    $answers = mysql_query("SELECT * FROM answer_choices WHERE question_id = '$question_id'") or die(mysql_error());

    if ($question_type == 'checkbox')
    {

    while ($ans=mysql_fetch_array($answers))
    {
    $ans_id = $ans;
    $ans_text = $ans;

    echo "<input type=\"hidden\" name=\"question_id[]\" value=\"$question_id\" />";
    echo "<input type=\"checkbox\" name=\"answer[]\" value=\"$ans_text\" /> $ans_text<br />";
    }
    }

    [/php]Now its instantly obvious that the problem is the fact that every possible answer in answer[] will have a matching entry in question[], regardless of the wether the checkbox is ticked or not.

    With other questions the question_id data is written to the form before the while loop above, but I thought it may be easier to solve this way, just can't get my head around it though.

    My general process for entering the question answers (but only expecting single answers) is like so:

    [php]
    for ($i=0; $i<count($question); $i++)
    {
    $qid = $question[$i];
    $ans = $answer[$i];


    $qid=cleanup_text($qid);
    $ans=cleanup_text($ans);

    if(!empty($ans))
    {

    $insert_answers = "insert into question_answers (process_id,question_id,answer) values ('$process_id', '$qid', '$ans')";
    mysql_query($insert_answers) or die(mysql_error());
    }

    }

    [/php]So I guess my question is, can I adapt the loop above to handle multiple answers or do I need to go back to square one and use seperate functions for different question types?

    Sorry to ask on first post!


Comments

  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    As I see it there, you are using a parallel array so you getting a 1 - 1 mapping of questions to answers.

    Why dont you make $answers[$i] a inner array of answers, so inside the for loop at the end there, you have an inner loop iterating through each answer. Thing is by the look of your SQL setup - you don't allow for multiple answers - or do you simply want to enter a extra row with same qeuestion_id but different answer. Depends on the SQL setup.

    Thats what I got from reading your post anyways :) - but as usual - havn't woken up yet


  • Closed Accounts Posts: 20 dattley


    Thanks for the reply. Yeah, the idea is each answer there's a single row in the question_answers table containing the question_id, answer_id, and a primary and foreign key, so the answers could be retrieved like so:

    [php]
    function show_question_answers($process_id)
    {

    $query = mysql_query("
    SELECT
    q.question_text, qa.answer
    FROM
    questions q, question_answers qa
    WHERE
    q.question_id = qa.question_id
    AND qa.process_id = '$process_id'

    ") or die(mysql_error());

    while ($row=mysql_fetch_array($query))
    {
    $question_text = $row;
    $answer = $row;

    echo "<tr><th colspan=\"2\">$question_text</th><td class=\"norm\" colspan=\"4\">$answer</td></tr>";
    }


    }


    [/php]As you said, i'm using a parallel array where answer is supposed to relate to question. Guess my problem really is the fact that the question value is added to question[] in a checkbox scenario regardless of wether the answer value is being added to its array. If that makes any sense!

    What do you mean by "make $answers[$i] a inner array of answers"?


Advertisement