【问题标题】:Formatting MySQL data output格式化 MySQL 数据输出
【发布时间】:2011-10-31 05:41:03
【问题描述】:

我正在使用this PHP sn-p 将 MySQL 表转换为 HTML 表,它看起来像这样 [html]:

/----------------------------------------\
| 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 秒?

【问题讨论】:

  • 你试过什么?如果您熟悉 PHP 和 MySQL,应该很简单。对于 #1,请尝试阅读 sn-p COMMENTS。
  • 很遗憾这是一篇文章,没有评论区:(
  • 代码中有cmets!
  • link facepalm

标签: php mysql html-table


【解决方案1】:

这是根据您的要求量身定制的代码:

// 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>';

【讨论】:

  • @Shef 是一个很棒的人。我也从他那里得到了很多帮助。他是百万分之一 :)
【解决方案2】:

关于日期,试试这样的:

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

【讨论】:

    【解决方案3】:

    您可以使用此代码删除列名。这与您发布的链接中的 sn-ps 相同,但删除了一些行。这也会以分钟为单位打印持续时间。

    <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>
    

    【讨论】:

      【解决方案4】:

      1.) 删除// printing table headers之后的for循环。

      2.) 使用 date()strtotime() 作为“订阅”列(如果您使用第二个) mysql_fetch_row())

      3.) 将第三列除以 60。

      但我建议使用mysqli_fetch_array() 而不是mysqli_fetch_row()

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-19
        • 1970-01-01
        • 2021-05-15
        • 2018-07-19
        相关资源
        最近更新 更多