简体   繁体   English

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

[英]If array element is between 2 dates echo text PHP

I have the following PHP code that I'm aiming to create an HTML email with that should output the details of all models with an expiry date landing between 2 set dates: 我有以下旨在创建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
}

What I'm stuck on is how to only output the combination of the header, table and footer if a model exists in the table where the date is between 2 dates ie 1st May 2014 and 31st May 2014. It's currently outputting all dates. 我要坚持的是,如果表中存在一个日期介于2个日期(即2014年5月1日至2014年5月31日)之间的模型,则如何仅输出页眉,表和页脚的组合。它当前正在输出所有日期。

I've tried various ways to do this but after about 6 hours of failure I'm about ready to throw my computer out the window. 我尝试了各种方法来执行此操作,但是在大约6个小时的故障后,我准备将计算机扔出窗户。 I only have a few weeks experience with PHP (and programming altogether for that matter) so it may (hopefully) be a simple fix that I'm unaware of. 我只有几周的PHP使用经验(以及与此有关的编程知识),因此(希望)它是我不知道的简单修复方法。

NOTE: I should note that with the current database I'm using to test this I could amend the select query to output the relevant dates but on the actual database I'm using (which I only have access to at work) there is no warranty field and I have to extract the information from the product's long description via php and feed this into a function. 注意:我应该注意,使用当前数据库进行测试时,我可以修改select查询以输出相关日期,但是在使用的实际数据库上(我只能在工作中访问)保修字段,我必须通过php从产品的详细描述中提取信息,并将其提供给函数。

Final code: 最终代码:

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

Inside your foreach loop, just do an if statement to check if that $row of data is within your date range, if it is then do your echoing, otherwise, do nothing. 在您的foreach循环内,只需执行一条if语句来检查$ row数据是否在您的日期范围内,如果是,则执行您的回显,否则,不执行任何操作。

EG: 例如:

foreach ($models as $index => $model) {

    if ($years[$index] >=  DATE('2014-05-01') && $years[$index] <=  DATE('2014-05-31')){

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

}

It seems a bit strange that you're aggregating a lot of stuff in your SQL query and then disaggregating it in your PHP. 您在SQL查询中汇总了很多内容,然后在PHP中对其进行了分解,这似乎有些奇怪。 This is making it hard for you to match dates, I believe. 我认为,这使您难以匹配日期。

You could try this non-aggregated query: 您可以尝试以下非汇总查询:

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,

This will give you back the detail records, the ones that match the date range. 这将为您返回详细记录,这些记录与日期范围匹配。

It's quite easy, I think, to then process that result set with php. 我认为,使用php处理结果集非常容易。 You'll need to detect the change in customer name to output the headers and footers. 您需要检测客户名称的更改以输出页眉和页脚。 That's what $previous_name is about. 这就是$ 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 */
}    

See how this goes? 看看情况如何? You request a detail resultset from SQL, and then use some programming techniques in PHP to divide that result set customer-by-customer. 您从SQL请求一个详细的结果集,然后使用PHP中的某些编程技术按客户划分该结果集。

That's easier and more reliable than getting a GROUPed result set and then trying to ungroup it. 这比获得GROUPED结果集然后尝试对其进行取消分组更容易和更可靠。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM