如何自动增加表中的复选框ID

时间:2016-05-19 07:44:40

标签: php html mysql

我正在使用此代码从数据库中获取所有数据并将其显示在表中:

class Product
{
private $conn;
private $id;
private $name;
private $description;
private $price;
private $category_id;
private $category_name;
private $created;

public function __construct($db)
{
    $this->conn = $db;
}

public function readAll()
{
    $stmt = $this->conn->prepare('SELECT name, description, price, CategoryID, created FROM products');
    $stmt->execute();
    echo "<table class=\"highlight responsive-table\">
        <thead>
            <tr>
                <th data-field=\"empty\"> </th>
                <th data-field=\"name\">Name</th>
                <th data-field=\"description\">Description</th>
                <th data-field=\"price\">Price</th>
                <th data-field=\"category\">Category</th>
                <th data-field=\"action\">Action</th>
            </tr>
        </thead>";

    while ($result = $stmt->fetch(PDO::FETCH_ASSOC)) {
        $n = $result['name'];
        $d = $result['description'];
        $p = $result['price'];
        $ca = $result['CategoryID'];
        $c = $result['created'];

        echo "<tbody>
                 <tr>
                 <td style=\"width:10%;\">

                            <input type=\"checkbox\" id=\"checkbox\" />
                            <label for=\"checkbox\"></label>

                    </td>



                    <td style=\"width:15%;\">" .$n. "</td>
                    <td style=\"width:30%;\">" . $d. "</td>
                    <td style=\"width:10%;\">" ."$".$p. "</td>
                    <td style=\"width:15%;\">" . $ca. "</td>
                    <td style=\"width:20%;\"> 
                        <a class=\"waves-effect waves-light btn\"><i class=\"material-icons\">mode_edit</i></a>
                        <a class=\"waves-effect waves-light btn\"><i class=\"material-icons\">delete</i></a>
                    </td>";

    }

    echo "</tbody> </table>";


}
}

它正在运行,但是在第一列我希望有一个复选框,以便我可以标记我想要删除/编辑的记录,但是现在,无论我点击哪个复选框,只检查第一个。如何自动增加复选框ID?

4 个答案:

答案 0 :(得分:2)

使用变量$ i,在while循环之前用1初始化它 在你的循环中,增加它 替换你的复选框代码,如

                        <input type=\"checkbox\" id=\"checkbox_$i\" />
                        <label for=\"checkbox_$i\"></label>

$ I ++;

答案 1 :(得分:2)

我建议使用products表中的id字段。如果它没有,我会添加它。

然后在您的html中将复选框行更改为:

<input type=\"checkbox\" id=\"checkbox".$id."\" name=\"delete_product\" value=\"".$id".\" />
<label for=\"checkbox".$id."\"></label>

如果您提交表单,我添加了名称和值,如果您最终使用表单,则可以在POST中获取该数据。

答案 2 :(得分:1)

尝试使用这样的计数变量:

$i = 0;
while ($result = $stmt->fetch(PDO::FETCH_ASSOC)) {
        $n = $result['name'];
        $d = $result['description'];
        $p = $result['price'];
        $ca = $result['CategoryID'];
        $c = $result['created'];

        echo "<tbody>
                 <tr>
                 <td style=\"width:10%;\">

                            <input type=\"checkbox\" id=\"checkbox".$i."\" />
                            <label for=\"checkbox".$i."\"></label>

                    </td>



                    <td style=\"width:15%;\">" .$n. "</td>
                    <td style=\"width:30%;\">" . $d. "</td>
                    <td style=\"width:10%;\">" ."$".$p. "</td>
                    <td style=\"width:15%;\">" . $ca. "</td>
                    <td style=\"width:20%;\"> 
                        <a class=\"waves-effect waves-light btn\"><i class=\"material-icons\">mode_edit</i></a>
                        <a class=\"waves-effect waves-light btn\"><i class=\"material-icons\">delete</i></a>
                    </td>";
        $i++;
    }

答案 3 :(得分:1)

将id列添加到sql查询中。然后将表格中的第一列修改为

<input type=\"checkbox\" id=\"checkbox\" . $result['id'] . " name=\"id[" . $result['id'] . "]\"/>
<label for=\"checkbox" . $result['id'] . "\"></label>

在控制器中,您可以使用例如$ _POST [&#39; id&#39;]

访问所选的ID