PHP Tutorial: PHP processing array mode to select the optional input multi-selection box

Source: Internet
Author: User

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:
    1. function ifnumexists ($numList, $key) {
    2. $return = false;
    3. $arraylist = Split (",", $numList);
    4. $Length = count ($arraylist)-1; The last array result is empty, so it should be subtracted.
    5. for ($i =0; $i < $Length; $i + +) {
    6. if ($arraylist [$i] = = $key) {
    7. $return = true;
    8. }
    9. }
    10. return $return;
    11. }

How to use:
    1. 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:
    1. if (Ifnumexists (ClassID, $value [' ID '])) {
    2. $list = $list. " <input type=\ "checkbox\" name=\ "sendnums[]\" "value=\" ". $value [' id ']." \ "checked=\" checked\ "/>". $value [' class ']. " </li> ";
    3. }else{
    4. $list = $list. " <input type=\ "checkbox\" name=\ "sendnums[]\" "value=\" ". $value [' id ']." \ "/>". $value [' class ']. " </li> ";
    5. }
    6. return $list;


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.