foreach中的php if子句没有正确检索数据

时间:2010-04-18 21:57:24

标签: php zend-form foreach

这是我的问题:在我的控制器中,我想从表单中获取用户输入。然后我解析输入,并将其与数据库值进行比较,以确保我抓住正确的输入。我只想匹配用户对问题的回答,获取用户ID,问题ID,然后确定答案是否适用于多项选择或复选框问题,或其他问题。我拿这些值并将它们插入答案表。忽略弃权的东西。一旦我正确输入了答案,我就会测试一下。

    // 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;
    }

从我的测试数据中,我希望得到2条符合多项选择和复选框标准的记录,以及ELSE子句中的1条记录,如下所示:

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

我得到的是一个3x3笛卡尔积,3个问题元素,每个都有3个可能的答案,如echo语句中的输出:

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

我已经尝试将IF子句放在内部foreach中,但是我得到了相同的结果。我一直盯着这个问题太长时间,看不出我做错了什么。非常感谢您的帮助。如果我的要求需要更多说明,请告诉我。

2 个答案:

答案 0 :(得分:0)

我真的不明白:

  1. 为什么你认为你会输出3行,因为你有一个嵌套循环,它会遍历每个问题的每个答案....
  2. 为什么你认为try-catch语句可以帮助我们回答你的问题。
  3. 但是我简化了你的代码,所以有人可以回答这个问题:

    <?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 />';
        }
    }
    
    ?>
    

答案 1 :(得分:0)

经过大量的脑力训练分析后,我终于得到了它(当然,在我的同事和arnorhs的帮助下)。非常感谢您的帮助。为每个人提供虚拟啤酒!

$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 );
                    }
                }

            }
        }
    }