search
Homephp教程php手册PHP教程:PHP处理数组模式的以选未选input多选框

一般从数据循环出来的选框要处理是否被选中都是使用IF去判断,结果造成代码一堆,速度过慢。

我做了一个闭包分享给大家,希望对大家有用。

代码:

  1. function ifNumExists($numList,$key) {
  2. $return = false;
  3. $arraylist = split(",", $numList);
  4. $Length = count($arraylist)-1; //最后一个数组结果为空,所以应当减去。
  5.         for ($i=0;$i
  6.                 if ($arraylist[$i] == $key) {
  7.                         $return = true;
  8.                 }
  9.         }
  10.         return $return;
  11. }

使用方法:

  1. ifNumExists(数组,当前单选框对应ID)

表结构:
单选框循环表(A):
id   class
1   类别1
2   类别2
3   类别3
4   类别4

单选值存放表(B):
id   classid
1   1,2,3,
2   2,3,4,
3   1,2,

当循环表A,判断是否当前input是否被选中,那么我们就可以使用
ifNumExists(classid,表Aid),返回的结果为true或者false,根据返回的结果我们给input增加或者不增加 checked="checked"。
如:
  1. if (ifNumExists(classid,$value['id'])) {
  2.  $list = $list."".$value['class']."
  3. ";
  4. }else{
  5.  $list = $list."".$value['class']."
  6. ";
  7. }
  8. return $list;



Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools