将复选框列添加到表中,该表具有从php中的数据库动态生成的值 [英] Adding a checkbox column to tables with values dynamically generated from a database in php

查看:36
本文介绍了将复选框列添加到表中,该表具有从php中的数据库动态生成的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的表中添加一列,其中将包含复选框,如果将其选中,我也许可以获取所选行数据的所有ID.

I want to add a column in my table that will contain check boxes that if it will be selected I may be able to get all the id's of the selected row data.

这是我的代码:

<table>
    <thead>
        <th>Name</th>
        <th>Company</th>
        <th>Address</th>
    </thead>
    <tbody>
            <?php 
                $sql = "SELECT * FROM Client";
                $qry = mysql_query($sql);

                while($row = mysql_fetch_array($qry)){
                    echo "<tr>
                            <td>$row[name]</td>
                            <td>$row[company]</td>
                            <td>$row[address]</td>
                        </tr>";
                }
    </tbody>
</table>

我的问题是,我如何添加另一列,该列将包含表示要选择的行数据的ID的复选框.以及如何检索这些值?谢谢!

My problem is, how could I add another column that will contain check boxes that will represent the id of row data being selected. And how can I retrieve those values? Thanks!

推荐答案

只需添加另一个并创建一个复选框数组并存储值即可.

Just add an another and create a check box array and store the value.

while($row = mysql_fetch_array($qry)){
   echo "<tr>
            <td>$row[name]</td>
            <td>$row[company]</td>
            <td>$row[address]</td>
            <td><input type='checkbox' name='row_id[]' id='rowid_<?php echo $id ?>' value='<?php echo $id ?>' />
         </tr>";
}

然后,您可以使用$ _POST []方法检索值,并使用所需的任何值.

Then you can retrieve the values by using $_POST[] method and use whatever you want.

$rowid      = $_POST['row_id'];

但是它将以数组格式返回选定的复选框值.您可以使用循环访问所有元素,也可以使用 implode()函数用作字符串.

But it will return the selected checkboxes value in an array format. You can use a loop to access all the elements or you can use implode() function to use as a string.

这篇关于将复选框列添加到表中,该表具有从php中的数据库动态生成的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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