如何为 html <th> table header 标签使用数据库 table header
How to use database table header for html <th> table header tag
我是 PHP 的新手。我使用代码(如下)将 MySQL table 打印为 HTML table。
但是我的代码打印了数据库中的 table header。
如何使用 <th>header</th>
标签以硬编码格式打印 HTML table header 并打印 [=] 中的所有行33=]?
谢谢!
<?php
$db_host = 'localhost';
$db_user = 'my user';
$db_pwd = 'my pwd';
$database = 'my db';
$table = 'subcontractor';
if (!mysql_connect($db_host, $db_user, $db_pwd))
die("Can't connect to database");
if (!mysql_select_db($database))
die("Can't select database");
// sending query
$result = mysql_query("SELECT * FROM {$table}");
if (!$result) {
die("Query to show fields from table failed");
}
$fields_num = mysql_num_fields($result);
echo "<table class='table table-bordered table-striped mb-none' id='datatable-tabletools' data-swf-path='assets/vendor/jquery-datatables/extras/TableTools/swf/copy_csv_xls_pdf.swf' >";
// printing table headers
echo "<thead>";
for($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($result);
echo "<th>{$field->name}</th>";
}
echo "</thead>";
// printing table rows
while($row = mysql_fetch_row($result))
{
echo "<tbody>";
echo "<tr>";
echo "</thead>";
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell)
echo "<td>$cell</td>";
echo "</tr>";
echo "</tbody>";
}
mysql_free_result($result);
?>
您可以通过删除这些代码行来更改 headers。
for($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($result);
echo "<th>{$field->name}</th>";
}
例如将上面几行替换为:
echo "<thead>";
echo "<th>My Header 1</th>";
echo "<th>My Header 2</th>";
echo "</thead>";
Thank you works perfect. Is there a way to make PHP skip a column from the table on the DB? Thanks –
是的,只需更改查询
$result = mysql_query("SELECT * FROM {$table}");
例如
$result = mysql_query("SELECT `name`,`email`,`address` FROM {$table}");
我是 PHP 的新手。我使用代码(如下)将 MySQL table 打印为 HTML table。
但是我的代码打印了数据库中的 table header。
如何使用 <th>header</th>
标签以硬编码格式打印 HTML table header 并打印 [=] 中的所有行33=]?
谢谢!
<?php
$db_host = 'localhost';
$db_user = 'my user';
$db_pwd = 'my pwd';
$database = 'my db';
$table = 'subcontractor';
if (!mysql_connect($db_host, $db_user, $db_pwd))
die("Can't connect to database");
if (!mysql_select_db($database))
die("Can't select database");
// sending query
$result = mysql_query("SELECT * FROM {$table}");
if (!$result) {
die("Query to show fields from table failed");
}
$fields_num = mysql_num_fields($result);
echo "<table class='table table-bordered table-striped mb-none' id='datatable-tabletools' data-swf-path='assets/vendor/jquery-datatables/extras/TableTools/swf/copy_csv_xls_pdf.swf' >";
// printing table headers
echo "<thead>";
for($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($result);
echo "<th>{$field->name}</th>";
}
echo "</thead>";
// printing table rows
while($row = mysql_fetch_row($result))
{
echo "<tbody>";
echo "<tr>";
echo "</thead>";
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell)
echo "<td>$cell</td>";
echo "</tr>";
echo "</tbody>";
}
mysql_free_result($result);
?>
您可以通过删除这些代码行来更改 headers。
for($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($result);
echo "<th>{$field->name}</th>";
}
例如将上面几行替换为:
echo "<thead>";
echo "<th>My Header 1</th>";
echo "<th>My Header 2</th>";
echo "</thead>";
Thank you works perfect. Is there a way to make PHP skip a column from the table on the DB? Thanks –
是的,只需更改查询
$result = mysql_query("SELECT * FROM {$table}");
例如
$result = mysql_query("SELECT `name`,`email`,`address` FROM {$table}");