2011-09-27 62 views
-2

我有一个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 
     ) 

) 

我试图通过数组做,循环,每次我遇到一个新的问题,我想outout与值的文本输入的问题。

+2

'似乎没有工作'不是问题描述。 – Bobby

回答

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; ?> 
+0

请参阅我的编辑以更好地描述我正在尝试做什么 – Udders

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']

这应该是你在找什么:

$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']为空时运行。然而,你的问题太模糊,看不清你的意思。

+0

请参阅我的编辑以更好地描述我正在尝试执行的操作 – Udders