对Mysql表在网页上显示时显示的内容进行排序

时间:2015-01-10 22:05:44

标签: php mysql

我有一个数据库,其中有PHP表单向此数据库表添加内容。然后我有一个显示表格的页面,这样人们可以看到数据。所有条目都是时间。

这方面的缺点是有很多空格或00:00:00,这使得屏幕看起来有点混乱。

在插入时间之前,有没有办法隐藏任何列?

这是我的代码

<?php
$db_host = 'localhost';
$db_user = '*********';
$db_pwd = '*******';

$database = '********';
$table = 'checkpoints';

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 border='1'><tr>";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
    $field = mysql_fetch_field($result);
    echo "<td>{$field->name}</td>";
}
echo "</tr>\n";
// printing table rows
while($row = mysql_fetch_row($result))
{
    echo "<tr>";

    // $row is array... foreach( .. ) puts every element
    // of $row to $cell variable
    foreach($row as $cell)
        if $cell="<td>00:00:00</td>" then echo"<td></td>" Else echo"<td>$cell</td>";

echo "</tr>\n";
}
mysql_free_result($result);
?>

2 个答案:

答案 0 :(得分:0)

您有此代码

 foreach($row as $cell)
    if $cell="<td>00:00:00</td>" then echo"<td></td>" Else echo"<td>$cell</td>";

$cell无法获得值<td>00:00:00</td>。另外,您使用的是一个等号,将该值分配给$cell

试试这个。

 foreach ( $row as $cell ) {
    if ( '00:00:00' == $cell ) {
        echo "<td></td>";
    } else {
        echo "<td>$cell</td>";
    }

当您从正在调试的php程序中获得空白浏览器屏幕时,请始终尝试查看源代码...

答案 1 :(得分:0)

好的...我现在已经开始工作了,感谢Ollie,Proved真有帮助

经过一些调整后,我发现了一个胭脂{这导致我的wsod

这是我更改的结束代码

foreach ($row as $cell) if ('00:00:00'==$cell){echo "<td></td>";} else {echo "<td>$cell</td>";}