我有以下旨在创建HTML电子邮件的PHP代码,该电子邮件应输出所有模型的详细信息,并在2个设置日期之间显示有效日期:
$sql = 'SELECT
GROUP_CONCAT(op.products_model),
GROUP_CONCAT(products.products_warranty_default),
GROUP_CONCAT(orders.date_purchased),
orders.customers_name
FROM orders_products op
INNER JOIN products
ON op.products_id = products.products_id
INNER JOIN orders
ON op.orders_id = orders.orders_id
INNER JOIN customers
ON orders.customers_id = customers.customers_id
GROUP BY op.orders_id';
$result = mysql_query($sql, $connection);
while ($data = mysql_fetch_row($result)) {
$models = explode(",",$data[0]); // create array from all models in the GROUP_CONCAT
$years = explode(",",$data[1]); // create array from all years in the GROUP_CONCAT
$dates = explode(",",$data[2]); // create array from all dates in the GROUP_CONCAT
$name = $data[3];
echo $header; // header of email (only the name changes on each loop)
foreach ($models as $index => $model) {
echo '
<tr>
<td width="149">
<center>'.$model.'</center>
</td>
<td width="149">
<center>'.date('d/m/Y', strtotime('+'.$years[$index].' years', strtotime($dates[$index]))).'</center>
</td>
</tr>'; // echos the model in first column and date+years in second column
}
echo $footer; // static HTML again like the header
}
我要坚持的是,如果表中存在一个日期介于2个日期(即2014年5月1日至2014年5月31日)之间的模型,则如何仅输出页眉,表和页脚的组合。它当前正在输出所有日期。
我已经尝试了各种方法来执行此操作,但是在大约6个小时的故障后,我准备将计算机扔到窗外。我只有几周的PHP使用经验(以及与此相关的编程知识),因此(希望)它是我不知道的简单修复方法。
注意:我应该注意,使用当前数据库进行测试时,我可以修改select查询以输出相关日期,但是在使用的实际数据库上(我只能在工作中访问)保修字段,我必须通过php从产品的详细描述中提取信息,并将其提供给函数。
最终代码:
$sql = 'SELECT op.products_model,
products.products_warranty_default,
orders.date_purchased,
orders.customers_name
FROM orders_products op
INNER JOIN products ON op.products_id = products.products_id
INNER JOIN orders ON op.orders_id = orders.orders_id
INNER JOIN customers ON orders.customers_id = customers.customers_id
ORDER BY orders.customers_name, op.orders_id,
op.products_model,
products.products_warranty_default,
orders.date_purchased';
$result = mysql_query($sql, $connection);
$previous_name = "";
while ($data = mysql_fetch_row($result)) {
$model = $data[0];
$year = $data[1];
$date = $data[2];
$name = $data[3];
$expiry = strtotime('+'.$year.' years', strtotime($date));
if ($previous_name != $name && $expiry >= strtotime('2014-05-01') && $expiry <= strtotime('2014-05-31')) { // on a new customer name ...
if ($previous_name != "") {
// if there is a previous customer, write the footer
echo $footer;
$previous_name = $name;
}
}
if ($expiry >= strtotime('2014-05-01') && $expiry <= strtotime('2014-05-31')){
echo '
<tr>
<td width="149">
<center>'.$model.'</center>
</td>
<td width="149">
<center>'.date('d/m/Y', $expiry).'</center>
</td>
</tr>'; // echos the model in first column and date+years in second column
}
}
if ( $previous_name != "" && $expiry >= strtotime('2014-05-01') && $expiry <= strtotime('2014-05-31')) {
echo $footer;
}
在SQL查询中聚集大量内容,然后在PHP中对其进行分类似乎有点奇怪。我认为,这使您难以匹配日期。
您可以尝试以下非汇总查询:
SELECT op.products_model,
products.products_warranty_default,
orders.date_purchased,
orders.customers_name
FROM orders_products op
INNER JOIN products ON op.products_id = products.products_id
INNER JOIN orders ON op.orders_id = orders.orders_id
INNER JOIN customers ON orders.customers_id = customers.customers_id
WHERE orders.date_purchased >= DATE('2014-05-01')
AND orders.date_purchased < DATE('2014-05-31') + INTERVAL 1 DAY
ORDER BY orders.customer_name, op.orders_id,
op.products_model,
products.products_warranty_default,
orders.date_purchased,
这将为您返回详细记录,这些记录与日期范围匹配。
我认为,使用php处理结果集非常容易。您需要检测客户名称的更改以输出页眉和页脚。这就是$ previous_name的含义。
$previous_name = "";
while ($data = mysql_fetch_row($result)) {
$model = $data[0];
$year = $data[1];
$date = $data[2];
$name = $data[3];
if ($previous_name != $name) { /* on a new customer name ... */
if ($previous_name != "") {
/* if there is a previous customer, write the footer */
echo $footer; /* static HTML again like the header */
}
/* write the header for the new customer */
echo $header;
/* pick up the new customer name */
$previous_name = $name;
}
echo '
<tr>
<td width="149">
<center>'.$model.'</center>
</td>
<td width="149">
<center>'.date('d/m/Y', strtotime('+'.$year.' years',
strtotime($date))).'</center>
</td>
</tr>'; /* echos the model in first column and date+years in second column */
}
} /* end while ($data... */
/* wrap up the report by writing the final footer if need be */
if ( $previous_name != "" ) {
echo $footer; /* static HTML again like the header */
}
看看这是怎么回事?您从SQL请求一个详细的结果集,然后使用PHP中的一些编程技术按客户划分该结果集。
这比获得GROUPED结果集然后尝试对其进行取消分组更容易和更可靠。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句