1

我有一个基于 SQL 查询生成 HTML 表的函数。如何将整个表格存储在格式化的字符串中?这是表格的代码。正如这个论坛上的一个人之前建议的那样,我想将此字符串作为电子邮件的正文发送。我想我对如何做到这一点有一个好主意,所以对格式化字符串的帮助会很棒!

    function tableGen($x) {
$term=$x;
$sql = mysql_query("select * from student_info where ID like '$term'");
echo "<h1>STUDENT DATA for ID: $term</h1>";
echo "<table>";
echo "<tr>
<th>ID</th>
<th>Project</th>
<th>Starter Project</th>
<th>Course</th>
<th>KDs Completed in your Course</th>
<th>Projects Completed</th>
<th>Project 1</th>
<th>P1KD1</th>
<th>P1KD2</th>
<th>P1KD3</th>
<th>P1KD4</th>
<th>P1KD5</th>
<th>Project 2</th>
<th>P2KD1</th>
<th>P2KD2</th>
<th>P2KD3</th>
<th>P2KD4</th>
<th>P2KD5</th>
<th>Project 3</th>
<th>P3KD1</th>
<th>P3KD2</th>
<th>P3KD3</th>
<th>P3KD4</th>
<th>P3KD5</th>
<th>Project 4</th>
<th>P4KD1</th>
<th>P4KD2</th>
<th>P4KD3</th>
<th>P4KD4</th>
<th>P4KD5</th>
</tr>";

while ($row = mysql_fetch_array($sql))
{
echo "<tr><td>";
echo $row['ID'];
echo "</td><td>";
echo $row['Project'];
echo "</td><td>";
echo $row['Starter Project'];
echo "</td><td>";
echo $row['Course'];
echo "</td><td>";
echo $row['KDs completed in your course'];
echo "</td><td>";
echo $row['Projects Completed'];
echo "</td><td>";
echo $row['Project 1'];
echo "</td><td>";
echo $row['P 1 KD 1'];
echo "</td><td>";
echo $row['P 1 KD 2'];
echo "</td><td>";
echo $row['P 1 KD 3'];
echo "</td><td>";
echo $row['P 1 KD 4'];
echo "</td><td>";
echo $row['P 1 KD 5'];
echo "</td><td>";
echo $row['Project 2'];
echo "</td><td>";
echo $row['P 2 KD 1'];
echo "</td><td>";
echo $row['P 2 KD 2'];
echo "</td><td>";
echo $row['P 2 KD 3'];
echo "</td><td>";
echo $row['P 2 KD 4'];
echo "</td><td>";
echo $row['P 2 KD 5'];
echo "</td><td>";
echo $row['Project 3'];
echo "</td><td>";
echo $row['P 3 KD 1'];
echo "</td><td>";
echo $row['P 3 KD 2'];
echo "</td><td>";
echo $row['P 3 KD 3'];
echo "</td><td>";
echo $row['P 3 KD 4'];
echo "</td><td>";
echo $row['P 3 KD 5'];
echo "</td><td>";
echo $row['Project 4'];
echo "</td><td>";
echo $row['P 4 KD 1'];
echo "</td><td>";
echo $row['P 4 KD 2'];
echo "</td><td>";
echo $row['P 4 KD 3'];
echo "</td><td>";
echo $row['P 4 KD 4'];
echo "</td><td>";
echo $row['P 4 KD 5'];
echo "</td></tr>";
}

echo "</table>";
}
4

2 回答 2

4

而不是“回显”,将您的字符串附加到一个变量,然后回显该变量。然后,您可以重用该变量来发送电子邮件或其他任何内容。

function tableGen($x) {
$term=$x;
$sql = mysql_query("select * from student_info where ID like '$term'");
$output = "";
$output .=  "<h1>STUDENT DATA for ID: $term</h1>";
$output .=  "<table>";
$output .=  "<tr>
<th>ID</th>
<th>Project</th>
<th>Starter Project</th>
<th>Course</th>
<th>KDs Completed in your Course</th>
<th>Projects Completed</th>
<th>Project 1</th>
<th>P1KD1</th>
<th>P1KD2</th>
<th>P1KD3</th>
<th>P1KD4</th>
<th>P1KD5</th>
<th>Project 2</th>
<th>P2KD1</th>
<th>P2KD2</th>
<th>P2KD3</th>
<th>P2KD4</th>
<th>P2KD5</th>
<th>Project 3</th>
<th>P3KD1</th>
<th>P3KD2</th>
<th>P3KD3</th>
<th>P3KD4</th>
<th>P3KD5</th>
<th>Project 4</th>
<th>P4KD1</th>
<th>P4KD2</th>
<th>P4KD3</th>
<th>P4KD4</th>
<th>P4KD5</th>
</tr>";

while ($row = mysql_fetch_array($sql))
{
$output .=  "<tr><td>";
$output .=  $row['ID'];
$output .=  "</td><td>";
$output .=  $row['Project'];
$output .=  "</td><td>";
$output .=  $row['Starter Project'];
$output .=  "</td><td>";
$output .=  $row['Course'];
$output .=  "</td><td>";
$output .=  $row['KDs completed in your course'];
$output .=  "</td><td>";
$output .=  $row['Projects Completed'];
$output .=  "</td><td>";
$output .=  $row['Project 1'];
$output .=  "</td><td>";
$output .=  $row['P 1 KD 1'];
$output .=  "</td><td>";
$output .=  $row['P 1 KD 2'];
$output .=  "</td><td>";
$output .=  $row['P 1 KD 3'];
$output .=  "</td><td>";
$output .=  $row['P 1 KD 4'];
$output .=  "</td><td>";
$output .=  $row['P 1 KD 5'];
$output .=  "</td><td>";
$output .=  $row['Project 2'];
$output .=  "</td><td>";
$output .=  $row['P 2 KD 1'];
$output .=  "</td><td>";
$output .=  $row['P 2 KD 2'];
$output .=  "</td><td>";
$output .=  $row['P 2 KD 3'];
$output .=  "</td><td>";
$output .=  $row['P 2 KD 4'];
$output .=  "</td><td>";
$output .=  $row['P 2 KD 5'];
$output .=  "</td><td>";
$output .=  $row['Project 3'];
$output .=  "</td><td>";
$output .=  $row['P 3 KD 1'];
$output .=  "</td><td>";
$output .=  $row['P 3 KD 2'];
$output .=  "</td><td>";
$output .=  $row['P 3 KD 3'];
$output .=  "</td><td>";
$output .=  $row['P 3 KD 4'];
$output .=  "</td><td>";
$output .=  $row['P 3 KD 5'];
$output .=  "</td><td>";
$output .=  $row['Project 4'];
$output .=  "</td><td>";
$output .=  $row['P 4 KD 1'];
$output .=  "</td><td>";
$output .=  $row['P 4 KD 2'];
$output .=  "</td><td>";
$output .=  $row['P 4 KD 3'];
$output .=  "</td><td>";
$output .=  $row['P 4 KD 4'];
$output .=  "</td><td>";
$output .=  $row['P 4 KD 5'];
$output .=  "</td></tr>";
}

$output .=  "</table>";
echo $output;
}
于 2013-04-30T02:57:03.537 回答
1

你应该使用Heredoc。这是一个例子:

$text = <<<END
//insert whatever text needs to be inserted
<table></table>
END;

现在$text将所有内容都存储为格式正确的文本echo,如果您愿意,您可以简单地使用它。

于 2013-04-30T02:57:11.740 回答