2011-09-27 3 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 
     ) 

) 
 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

'작동하지 않는 것 같습니다. '는 문제 설명이 아닙니다. – 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을 반환 할 것이다.

    $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; ?> 
    

    이 그러나 아직도 이상하다 난 당신이 당신이 찾고있는 무엇을해야 $qa['current_question']

이와 비교하려는 생각 왜냐하면 $current_question을 빈 문자열로 정의하기 때문에 $qa['current_question']이 비어있는 경우에만 조건부가 실행됩니다. 그러나 귀하의 질문은 당신이 정확히 무엇을 의미하는지보기에는 너무 모호합니다.

+0

내가하려는 일에 대한 더 자세한 설명을 보려면 제 편집을 참조하십시오. – Udders

관련 문제