如果数组元素在2个日期之间,则回显文本PHP

用户名

我有以下旨在创建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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

PHP在元素之间回显逗号

来自分类Dev

php代码回显两个日期之间的所有日期

来自分类Dev

PHP wordpress如果文本值为空,则回显

来自分类Dev

无法在PHP中回显数组中元素的值

来自分类Dev

PHP回显纯文本

来自分类Dev

从PHP中的数组中回显一个项目

来自分类Dev

php数组回显if语句

来自分类Dev

PHP 回显数组与串联

来自分类Dev

比较2个数组并在php中回显数组相同内容的值的最佳方法是什么?

来自分类Dev

来自 2 个不同会话的 PHP 回显头链接

来自分类Dev

php会话,如果语句回显

来自分类Dev

PHP,如果页面等于则回显DIV

来自分类Dev

PHP foreach 回显 X 个元素并计算剩余的元素数量

来自分类Dev

PHP在2个日期之间选择数据

来自分类Dev

2个日期之间的PHP SQL查询

来自分类Dev

如何获得2个日期之间的日期数组?

来自分类Dev

如果关联值不在数组中,PHP如何回显某些结果?

来自分类Dev

php回显一些文本;

来自分类Dev

在Windows Batch文件中,如何回显或索引数组中的第(n + 1)个元素?

来自分类Dev

在回显表的同时从数组中获取第7个元素

来自分类Dev

PHP,从多维数组中的多个关联数组中回显一个特定值

来自分类Dev

如何从日期数组中回显单个日期?

来自分类Dev

PHP回显数组值而不循环

来自分类Dev

如何回显从php发布的多维数组

来自分类Dev

PHP:如何回显返回数组的方法?

来自分类Dev

用PHP从数组中回显数据

来自分类Dev

用PHP多维数组的结果回显

来自分类Dev

PHP回显数组并将其拆分

来自分类Dev

来自数组的对象的PHP回显值