在继续之前,foreach循环检查值已更改

时间:2011-09-27 11:08:14

标签: php loops foreach

我有一个看起来像这样的foreach循环,

<?php $current_question = "";  
    foreach ($question_and_answers as $qa) : ?>  
        <?php $current_question == $qa['current_question']; ?>  
        <?php if($current_question == $current_question) : ?>  
            <input type="text" name="question[]" value="<?php echo $qa['question']; ?>"/>  
        <?php endif; ?>  
<?php endforeach; ?>

我想在每次循环遇到一个新问题时创建一个输入字段(一个问题会返回多次,因为一个问题可以有很多答案)。我所做的事似乎不起作用。

我认为看到我使用的数组有帮助,

    Array
(
    [0] => Array
        (
            [question_id] => 2
            [question] => What is my name?
            [tests_test_id] => 2
            [answer_id] => 5
            [answer] => Simon
            [questions_question_id] => 2
            [correct] => true
        )

    [1] => Array
        (
            [question_id] => 2
            [question] => What is my name?
            [tests_test_id] => 2
            [answer_id] => 6
            [answer] => Dave
            [questions_question_id] => 2
            [correct] => false
        )

    [2] => Array
        (
            [question_id] => 2
            [question] => What is my name?
            [tests_test_id] => 2
            [answer_id] => 7
            [answer] => Fred
            [questions_question_id] => 2
            [correct] => false
        )

    [3] => Array
        (
            [question_id] => 2
            [question] => What is my name?
            [tests_test_id] => 2
            [answer_id] => 8
            [answer] => John
            [questions_question_id] => 2
            [correct] => false
        )

    [4] => Array
        (
            [question_id] => 3
            [question] => What is my surname?
            [tests_test_id] => 2
            [answer_id] => 9
            [answer] => Crawford
            [questions_question_id] => 3
            [correct] => true
        )

    [5] => Array
        (
            [question_id] => 3
            [question] => What is my surname?
            [tests_test_id] => 2
            [answer_id] => 10
            [answer] => Caine
            [questions_question_id] => 3
            [correct] => false
        )

    [6] => Array
        (
            [question_id] => 3
            [question] => What is my surname?
            [tests_test_id] => 2
            [answer_id] => 11
            [answer] => Rooney
            [questions_question_id] => 3
            [correct] => false
        )

    [7] => Array
        (
            [question_id] => 3
            [question] => What is my surname?
            [tests_test_id] => 2
            [answer_id] => 12
            [answer] => Ainley
            [questions_question_id] => 3
            [correct] => false
        )

    [8] => Array
        (
            [question_id] => 4
            [question] => What is my favourite colour?
            [tests_test_id] => 2
            [answer_id] => 13
            [answer] => Blue
            [questions_question_id] => 4
            [correct] => true
        )

    [9] => Array
        (
            [question_id] => 4
            [question] => What is my favourite colour?
            [tests_test_id] => 2
            [answer_id] => 14
            [answer] => Yellow
            [questions_question_id] => 4
            [correct] => false
        )

    [10] => Array
        (
            [question_id] => 4
            [question] => What is my favourite colour?
            [tests_test_id] => 2
            [answer_id] => 15
            [answer] => Green
            [questions_question_id] => 4
            [correct] => false
        )

    [11] => Array
        (
            [question_id] => 4
            [question] => What is my favourite colour?
            [tests_test_id] => 2
            [answer_id] => 16
            [answer] => Red
            [questions_question_id] => 4
            [correct] => false
        )

    [12] => Array
        (
            [question_id] => 5
            [question] => Who do I support?
            [tests_test_id] => 2
            [answer_id] => 17
            [answer] => Huddersfield Town
            [questions_question_id] => 5
            [correct] => true
        )

    [13] => Array
        (
            [question_id] => 5
            [question] => Who do I support?
            [tests_test_id] => 2
            [answer_id] => 18
            [answer] => Leeds United
            [questions_question_id] => 5
            [correct] => false
        )

    [14] => Array
        (
            [question_id] => 5
            [question] => Who do I support?
            [tests_test_id] => 2
            [answer_id] => 19
            [answer] => Manchester United
            [questions_question_id] => 5
            [correct] => false
        )

    [15] => Array
        (
            [question_id] => 5
            [question] => Who do I support?
            [tests_test_id] => 2
            [answer_id] => 20
            [answer] => Wolverhampton Wanderes
            [questions_question_id] => 5
            [correct] => false
        )

)

我正在尝试做什么,遍历数组,每当我遇到一个新问题时,我想要输出一个带有问题值的文本输入。

2 个答案:

答案 0 :(得分:0)

试试这个。

<?php $current_question = "";  
    foreach ($question_and_answers as $qa) : ?>  
        <?php if($current_question == $qa['current_question']) : ?>  
            <input type="text" name="question[]" value="<?php echo $qa['question']; ?>"/> 
            <?php $current_question = $qa['current_question']; ?>  
        <?php endif; ?>  
<?php endforeach; ?>

答案 1 :(得分:0)

您的代码非常奇怪,您可能有理由以这种方式编写代码,但让我重写它以便我可以更好地查看:

$current_question = '';

foreach($questions_and_answers as $qa){

    $current_question == $qa['current_question'];

    if($current_question == $current_question){ ?>
        <input type="text" name="question[]" value="<?php echo $qa['question']; ?>"/>
    <? }

}

我看到了几个错误:

  1. $current_question == $qa['current_question'];什么都不做,这是一个有条件的,会返回true或false,但它不会返回任何东西。我认为你已经把这个与定义混为一谈:$current_question = $qa['current_question'];
  2. $current_question == $current_question;将始终返回1,因为它是相同的变量。我认为您正在尝试将其与$qa['current_question']
  3. 进行比较

    这应该是你要找的东西:

    $current_question = '';
    
    foreach($questions_and_answers as $qa)
        if($current_question == $qa['current_question']){ ?>
            <input type="text" name="question[]" value="<?php echo $qa['question']; ?>"/>
        <? }
    

    或者在您的代码中:

    <?php $current_question = "";  
        foreach ($question_and_answers as $qa) : ?>  
            <?php if($current_question == $qa['current_question']) : ?>  
                <input type="text" name="question[]" value="<?php echo $qa['question']; ?>"/>  
            <?php endif; ?>  
    <?php endforeach; ?>
    

    然而,这仍然很奇怪,因为您将$current_question定义为空字符串,因此仅当$qa['current_question']为空时才会运行条件。但是你的问题太模糊了,看不出你的意思。