doudong7256 2012-12-04 21:30
浏览 34
已采纳

从已发布的表单显示多维数组

Hi I am trying to display information from a form, the number of question and number of choices are dynamically generated by the user. The form is as follows:

<input name='questions[]' type='text' value='question 1' /><br/>
   <input name='choice[1-1][]' type='text' value='option 1' /><br/>
   <input name='choice[1-1][]' type='text' value='option 2' /><br/>
   <input name='choice[1-1][]' type='text' value='option 3' /><br/>
<input name='questions[]' type='text' value='question 2' /><br/>
   <input name='choice[2-1][]' type='text' value='option 1' /><br/>
   <input name='choice[2-1][]' type='text' value='option 2' /><br/>
   <input name='choice[2-1][]' type='text' value='option 3' /><br/>

etc...

I have had no problems with creating the code to generate the form, my problem lies in the displaying the information which the uses puts into the form.

So far I have tried using a foreach loop to display the question then another foreach loop to display the choices. I have also tried using the count() then using a for loop to count through the number of choices and output them that way, with no success.

My problem is that when I try to display the choices, they are all displayed before the next question is displayed and then all the choices are displayed again, before the script moves onto the next question and so on.

  • 写回答

1条回答 默认 最新

  • douhu2525 2012-12-04 22:03
    关注

    Your intended outcome is not very clear, but here is an idea of what I think you are looking to do.

    $i=1;
    foreach ($questions as $question){
    echo $question;
    echo "<br />";
    foreach($choice[$i.'-1'] as $choices){
    echo '-'.$choices.'<br />';
    }
    echo "<br />";
    $i++;
    }
    

    Based off your <form> code, when it is posted, you would have arrays like -

    $questions = array('question 1', 'question 2');
    $choice['1-1']= array('option 1','option 2','option 3');
    $choice['2-1']= array('option 1','option 2','option 3');
    

    So using the foreach on those arrays, you would get an output of -

    question 1
    -option 1
    -option 2
    -option 3
    
    question 2
    -option 1
    -option 2
    -option 3
    

    You can see a phpfiddle at - http://phpfiddle.org/main/code/i4f-k7w

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C