search
Homephp教程php手册thinkphp3.2实现动态构建单选列表

仅提供思路, 抛砖引玉之用!用控制器代码控制模板的输出。 可以重复利用代码, 不用重复制造轮子。
高手请绕道, php纯技术讨论学习群:323899029


function.php 中写一个公共方法用于创建单选列表function CreateRadio($name,$default,$val){<br>     $str = '<span>';<br>     foreach($val as $k => $v){<br>         $str .= '<i>';<br>         if($k==$default){<br>             $str .= '<input><label>'.$v.'</label>';    <br>         }else{<br>             $str .= '<input><label>'.$v.'</label>';    <br>         }<br>         $str .= '</i>';<br>     }<br>     $str .= '</span>';<br>     return $str;<br> <br> }在需要的控制器中写像这样写$sexRadio = CreateRadio('sex',1,array('0'=>'保密','1'=>'男','2'=>'女'));<br>  $this->assign('sexRadio',$sexRadio); 在模板中直接输出 {$sexRadio} 变量即可

最后html的表现是这样的:<span><br>     <i><br>         <input><label>保密</label><br>     </i><br>     <i><br>         <input><label>男</label><br>     </i><br>     <i><br>         <input><label>女</label><br>     </i><br> </span>说明:

CreateRadio($name,$default,$val)

$name 是单选列表的名称,html中的 样式名称sex就为了标明是性别的单选列表, 用于在前台方便js调用。

$default 是列表的默认值,在html中的表现就是那个值被选了,我传进去的是1,所以“男”这个就被选择了。 当然可以传空值进去。

$sexRadio 是一个数组,根据数组的长度循环出所有的值,根据自己的需求传值进去。


当然这是抛砖引玉,用这样的思路可以构建下拉列表、复选框列表。

AD:真正免费,域名+虚机+企业邮箱=0元

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 Article

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)