Reputation: 9149
I have a function that generates an HTML table based on an SQL query. How can I store this whole table in a formatted string? Here is the code for the table. As a person on this forum suggested earlier, I would like to send this string as the body of an email. I think I have a good idea on how to do this, so help on the formatted string would be great!
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>";
}
Upvotes: 1
Views: 1783
Reputation: 11714
You should use Heredoc. Here's an example:
$text = <<<END
//insert whatever text needs to be inserted
<table></table>
END;
Now $text
will have everything stored as text properly formatted too and you can simply echo
it if you choose.
Upvotes: 1
Reputation: 2789
Instead of 'echo'ing, append your strings to a variable, and then echo that variable. You can then reuse that variable to send off an email or whatever else.
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;
}
Upvotes: 4