我想使用PHP中的复选框从数据库中选择特定字段。 我的代码是:
<?php
echo "<table border='1' class='tabtext'>";
$result = mysql_query("SELECT * FROM orders");
$numfields = mysql_num_fields($result);
echo "<table>\n<tr>";
for ($i=0; $i < $numfields; $i++) // Header
{
echo '<th>'.mysql_field_name($result, $i).'</th>';
echo '<tr><td><input type="hidden" name="checkbox[]" value="false"/></td></tr>';
echo '<tr><td><input type="checkbox" name="checkbox[]" value="true"/></td></tr>';
$checkbox[$i] = isset($_POST['checkbox'][$i]) ? true : false;
if(isset($checkbox))
{
foreach($checkbox as $value)
{
echo $value."<br>"; //it will print the value of your checkbox that you checked
}
}
}
答案 0 :(得分:0)
我认为这样的事情会给你带来更好的结果。
<?php
echo "<table border='1' class='tabtext'>";
$result = mysql_query("SELECT * FROM orders");
$numrows = mysql_num_rows($result);
$numfields = mysql_num_fields($result);
// show headers
echo '<thead><tr>';
for ($field = 0; $field < $numfields; $field++) {
$field_name = mysql_field_name($result, $i);
echo '<th><label><input type="checkbox" name="checkbox[' . $field_name . ']" value="1"/> ' . $field_name . '</label></th>';
}
echo '</tr></thead>';
echo '<tbody>';
for ($row = 0; $row < $numrows; $row++) {
$data = mysql_fetch_assoc($result);
echo '<tr>';
for ($field = 0; $field < $numfields; $field++) {
$field_name = mysql_field_name($result, $i);
if (isset($_POST['checkbox'][$field_name])) {
echo '<td>' . $data[$field_name] . '</td>';
}
}
echo '</tr>';
}
echo '</tbody>';
echo '</table>';