我正在尝试生成一个包含每行复选框的表格。我在下面找到了一个基于查询结果生成表的工作代码。是否可以在此处插入一个代码,该代码将提供一个额外的列,其中包含每行中的复选框?
<?php
function SQLResultTable($Query)
{
$link = mysql_connect("localhost","root" , "") or die('Could not connect: ' . mysql_error()); //build MySQL Link
mysql_select_db("dbName") or die('Could not select database'); //select database
$Table = ""; //initialize table variable
$Table.= "<table border='1' style=\"border-collapse: collapse;\">"; //Open HTML Table
$Result = mysql_query($Query); //Execute the query
if(mysql_error())
{
$Table.= "<tr><td>MySQL ERROR: " . mysql_error() . "</td></tr>";
}
else
{
//Header Row with Field Names
$NumFields = mysql_num_fields($Result);
$Table.= "<tr style=\"background-color: #000066; color: #FFFFFF;\">";
for ($i=0; $i < $NumFields; $i++)
{
$Table.= "<th>" . mysql_field_name($Result, $i) . "</th>";
}
$Table.= "</tr>";
//Loop thru results
$RowCt = 0; //Row Counter
while($Row = mysql_fetch_assoc($Result))
{
//Alternate colors for rows
if($RowCt++ % 2 == 0) $Style = "background-color: #FFCCCC;";
else $Style = "background-color: #FFFFFF;";
$Table.= "<tr style=\"$Style\">";
//Loop thru each field
foreach($Row as $field => $value)
{
$Table.= "<td>      $value      </td>";
}
$Table.= "</tr>";
}
}
$Table.= "</table>";
return $Table;
}
?>
答案 0 :(得分:1)
如果您希望它作为每行的最后一列:
<?php
function SQLResultTable($Query)
{
$link = mysql_connect("localhost","root" , "") or die('Could not connect: ' . mysql_error()); //build MySQL Link
mysql_select_db("dbName") or die('Could not select database'); //select database
$Table = ""; //initialize table variable
$Table.= "<table border='1' style=\"border-collapse: collapse;\">"; //Open HTML Table
$Result = mysql_query($Query); //Execute the query
if(mysql_error())
{
$Table.= "<tr><td>MySQL ERROR: " . mysql_error() . "</td></tr>";
}
else
{
//Header Row with Field Names
$NumFields = mysql_num_fields($Result);
$Table.= "<tr style=\"background-color: #000066; color: #FFFFFF;\">";
for ($i=0; $i < $NumFields; $i++)
{
$Table.= "<th>" . mysql_field_name($Result, $i) . "</th>";
}
$Table.= "<th>Checkbox column</th>";
$Table.= "</tr>";
//Loop thru results
$RowCt = 0; //Row Counter
while($Row = mysql_fetch_assoc($Result))
{
//Alternate colors for rows
if($RowCt++ % 2 == 0) $Style = "background-color: #FFCCCC;";
else $Style = "background-color: #FFFFFF;";
$Table.= "<tr style=\"$Style\">";
//Loop thru each field
foreach($Row as $field => $value)
{
$Table.= "<td>      $value      </td>";
$Table.= "<td><input type="checkbox" name="nameHere" value="valueHere" ></td>";
}
$Table.= "</tr>";
}
}
$Table.= "</table>";
return $Table;
}
?>
虽然生成的代码非常难看 - 内联样式,呃。 -_-
答案 1 :(得分:0)
这是一个非常简单的逻辑,对不起,我不使用您的完整代码:
<强>的index.php:强>
<html>
<head>
<title>your page</title>
</head>
<body>
<form method="post">
<table>
<caption>myTable</caption>
<thead>
<tr>
<th>column with checkbox</th>
<th>column with text</th>
</tr>
</thead>
<tbody>
<?php
// get your databaseresult to an array called $result
$connection = mysql_connect("server", "user", "password");
mysql_select_database("databasename");
$resultHash = mysql_query("SELECT * FROM mytable");
while($row = mysql_fetch_array($resultHash)){
$result[] = $row;
}
mysql_close($connection); // never forget to close the connection if not longer needed
foreach($result as $key => $value)
{
echo "<tr>\r\n";
echo " <td><input type=\"checkbox\" name=\"yourCheckboxName".$key."\" /></td>\r\m";
// $key is the index of your numeric $result array
echo " <td>".$value[0]."</td>\r\n";
echo "</tr>\r\n";
}
?>
<tbody>
</table>
</form>
</body>
<html>
这就是我想要做的所有事情,请与mysql_fetch_array()
合作
http://php.net/manual/en/function.mysql-fetch-array.php
它更快
我希望代码是正确的,如果不是通过使用$result
查看数组$value
和var_dump()
是什么样的。没有测试它,并没有写PHP 4周或类似的东西
编辑: 昨晚我犯了一个错误,对不起,这是一个小小的纠正。
我认为您的数据库表设计就像。
table(id int auto_increment,varchar(255)not null,primary key(id))
<?php
foreach($result as $index => $row){
echo "<tr>";
echo "<td>";
echo "<input type='checkbox' name='yourname".$index."' />"; // now every checkbox has an unique identifier
echo "</td>";
foreach($row as $column => $value){
echo "<td>";
echo "column = ".$column;
echo "\r\n";
echo "value = ".$value;
echo "<td>";
}
echo "</tr>";
}
?>