我正在使用this PHP代码段将MySQL表转换为HTML表格,它看起来像这样[html]:
/----------------------------------------\
| Name | Subscribed | Duration | <- Column names
|----------------------------------------|
| Bob | 2011-08-20 04:07:01 | 60 |
|----------------------------------------|
| Ben | 2011-08-20 04:07:01 | 260 |
\----------------------------------------/
答案 0 :(得分:2)
以下是根据您的要求量身定制的代码:
// 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 "<h1>Table: {$table}</h1>";
echo "<table border='1'>";
// printing table rows
while($row = mysql_fetch_assoc($result))
{
echo "<tr>";
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $key => $cell){
if($key == 'Subscribed'){
$cell = date('F j, Y H:i:s', strtotime($cell)); // format date August 20, 2011 4:07:01
} elseif($key == 'Duration'){
$cell = $cell/60; // format time in minutes
}
echo "<td>$cell</td>";
}
echo "</tr>\n";
}
echo '</table>';
答案 1 :(得分:0)
1。)删除// printing table headers
之后的for循环。
2。)使用date()和strtotime()作为“已订阅”列(如果您使用,则使用第二个) mysql_fetch_row())
3。)将第三列除以60。
答案 2 :(得分:0)
关于约会,请尝试以下方式:
echo strftime("%F %d, %Y %g:%i:%S", strtotime($v["datum"]))
;
答案 3 :(得分:0)
您可以使用此代码删除列名称。这与您发布的链接相同,但删除了一些行。这也将以分钟为单位打印持续时间。
<html><head><title>MySQL Table Viewer</title></head><body>
<?php
$db_host = 'localhost';
$db_user = 'root';
$db_pwd = 'lptm42b';
$database = 'sphinx';
$table = 'spheres';
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");
}
echo "<h1>Table: {$table}</h1>";
echo "<table border='1'>";
// printing table rows
while($row = mysql_fetch_assoc($result))
{
echo "<tr>";
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $field => $value)
{
if ($field === 'Duration')
{
$value = $value / 60;
}
echo "<td>$value</td>";
}
echo "</tr>\n";
}
mysql_free_result($result);
?>
</body></html>