我需要有关此代码的帮助。标头标记中的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> </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> </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','','');
因此,电子邮件报告可以工作,但是当收件人收到电子邮件报告时,空白行将不会显示在电子邮件报告中。我非常感谢您的帮助。谢谢。
答案 0 :(得分:0)
如果可以解决您的问题,请尝试使用以下代码。
$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> </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> </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>