Generally from the data cycle out of the selection box to handle whether it is selected is to use if to judge, resulting in a heap of code, the speed is too slow.
I made a closure to share with everyone, hope to be useful to everyone.
Code:
- function ifnumexists ($numList, $key) {
- $return = false;
- $arraylist = Split (",", $numList);
- $Length = count ($arraylist)-1; The last array result is empty, so it should be subtracted.
- for ($i =0; $i < $Length; $i + +) {
- if ($arraylist [$i] = = $key) {
- $return = true;
- }
- }
- return $return;
- }
How to use:
- Ifnumexists (array, corresponding ID of the current radio box)
table structure:
Single selection box loop table (A):
ID class
1 Category 1
2 Category 2
3 Category 3
4 Category 4
Single Value deposit table (B):
ID ClassID
1 1,2,3,
2 2,3,4,
3 1, 2,
When loop table A determines whether the current input is selected, then we can use the
Ifnumexists (ClassID, table aid), returns a result of true or false, according to the results of the return we give input or do not increase checked= "checked".
Such as:
- if (Ifnumexists (ClassID, $value [' ID '])) {
- $list = $list. " <input type=\ "checkbox\" name=\ "sendnums[]\" "value=\" ". $value [' id ']." \ "checked=\" checked\ "/>". $value [' class ']. " </li> ";
- }else{
- $list = $list. " <input type=\ "checkbox\" name=\ "sendnums[]\" "value=\" ". $value [' id ']." \ "/>". $value [' class ']. " </li> ";
- }
- return $list;