动态创建复选框 [英] Dynamically creating checkboxes

查看:117
本文介绍了动态创建复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是php的新手。我想动态创建从MySQL获取的结果的复选框。如果我在雇员表中有10条记录,所以它必须创建10个带有员工名称的复选框作为value.I曾看过几个教程使数组的复选框等,但无法解决问题。请任何人在那里帮助!!!

i am new to php.I want to dynamically create check boxes upon the result fetched from MySQL.If i have 10 records in employee Table so it must create 10 check boxes with employee name as value.I had seen several tutorials to make array of check boxes etc but could not fix the problem.Please anyone there to help!!!

推荐答案

试试这个:

<?php 
    //Create the query
    $sql = "SELECT `name` FROM Employees";

    //Run the query
    $query_resource = mysql_query($sql);

    //Iterate over the results that you've gotten from the database (hopefully MySQL)
    while( $employee = mysql_fetch_assoc($query_resource) ):
?>

    <span><?php echo $employee['name']; ?></span>
    <input type="checkbox" name="employees[]" value="<?php echo $employee['name']; ?> /><br />

<?php endwhile; ?>

两件事实际上正常工作:

The example you see above relies on two things to actually function properly:


  1. 您正在使用MySQL

  2. 您的SQL查询必须检索员工的姓名(以便您可以在循环中使用它们)

这篇关于动态创建复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆