2011-09-27 49 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