问题
I want to store checkbox in database.. but the checkbox is a bit complex than regular. Basically this is what I want to implement.
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 ) )
My HTML code.
<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
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!
回答1:
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.
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
}
Here is a link to a working example: http://myingling.com/random/random/44477622/
Some things worth reading about:
- associative arrays
- foreach (especially using the
$key => $val
structure) - the key function
来源:https://stackoverflow.com/questions/44477622/store-multidimensional-checkbox-in-database