从服务器读取的每一行的独立单选按钮

我目前正在尝试为从我的服务器读取的每一行生成独立的单选按钮。它们应该象征着 ON 和 OFF 开关。我当前的问题是我设法为每一行生成按钮,但这些不是独立的。我一次只能关闭或打开一个。


我知道当每个按钮的名称不同时,单选按钮变得真正“独立”。但我似乎无法想出一种方法来为生成的每一行创建一个新名称。


目前我的代码看起来像这样


    res = $db->query($query);

            if ($res->num_rows > 0) {

                while ($row = $res->fetch_object()) {

                  $query = "SELECT * FROM device_status

                            WHERE device = ($row->id)";

                  $status = $db->query($query);

                  $device_status = $status->fetch_object();

              $content .= <<<END

            <tr>

                <td>{$row->devices}</td>

                <td>{$row->description}</td>

                <td>{$row->room}</td>

                <td><input type='radio' name='radiobutton' id='myCheck'></td>

                <td><input type='radio' name='radiobutton' id='myCheck' checked='checked'></td>

                <!-- <td>{$device_status->status}</td> -->

                <td><a href="delete-device.php?id=$row->id">Delete</a>

                <a href="edit-device.php?id=$row->id">Edit</a></td>

            </tr>

END;

                        }

                    }

    echo $content;

我一直在尝试不同的方法,例如使用<td><input type='radio' name='$row->id' id='myCheck'></td>


但还没有得到它的工作。非常感谢有关如何解决此问题的任何帮助。


谢谢你!


慕码人8056858
浏览 87回答 2
2回答

慕容3067478

如果您想要生成的所有复选框的唯一名称,您可以通过$i为每一行递增来获得它们。res = $db->query($query);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($res->num_rows > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $i = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while ($row = $res->fetch_object()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $query = "SELECT * FROM device_status&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WHERE device = ($row->id)";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $status = $db->query($query);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $device_status = $status->fetch_object();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $content .= <<<END&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>{$row->devices}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>{$row->description}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>{$row->room}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><input type='radio' name='radiobutton<?php echo $i?>' id='myCheck'></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><input type='radio' name='radiobutton<?php echo $i?>' id='myCheck' checked='checked'></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <!-- <td>{$device_status->status}</td> -->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><a href="delete-device.php?id=$row->id">Delete</a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="edit-device.php?id=$row->id">Edit</a></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; END;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $i++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; echo $content;

慕莱坞森

如果您不关心单选按钮的名称,您可以使用“php 随机数生成器”,每一行您为单选按钮名称分配一个新的随机数:<?php&nbsp;$x&nbsp;=&nbsp;rand(5,&nbsp;100000);&nbsp;?>&nbsp;<input&nbsp;type='radio'&nbsp;name='<?php&nbsp;echo&nbsp;$x;&nbsp;?>'&nbsp;id='myCheck'>
打开App,查看更多内容
随时随地看视频慕课网APP