Mixed Bag of HTML and PHP
In my PHP page, I am extracting a bunch of variables from the URL, and formatting their output into a nice HTML table. One section in the table needs to be dynamically created, dependent upon what was ordered on the previous webpage. Finally, I'm using the $mail function to send the HTML table with all the info to an email recipient.
The table works great, EXCEPT for the dynamic section with the while loop. The compiler is getting confused because my syntax is wrong. I suspect this is because my code is inside the $messa开发者_C百科ge'...' variable. Any advice?
<?php
// Extracting the variables from URL
$params = $_SERVER['QUERY_STRING'];
// Placing the variables into $array
$array=array();
parse_str($params,$array);
// Identifying the length of the main array and creating an array of KEYS
$keys = array_keys($array);
$keysCount = count($keys);
// And creating an array of corresponding values
$values = array_values($array);
$to = "steve@dutchlandfrozenfoods.com";
$subject = "NEW EUROCLASSIC ORDER";
$message = '
<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>EuroClassic Fine Family of Pastries - Ordering</title>
</HEAD>
<BODY bgcolor="#F0EFEE">
<table id="hor-minimalist-b" summary="Customer Information">
<thead>
<tr>
<th scope="col">Customer Contact:</th>
<th scope="col">Shipping To:</th>
</tr>
</thead>
<tbody>
<tr>
<td>' . $values[0] . '</td>
<td>' . $values[1] . '</td>
</tr>
<tr>
<td>' . $values[2] . '</td>
<td>' . $values[3] . '</td>
</tr>
<tr>
<td>' . $values[4] . '</td>
<td>' . $values[5] . '</td>
</tr>
<tr>
<td></td>
<td>' . $values[6] . '</td>
</tr>
</tbody>
</table>
<table id="hor-minimalist-b" summary="Order Details">
<thead>
<tr>
<th scope="col">Product:</th>
<th scope="col">Item Code:</th>
<th scope="col">Quantity:</th>
<th scope="col">Ext Price:</th>
</tr>
</thead>
<tbody>
while ($i = 13; $i < $keysCount-3; $i = $i+2;)
{
<tr>
<td>' . $values[$i] . '</td>
$i = $i+1;
<td>' . $values[$i] . '</td>
$i = $i+1;
<td>' . $values[$i] . '</td>
$i = $i+1;
<td>' . $values[$i] . '</td>
</tr>
}
</tbody>
</table>
</BODY>
</HTML>
';
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "To:Steve <steve@dutchlandfrozenfoods.com>\r\n";
mail($to, $subject, $message, $headers);
?>
Prepare the tbody in a variable before.
$tbody = '';
while ($i = 13; $i < $keysCount-3; $i = $i+2;) {
$tbody .= '<tr><td>' . $values[$i] . '</td>';
$i = $i+1;
$tbody .= '<td>' . $values[$i] . '</td>';
$i = $i+1;
$tbody .= '<td>' . $values[$i] . '</td>';
$i = $i+1;
$tbody .= '<td>' . $values[$i] . '</td></tr>';
}
And then:
$message = "...<tbody>" . $tbody . "</tbody> ....";
But I must say, the logic here isn't understandable, and if something changes, for example, count of columns, code could become quite hard to maintain.
Briedis point is important and necessary. but more simply - that is not how a while loop works
while(exp)
{
//body runs while exp remains true
}
You don't put three expressions up there like you do in a for loop. If you want to keep it as a while loop, do
$i = 13
while($i < $keysCount-3)
{
//body, with necessary increments
}
And if you decide to do a for loop instead, there is no semi-colon after the third (generally the incrementing) expression
Whichever method of loop you use, you need to take it out of the string as Briedis indicated. either make a temp variable such as $tbody
or repeatedly concatenate to $message
in the loop body.
As far as I know, you cannot call a PHP function and set it equal to a variable at the same time. you would need to do something similar to the following:
'... <tbody>';
while ($i = 13; $i < $keysCount-3; $i = $i+2;)
{
$message = '<tr><td>' . $values[$i] . '</td>';
$i = $i+1;
$message = '<td>' . $values[$i] . '</td>';
$i = $i+1;
$message = '<td>' . $values[$i] . '</td>';
$i = $i+1;
$message = '<td>' . $values[$i] . '</td></tr>';
}
$message = '</tbody> ...'
精彩评论