从MySQL表中解析空行并输出为没有空单元格的html表

时间:2016-04-16 08:44:10

标签: php html mysql arrays if-statement

我一直在尝试创建一个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();

1 个答案:

答案 0 :(得分:1)

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

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