2017-02-21 81 views
0

所以我試圖輸出一個簡單的測驗的多維數組。我認爲這是一個邏輯錯誤,但我在這個問題上摸不着頭腦。問題和答案選擇不會顯示在瀏覽器中,但所有的單選按鈕都可以。輸出多維數組,只顯示單選按鈕(嵌套的foreach循環)

任何幫助/提示將不勝感激。

<body> 
<?php 
$q_and_ans = array(
    array('question' => 'What\'s the narrators name?', 
     'ans' => array(
      'Greg Scmitz', 
      'Greg Sanders', 
      'Greg Smith'), 
      'correct_ans' => 0), 
array('question' => 'What is Greg\'s new friend\'s name?', 
    'ans' => array(
     'Jose', 
     'Man`enter code here`uel', 
     'Manuelo'), 
     'correct_ans' => 1), 
array('question' => 'What beer are they drinking?', 
    'ans' => array(
     'Pacifico', 
     'Corona', 
     'Pilsener'), 
     'correct_ans' => 2), 
array('question' => 'Where was the Mariscopa video taken?', 
    'ans' => array(
     'Ecuador', 
     'El Salvador', 
     'Mexico'), 
     'correct_ans' => 1), 
array('question' => 'What was in the narrator\'s bowl that he failed to mention?', 
    'ans' => array(
     'Shrimp', 
     'Octopus', 
     'Squid'), 
     'correct_ans' => 0), 
array('question' => 'What color was Manuel\'s shirt?', 
    'ans' => array(
     'Green', 
     'White', 
     'Blue'), 
     'correct_ans' => 2), 
array('question' => 'What was printed on the beer bucket?', 
    'ans' => array(
     'Corona Light', 
     'Corona Extra', 
     'Modelo'), 
     'correct_ans' => 1), 
array('question' => 'What kind of vehicle is behind Manuel?', 
    'ans' => array(
     'Car', 
     'Truck', 
     'There is no vehicle'), 
     'correct_ans' => 1), 
array('question' => 'What color is the roof over the narrator\s head?', 
    'ans' => array(
     'Brown', 
     'Tan', 
     'Green'), 
     'correct_ans' => 2), 
array('question' => 'What kind of animal is on Manuel\s shirt?', 
    'ans' => array(
     'Aligator', 
     'Duck', 
     'There is no animal'), 
     'correct_ans' => 0), 

) 
?> 

我認爲這個問題是在嵌套的foreach循環

<form method="post" action=""> 

    <?php 
     foreach($q_and_ans as $i => $q_and_an):?> 

      <p><?php $q_and_an['question']?></p> 
      <?php foreach($q_and_an['ans'] as $a => $ans): ?> 
      <input type="radio" value="$a" name="question[$i]"> <?php $ans ?> 

      <?php endforeach;?> 
    <?php endforeach;?> 

    <input type="submit" name="submit" id="submit"> 
</form> 

回答

0

「值=」 $」名稱= 「的問題[$ i]」 $和$我是不能在PHP標籤。 而且我會用<?php echo $q_and_an['question'];?>

+0

但現在我只能選擇一個出口30個收音機的?嗯 – theBartender

+0

<?PHP \t \t \t的foreach($ q_and_ans爲$ I => $ q_and_an):?????> \t \t

<?php foreach($ q_and_an ['ans'] as $ a => $ ans):?> 」name =「question []」><?php // $ ANS> \t \t \t
\t \t \t theBartender

+0

'<形式方法= 「POST」 行動= 「」 > <?PHP的 的foreach($ q_and_ans爲$ I => $ q_and_an):>

$ ans):?> 」name =「question [<?php echo $ i;?>]」 ><?php echo $ ans; ?> ' – bin