根据查询结果在 html 表中生成复选框
我正在尝试生成一个每行都有复选框的表。我在下面找到了一个工作代码,用于根据查询结果生成表。是否可以在此处插入一段代码,该代码将提供一个额外的列,该列将在每行中包含复选框?
<?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;
}
?>
I'm trying to generate a table with checkboxes on each row. I have found a working code below for generating a table based on query results. Is it possible to insert a code in here which will provide an extra column which will contain checkboxes in each row?
<?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;
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您希望它作为每行的最后一列:
虽然生成的代码确实很难看 - 内联样式,呃。 -_-
If you want it as the last column in each row:
Though the code's that's generated is really ugly - inline styles, ugh. -_-
这是一个非常简单的逻辑,对不起,我不使用您的完整代码:
index.php:
我认为这就是您需要做的所有事情,请使用
mysql_fetch_array()< /代码>
http://php.net/manual/en/function.mysql-fetch-array.php
它甚至更快我希望代码是正确的,如果不正确看看数组
$result< /code> 和
$value
就像使用var_dump()
一样。没有测试它,也没有写 php 4 周或类似的东西编辑:
昨晚犯了一个错误,抱歉,这里稍微纠正一下。
我认为你的数据库表设计是这样的。
表(id int auto_increment,某些varchar(255)不为空,主键(id))
its a very simply logic, excuse me i don't use your complete code:
index.php:
That is simply all you need to do I think, and please work with
mysql_fetch_array()
http://php.net/manual/en/function.mysql-fetch-array.php
it's even fasterI hope the code is correct, if not look what the arrays
$result
and$value
are like by usingvar_dump()
. Didn't test it and didn't write php 4 weeks or something like thisEdit:
I did a mistake last night, sorry, here is a little correction.
I think your database table design is like.
table(id int auto_increment, something varchar(255) not null, primary key(id))