gpt4 book ai didi

php - 如果数组元素位于 2 个日期之间,则回显文本 PHP

转载 作者:行者123 更新时间:2023-11-30 00:19:45 24 4
gpt4 key购买 nike

我有以下 PHP 代码,我的目标是创建一封 HTML 电子邮件,该电子邮件应输出到期日期在 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 经验(以及与此相关的编程经验),因此它可能(希望)是一个我不知道的简单修复。

注意:我应该注意,使用我用来测试的当前数据库,我可以修改选择查询以输出相关日期,但在我正在使用的实际数据库上(我只能在工作中访问)没有保修字段,我必须通过 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 中的一些编程技术将该结果集逐个客户划分。

这比获取分组结果集然后尝试取消分组更容易、更可靠。

关于php - 如果数组元素位于 2 个日期之间,则回显文本 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23330395/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com