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 = "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);

?>
4

3 回答 3

3

之前在变量中准备 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> ....";

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

于 2011-03-25T15:18:03.373 回答
1

Briedis 点是重要且必要的。但更简单 - 这不是 while 循环的工作方式

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

您不会像在 for 循环中那样将三个表达式放在那里。如果你想保持它作为一个while循环,做

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

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

无论您使用哪种循环方法,都需要按照 Briedis 的指示将其从字符串中取出。要么在循环体中创建一个临时变量,$tbody要么重复连接。$message

于 2011-03-25T15:27:54.843 回答
0

据我所知,您不能同时调用 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> ...'
于 2011-03-25T15:22:18.360 回答