There is a problem with the program logic, please prawn help
$arr = Array (
0 = = Array (' ID ' =>12, ' name ' = ' # '),
1 = = Array (' ID ' =>13, ' name ' = ' No.2 '),
);
$_check_id = 0;
if ($arr) {
foreach ($arr as $v) {
?>
}
}?>
The initial state of the program is up.
Note that $_check_id is three states, the first is 0, that is $_check_id=0, the second is a value, but not equal to the ID in the array, that is $_check_id=2, the third State is: $_check_id is equal to the ID in the array
Now requires that the $_check_id be compared to the ID in the array at the time of the loop
If equal, the radio in input is selected, that is, the state is checked
If not equal, the first radio that is looped out is selected as checked
Of course, can also take out the loop body to achieve, as long as the above problems can be solved, think for two or three hours, trouble everyone to give a thought it
Of course, the array may also be empty, this does not have to judge, very good solution, now the trouble is how to solve the above problem, think of a half-day, no clue, head big.
------Optimal Solution--------------------
$arr = Array (
0 = = Array (' ID ' =>12, ' name ' = ' # '),
1 = = Array (' ID ' =>13, ' name ' = ' No.2 '),
2 = = Array (' ID ' =>14, ' name ' = ' No.3 '),
3 = = Array (' ID ' =>15, ' name ' = ' No.4 '),
);
$_check_id = 14;
$found = false;
$htmls = Array ();
if ($arr) {
foreach ($arr as $v) {
$input _pre = <<
<>
EOT;
$input _post = <<
<>
/>{$v [' Name ']}
EOT;
if (! $found && $_check_id = = $v [' id ']) {
$found = true;
$input _pre. = ' checked ';
}
$htmls [] = Array ($input _pre, $input _post);
}
}
$htmlstr = ";
foreach ($htmls as $html) {
if (! $found) {
$html [0]. = ' checked ';
$found = true;
}
$htmlstr. = Implode ("', $html);
}
Echo $htmlstr;
can be executed directly.
------Other Solutions--------------------
$arr = Array (
0 = = Array (' ID ' =>12, ' name ' = ' # '),
1 = = Array (' ID ' =>13, ' name ' = ' No.2 '),
2 = = Array (' ID ' =>14, ' name ' = ' No.3 '),
);
$_check_id = 0;
if ($arr) {
foreach ($arr as & $v) {
if ($_check_id = = $v [' id ']) {
$v [' check '] = ' checked ';
$_check_id =-1;
}
}
if ($_check_id >=0) $arr [0][' check '] = ' checked ';
foreach ($arr as $v) {
?>
/>
}
}?>
------Other Solutions--------------------
foreach ($arr as $key = = $v) {
if ($key = = $_check_id) {
Here's checked.
Continue
}
......