2011-08-20 53 views
2

我将MySQL表为使用this PHP代码段HTML表格,它看起来是这样的[HTML]:格式化MySQL数据输出

/----------------------------------------\ 
| Name | Subscribed   | Duration | <- Column names 
|----------------------------------------| 
| Bob | 2011-08-20 04:07:01 | 60  | 
|----------------------------------------| 
| Ben | 2011-08-20 04:07:01 | 260  | 
\----------------------------------------/ 
  1. 我怎么能不输出列名的表? (名称,订阅,期限)从Bob开始。
  2. 是否有可能将该日期转换为如下所示:2011年8月20日4:07:01?
  3. 持续时间以秒为单位,我怎样才能在PHP中将它转换为分钟(Duration(s)/ 60),以便它显示1而不是60秒?
+0

你尝试过什么?如果您熟悉PHP和MySQL,应该很简单。对于#1,请尝试阅读片段COMMENTS。 –

+0

不幸的是它是一篇文章,没有评论部分:( – Timur

+1

有意见的代码! –

回答

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

谢谢你,男人!你用勺子喂我:D – Timur

+1

@Shef是一个很棒的家伙,我也得到了他很多的帮助,他是百万分之一:) –

0

关于日期,尝试这样的事情:

echo strftime("%F %d, %Y %g:%i:%S", strtotime($v["datum"]));

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>