【问题标题】:Hide empty row in an email report隐藏电子邮件报告中的空行
【发布时间】:2018-12-10 00:20:50
【问题描述】:

我需要有关此代码的帮助。标头标记中的 javascript 代码和样式用于隐藏空行,因为并非所有行都包含数据,并且在我的网页上显示时有效。 但是,当我使用它将报告作为报告发送到 电子邮件地址 时,会显示空行。 我需要考虑什么让它隐藏收件人电子邮件中的空行。 下面是代码:

enter$sql = mysqli_query($con_path, "SELECT * FROM classscore WHERE emailsent=0 LIMIT 20");
$numRows = mysqli_num_rows($sql); 
$mail_body = '';
while($row = mysqli_fetch_array($sql)){
$studentid = $row['student_id'];
$classname = $row['class_name'];
$english  =  $row['english'];
$maths = $row['maths'];
$accounts = $row['accounts'];
$music = $row['music'];
$email = $row['email'];

$mail_body = '<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Report</title>
<style>
.hide { 
display: none; 
}
</style>
</head>

 <body>
 <table id="table" width="40%" border="1" align="center" cellpadding="0" cellspacing="0" style="border-radius:10px;-moz-border-radius:10px;-webkit-border-radius:10px; color:#000;">
<tr>
<td width="23%">Student ID</td>
<td>&nbsp;</td>
<td> '.$studentid.' / '.$classname.'</td>
</tr>
<tr>
<td><strong>Subjects</strong></td>
<td width="21%"><strong>Overall Score</strong></td>
<td width="56%"><strong>Actual Score</strong></td>
</tr>
<tr>
<td>English</td>
<td>100</td>
<td> '.$english.'</td>
</tr>
<tr>
<td>Maths</td>
<td>100</td>
<td>'.$maths.'</td>
</tr>
<tr>
<td>Account</td>
<td>100</td>
<td> '.$accounts.'</td>
</tr>
<tr>
<td>Music</td>
<td>100</td>
<td> '.$music.'</td>
</tr>
</table>
<p>&nbsp; </p>
<script language="javascript">
var tbl = document.getElementById("table");
var rows = tbl.querySelectorAll("tbody tr");

for(i = 0; i < rows.length; i++) {

var cells = rows[i].querySelectorAll("td");

var flag = true;
//this number controls the colunm the code will check before it disappears.
for(j = 2; j < cells.length; j++) {
    if (cells[j].innerHTML != 0) { 
        flag = false; 
    }
}


if(flag) { 
    rows[i].classList.add("hide"); 
}
}                                      
</script>
</body>
</html>';
$subject = "Report";
$headers = 'MIME-Version: 1.0';
$headers  = "From:schoolreport@pearlgateconsult.com\r\n";
$headers .= "Content-type: text/html\r\n";
$to = "$email";

$mail_result = mail($to, $subject, $mail_body, $headers);

if ($mail_result) {
    mysqli_query($con_path, "UPDATE classscore SET emailsent=1 WHERE email='$email' LIMIT 1");
} else {

}

}

?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<?php echo $mail_body; ?>
<body>
</body>
</html>

DROP TABLE IF EXISTS classscore ;
CREATE TABLE classscore
(student_id INTEGER (11) NULL,
email varchar(255),
class_name varchar(255),
english INTEGER (11) NULL,
maths INTEGER (11) NULL,
accounts INTEGER (11) NULL,
music INTEGER (11) NULL,
emailsent varchar(255) DEFAULT '0'
);

INSERT INTO classscore
(student_id,email,class_name,english,maths,accounts,music) VALUES
(10011,'tosinsog@gmail.com','Basic1','70','80','75',''),
(10012,'tosinsog@gmail.com','Basic1','55','','','69'),
(10013,'tosinsog@gmail.com','Basic1','74','75','','85'),
(10014,'tosinsog@gmail.com','Basic1','64','84','58',''),
(10015,'tosinsog@gmail.com','Basic1','69','','65','73'),
(10016,'tosinsog@gmail.com','Basic1','70','80','74','68'),
(10017,'tosinsog@gmail.com','Basic1','75','82','','');

因此电子邮件报告有效,但当收件人收到电子邮件报告时,空行不会显示在电子邮件报告中。我非常感谢您的帮助。谢谢。

【问题讨论】:

    标签: javascript php css email


    【解决方案1】:

    如果可以解决您的问题,请尝试使用以下代码。

     $mail_body = '<html xmlns="http://www.w3.org/1999/xhtml">
     <head>
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     <title>Report</title>
     <style>
      .hide { 
         display: none; 
       }
      </style>
     </head>
    
     <body>
       <table id="table" width="40%" border="1" align="center" cellpadding="0" cellspacing="0" style="border-radius:10px;-moz-border-radius:10px;-webkit-border-radius:10px; color:#000;">
      <tr>
         <td width="23%">Student ID</td>
         <td>&nbsp;</td>
         <td> '.$studentid.' / '.$classname.'</td>
      </tr>
      <tr>
         <td><strong>Subjects</strong></td>
         <td width="21%"><strong>Overall Score</strong></td>
         <td width="56%"><strong>Actual Score</strong></td>
      </tr>';
    
      if($english!=0){
    
      $mail_body.='<tr>
         <td>English</td>
         <td>100</td>
         <td> '.$english.'</td>
      </tr>'; }
    
      if($maths!=0) { 
       $mail_body.='<tr>
         <td>Maths</td>
         <td>100</td>
         <td>'.$maths.'</td>
      </tr>'; }
    
      if($accounts!=0){
      $mail_body.='<tr>
         <td>Account</td>
         <td>100</td>
         <td> '.$accounts.'</td>
      </tr>'; }
    
      if($music!=0){
      $mail_body.='<tr>
          <td>Music</td>
          <td>100</td>
          <td> '.$music.'</td>
      </tr>'; }
    $mail_body.='</table>
    <p>&nbsp; </p>
    <script language="javascript">
       var tbl = document.getElementById("table");
       var rows = tbl.querySelectorAll("tbody tr");
    
       for(i = 0; i < rows.length; i++) {
    
        var cells = rows[i].querySelectorAll("td");
    
        var flag = true;
        //this number controls the colunm the code will check before it disappears.
       for(j = 2; j < cells.length; j++) {
         if (cells[j].innerHTML != 0) { 
          flag = false; 
         }
        }
    
    
       if(flag) { 
          rows[i].classList.add("hide"); 
       }
     }                                      
     </script>
     </body>
     </html>';
     $subject = "Report";
     $headers = 'MIME-Version: 1.0';
     $headers  = "From:schoolreport@pearlgateconsult.com\r\n";
     $headers .= "Content-type: text/html\r\n";
     $to = "$email";
    
     $mail_result = mail($to, $subject, $mail_body, $headers);
    
      if ($mail_result) {
        mysqli_query($con_path, "UPDATE classscore SET emailsent=1 WHERE email='$email' LIMIT 1");
      }
    
    ?>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    </head>
    <?php echo $mail_body; ?>
    <body>
    </body>
    </html>
    

    【讨论】:

    • 谢谢,它没有工作,也没有发送报告。我要解决的问题是每个学生的报告在到达各自的邮箱时只显示带有分数的行。如果四行有分数,则显示所有四行,如果有两个分数,则显示两行,如果三个分数,则在他们通过电子邮件将分数发送给他们时显示三行。
    • 我已经对主要答案进行了更改,试试它是否适合你
    • 再次感谢,但它仍然在收件人的电子邮件中显示带有空 0 的行。报告被发送到 gmail,所以也许 gmail 不允许脚本隐藏空行。
    • 将 if ($english!='') 语句和任何其他语句更改为 if (!empty($english) || $english!= 0)
    • @shubhangee & @Gabor,谢谢你们,这些作品。这些都可以正常工作,将 if($english!='') 更改为 if (!empty($english) || $english!= 0) 或将 if($english!='') 更改为 if($english!= 0)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-23
    • 1970-01-01
    相关资源
    最近更新 更多