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 に答える