HTML 和 PHP 的混合包

发布于 2024-10-27 06:17:44 字数 2906 浏览 0 评论 0原文

在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

红衣飘飘貌似仙 2024-11-03 06:17:44

之前在变量中准备好 tbody。

$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>';
}  

然后:

$message  = "...<tbody>" . $tbody . "</tbody> ....";

但我必须说,这里的逻辑是不可理解的,如果发生变化,例如列数,代码可能会变得很难维护。

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.

淡淡绿茶香 2024-11-03 06:17:44

Briedis点很重要而且必要。但更简单的是,这不是 while 循环的工作原理。

while(exp)
{
    //body runs while exp remains true
}

您不会像在 for 循环中那样放置三个表达式。如果您想将其保留为 while 循环,请执行 do

$i = 13
while($i < $keysCount-3)
{
    //body, with necessary increments
}

如果您决定执行 for 循环,则第三个(通常是递增)表达式后面没有分号

无论您使用哪种循环方法,都需要采用它正如 Briedis 所指出的那样。创建一个临时变量,例如 $tbody 或在循环体中重复连接到 $message

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.

南风几经秋 2024-11-03 06:17:44

据我所知,你不能在调用 PHP 函数的同时将其设置为等于变量。您需要执行类似于以下操作的操作:

'... <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> ...'

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> ...'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文