views:

178

answers:

2

Here's my issue: In my controller, I want to grab user input from a form. I then parse the input, and compare it to database values to ensure I'm grabbing the correct input. I simply want to match the user's answers to the question, grab the user ID, the question ID, and then determine if the answer applies to a multiple choice or checkbox question, or something else. I take those values and insert them into the answer table. Ignore the waiver stuff. I'll test that once I get the answers input correctly.

    // add answers and waiver consent records
    try {

        $answerArray = array();
        $waiverArray = array();

    // retrieve answers, waiver consents, and the question ID's from form object
    foreach ($formData as $key => $value) {

        $parts = explode("_", $key);

        if ($parts[0] == 'question') {                                                      
            array_push($answerArray, $value);           
        }

        if ($parts[0] == 'waiverTitle') {                                                       
            array_push($waiverArray, $value);                           
        }
    }

    $questions = new Model_DbTable_Questions();
    $questionResults = $questions->getQuestionResults($session->idEvent);                                   

    foreach ( $questionResults as $qr ) {
        if ($qr ['questionType'] == 'multipleChoice' || $qr ['questionType'] == 'checkBox') {
            foreach ( $answerArray as $aa ) {
                $answerData = $answers->addAnswer ( $lastUserID, $qr ['idQuestion'], null, $aa );
echo count ( $answerData ) . ', ' . $qr ['questionType'] . ', ' . $aa . '<br />';
            }
        } else {
            foreach ( $answerArray as $aa ) {
                $answerData = $answers->addAnswer ( $lastUserID, $qr ['idQuestion'], $aa, null );
echo count ( $answerData ) . ', ' . $qr ['questionType'] . ', ' . $aa . '<br />';
            }
        }
    }

    } 

    catch (Zend_Db_Statement_Exception $e) 
    { 

        $e->getMessage();
        throw $e;
    }

From my test data, I expect to get 2 records that match the multiple choice and checkbox criteria, and 1 record for text in the ELSE clause like this:

3, checkbox, 1
3, multipleChoice, 1
3, text, question_2

What I get is a 3x3 Cartesian product, 3 question elements each with the 3 possible answers like this output from the echo statements:

4, checkBox, 1
4, checkBox, 1
4, checkBox, question_2
4, multipleChoice, 1
4, multipleChoice, 1
4, multipleChoice, question_2
4, text, 1
4, text, 1
4, text, question_2

I've tried placing the IF clause inside the inner foreach, but I get the same results. I've been staring at this problem for way too long and cannot see what I'm doing wrong. Your kind assistance would be greatly appreciated. Please let me know if my request requires more clarification.

A: 

I don't really understand:

  1. Why you thought you would get 3 lines outputted, since you've got a nested loop there which iterates through each answer for each question....
  2. Why you thought the try-catch statement would help us answer your question.

But I simplified your code, so somebody can maybe answer the question:

<?php
$answerArray = array();
$waiverArray = array();

// retrieve answers, waiver consents, and the question ID's from form object
foreach ($formData as $key => $value) {

    $parts = explode("_", $key);

    if ($parts[0] == 'question') {                                                      
        array_push($answerArray, $value);           
    }

    if ($parts[0] == 'waiverTitle') {                                                       
        array_push($waiverArray, $value);                           
    }
}

$questions = new Model_DbTable_Questions();
$questionResults = $questions->getQuestionResults($session->idEvent);                                   

foreach ( $questionResults as $qr ) {
    foreach ( $answerArray as $aa ) {
        if (in_array($qr['questionType'], array('multipleChoice','checkBox') )) {
            $answerData = $answers->addAnswer ( $lastUserID, $qr ['idQuestion'], null, $aa );
        } else {
            $answerData = $answers->addAnswer ( $lastUserID, $qr ['idQuestion'], $aa, null );
        }
        echo count ( $answerData ) . ', ' . $qr ['questionType'] . ', ' . $aa . '<br />';
    }
}

?>
arnorhs
It may be self-evident, but my ZF and PHP knowledge is limited. So, I make no claims to the efficiency or efficacy of this code. Y'know how it goes. Think about what you want to do, map out the flow, search the Internet for syntax construction, then try something. That's where I am. I will most certainly apply your rework and learn from the experience. I'll keep you posted. Thanks for your help. Believe me, I need all the help I can get.
Mike
Well, it was worth a shot. Your code provides the exact same results as I originally posted. Thanks anyway.
Mike
yes. I just simplified it for others to solve. Because I don't understand your form parsing and your logic in the questions/answers.
arnorhs
if it helps at all, the business logic is this: extract the user's answers from the form and insert into the answers table. each row needs 3 parameters -- a user ID, a question ID, and either a selectedChoice (for multi-choice and checkbox answers) or a response (in text format). there can be multiple questions on a form. so i need to loop thru the answers, match each with its question, and insert into the db, based on the question type. clear as mud? that's what i'm try to express in code.
Mike
A: 

After much brain-crunching analysis, I finally got it (with much assistance from my coworker and arnorhs, of course). Many thanks for your help. Virtual beers for everyone!

$answerData = array();

$questions = new Model_DbTable_Questions();
$questionResults = $questions->getQuestionResults($session->idEvent);                                   

// retrieve answers, waiver consents, and the question ID's from form object
    foreach ($formData as $key => $value) 
    {
        $parts = explode("_", $key);
        if($parts[0] == 'question')
        {                           
            foreach ($questionResults as $question) 
            {

                if ($parts[1] == $question['idQuestion']) 
                {                   
                    if (in_array($question['questionType'], array('multipleChoice','checkBox') )) 
                    {   
                        $answerData = $answers->addAnswer ( $lastUserID, $question['idQuestion'], null, $value );
                    }
                    else 
                    {
                        $answerData = $answers->addAnswer ( $lastUserID, $question['idQuestion'], $value, null );
                    }
                }

            }
        }
    }
Mike