在数据库中存储Multidimentional复选框 [英] Store Multidimentional checkbox in database

查看:164
本文介绍了在数据库中存储Multidimentional复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将复选框存储在数据库中..但复选框比常规复杂。
基本上是我想要实现的。

I want to store checkbox in database.. but the checkbox is a bit complex than regular. Basically this is what I want to implement.

我能够从输入中收集数据并将其存储在数组中,但我无法按顺序显示它。

我输入的例子
然后这就是我在print_r()数组:

I am able to collect data from input and store it in an array, but I am not able to display it in sequence.
Example of inputs that I give.
Then this is what I get when I print_r() the array:

Array ( [aadhar] => Array ( [original] => 1 ) [pan] => Array ( [original] => 1 ) [address] => Array ( [xerox] => 1 ) [lightbill] => Array ( [original] => 1 ) )

我的HTML代码。

 <table>
      <tr>
      <td>Aadhar</td>
      <td><input type="checkbox" name="document[aadhar][original]" value="1"/></td>
      <td><input type="checkbox" name="document[aadhar][xerox]" value="1"/></td>
      </tr>
      <tr>
      <td>Pan Card</td>
      <td><input type="checkbox" name="document[pan][original]" value="1"/></td>
      <td><input type="checkbox" name="document[pan][xerox]" value="1"/></td>
      </tr>
      <tr>
      <td>Address</td>
      <td><input type="checkbox" name="document[address][original]" value="1"/></td>
      <td><input type="checkbox" name="document[address][xerox]" value="1"/></td>
      </tr>
      <tr>
      <td>Light Bill</td>
      <td><input type="checkbox" name="document[lightbill][original]" value="1"/></td>
      <td><input type="checkbox" name="document[lightbill][xerox]" value="1"/></td>
      </tr>
      <tr>
      <tr>
      <td><input type="submit" value="Submit" /></td>
      </tr>
    </table>

问题:
用户点击提交后,我想向他展示他的选择, (如确认)。
示例:

PROBLEM: After the user click submit, I want to show him, his selection,(like a confirmation). Example:

Document Selected
----------------- 
Adhaar - Original 
Pan card - Original
Address - xerox 
Light bill - Original

我应该怎么做?我尝试过foreach()但不工作。
即使当我尝试简单显示像 echo文件[adhaar] [原始]; 仍然显示错误!

How should I do it? I tried foreach() but not working. Even when I try to simple display like echo document[adhaar][orginal]; still it shows error!

推荐答案

第一:你的意思是使用单选按钮取而代之?如果这个人应该只能选择原始或Xerox每行,你可能需要单选按钮,而不是复选框。

First: Do you mean to be using radio buttons instead? If the person should only be able to choose Original OR Xerox per row, you might want radio buttons instead of checkboxes.

也就是说,如果你确实需要复选框,输出PHP会像这样:

That said, if you do want checkboxes, the output PHP would be something like:

<?php

// loops through each "document" value in the POST
foreach($_POST['document'] as $key => $val) {
    echo $key; // print the key for each value in document (what you treat as a "row"
    foreach($val as $k => $v) {
       echo " - ".$k; // print EACH value within that key (what you thread as a "column"
    }
    echo "<br/>\n"; // after all columns per row are printed, print a line break
}

以下是一个实际示例的链接:
< a href =http://myingling.com/random/random/44477622/ =nofollow noreferrer> http://myingling.com/random/random/44477622/

Here is a link to a working example: http://myingling.com/random/random/44477622/

有些值得一读的内容:

Some things worth reading about:


  1. 关联数组
  2. foreach (尤其是使用 $ key => $ val structure)

  3. 功能

  1. associative arrays
  2. foreach (especially using the $key => $val structure)
  3. the key function

这篇关于在数据库中存储Multidimentional复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆