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

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

问题描述

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

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 document [adhaar] [orginal]; 这样的简单显示,它仍然显示错误!

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!

推荐答案

首先:您是说要使用单选按钮代替?如果此人只能每行选择原始或施乐,则可能需要单选按钮而不是复选框。

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/ rel = nofollow noreferrer> http://myingling.com/random/random/44477622/

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

一些值得一读的东西:


  1. 关联数组

  2. foreach (尤其是使用 $ key => $ val 结构)

  3. key 函数

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

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

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