【问题标题】:Parsing empty rows from MySQL table and output as html table without empty cells从 MySQL 表中解析空行并输出为没有空单元格的 html 表
【发布时间】:2016-04-16 17:25:43
【问题描述】:

我一直在尝试创建一个 PHP 函数,该函数可以使用 PHP 将 MySQL 表显示为 HTML 表。到目前为止,我能够输出我选择的任何表,但是当 MySQL 表包含空行时遇到问题,因为空单元格会生成 HTML 表。我的代码是这样的:

<?php
function getTABLE(){
$db_host = 'HOST.com';
$db_user = 'USER1';
$db_pwd = 'PASSWORD';
$database = 'testdb';
$table = 'FAQTable';  

 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 and only result cell that are not NULL
$result = mysql_query("SELECT * FROM {$table}");

 if (!$result) {
    die("Query to show fields from table failed");
 }

$fields_num = mysql_num_fields($result);
echo "<h3><center>Table: {$table}</h3>";
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>";
 }

  //// 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)
        echo "<td>$cell</td>";
     echo "</tr>";
 }
 print "</TABLE>";
 mysql_close();
 }

 print getTABLE();

?>  

我的困境在于代码的“打印表格行”部分。我希望while($row = mysql_fetch_row($result)) 中有一种方法只接受其中包含值的行。有什么想法吗?

我已经尝试过使用以下几行,但没有成功:

$result = mysql_query("SELECT * FROM {$table} WHERE * IS NOT NULL");
$result = mysql_query("SELECT COUNT(id) FROM {$table} where answer IS NOT NULL or answer <>'' ");
$result = mysql_query('SELECT COUNT(*) FROM {$table} WHERE answer <> ""');
$result = mysql_query("SELECT * FROM {$table} WHERE CHAR_LENGTH>0");   
$result = mysql_query("SELECT * FROM {$table} WHERE val1 is <> '' "); 
$result = mysql_query("SELECT * FROM {$table} WHERE col1 is <> '' "); 

//// Outputs funky count in a separate table, but not the desired table with no empty cells
$result = mysql_query("SELECT COUNT(answer) FROM {$table} WHERE CHAR_LENGTH(answer)>0");   
$result = mysql_query("SELECT COUNT(answer) FROM {$table} WHERE LENGTH(answer)>0");   

$reslts = mysql_query("SELECT * FROM {$table}");
 while($row = mysql_fetch_row($reslts)){
     $empty_count = 0;
     $count = count($row);
      for($i = 0; $i < $count; $i++)
           if($row[$i] === '' || $row[$i] === 'NULL')
              $empty_count++;
         $result = ($count);
  }

感谢 Paul Spiegal 提供了这个 PHP 函数,它可以将任何 MySQL 表输出到 HTML 表中以显示在网站上... 工作函数如下,只需更改变量的值即可访问任何MySQL 数据:

function getTABLE(){
$db_host = 'www.host.com';
$db_user = 'user1';
$db_pwd = 'password';
$database = 'testdb';
$table = 'MyTable';  

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 "<h3><center>Table: {$table}</h3>";
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>";
 }

//// printing table rows
 while($row = mysql_fetch_row($result)){
    echo "<tr>";
        if (strlen(implode('', $row )) == 0) {
            continue;
       }else {
          foreach($row as $cell)
            echo "<td>$cell</td>";
            echo "</tr>";
        }
 }
print "</TABLE>";
mysql_close();
}

print getTABLE();

【问题讨论】:

  • 什么是“空行”?所有的 feald 都是 NULL?
  • 你知道表格字段。所以在 不为 null 的地方设置一个条件。 Not * 不为空。
  • 抱歉,我所说的“空行”是指行/记录是一个 mysql 表,它要么为 NULL,要么由于输入错误而为空。
  • 我应该补充一点,这个函数被设计用于未知的 3rd 方表,其中记录和字段是未知的,并且执行循环以查找该信息。

标签: php html mysql arrays if-statement


【解决方案1】:

使用 implode() 函数,您可以将所有单元格组合成一个字符串。如果该字符串为空,则跳过打印该行。

while($row = mysql_fetch_row($result)){
    if (strlen(implode('', $row)) == 0) {
        continue; // skip this empty row
    } else {
        // TODO: print this row
    }
}

【讨论】:

  • 这正是我需要的功能,并且确实知道它是否存在或它被称为什么。非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-01-18
  • 1970-01-01
  • 2014-08-10
  • 2014-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多