foreach loop checking a value has changed before moving on
I have a foreach loop that looks like this,
<?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; ?>
I am wanting to create an input field every time the loop hits a new question (a question gets returned numberous times, as a question can have numerous answers). What I have done does not seem to work.
I think seeing the array I working with help,
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
开发者_C百科 [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
)
)
What I am trying to do, loop through the array, and everytime I meet a new question I want to outout a text input with the value of the question.
Try this.
<?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; ?>
Your code is very strange, you probably have your reasons for writing it that way but let me rewrite it so I can take a better look:
$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']; ?>"/>
<? }
}
I see a couple of faults:
$current_question == $qa['current_question'];
does nothing, this is a conditional and will return true or false, but it won't return to anything. I think you've got this mixed up with a definition:$current_question = $qa['current_question'];
$current_question == $current_question;
will always return1
because it's the same variable. I think you are trying to compare it to$qa['current_question']
This should be what you're looking for:
$current_question = '';
foreach($questions_and_answers as $qa)
if($current_question == $qa['current_question']){ ?>
<input type="text" name="question[]" value="<?php echo $qa['question']; ?>"/>
<? }
Or in your code:
<?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; ?>
This is still strange however, because you define $current_question
as an empty string, so the conditional will only run if $qa['current_question']
is empty. However your question is too vague to see what you mean exactly.
精彩评论