HTML 和 PHP 的混合包
在我的 PHP 页面中,我从 URL 中提取了一堆变量,并将它们的输出格式化为一个漂亮的 HTML 表。表中的一个部分需要动态创建,具体取决于上一网页上订购的内容。最后,我使用 $mail 函数将包含所有信息的 HTML 表发送给电子邮件收件人。
该表工作得很好,除了带有 while 循环的动态部分。编译器感到困惑,因为我的语法错误。我怀疑这是因为我的代码位于 $message'...' 变量内。有什么建议吗?
<?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 = "[email protected]";
$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 <[email protected]>\r\n";
mail($to, $subject, $message, $headers);
?>
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 $message'...' 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 = "[email protected]";
$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 <[email protected]>\r\n";
mail($to, $subject, $message, $headers);
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
之前在变量中准备好 tbody。
然后:
但我必须说,这里的逻辑是不可理解的,如果发生变化,例如列数,代码可能会变得很难维护。
Prepare the tbody in a variable before.
And then:
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点很重要而且必要。但更简单的是,这不是 while 循环的工作原理。
您不会像在 for 循环中那样放置三个表达式。如果您想将其保留为 while 循环,请执行 do
如果您决定执行 for 循环,则第三个(通常是递增)表达式后面没有分号
无论您使用哪种循环方法,都需要采用它正如 Briedis 所指出的那样。创建一个临时变量,例如
$tbody
或在循环体中重复连接到$message
。Briedis point is important and necessary. but more simply - that is not how a while loop works
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
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.据我所知,你不能在调用 PHP 函数的同时将其设置为等于变量。您需要执行类似于以下操作的操作:
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: