0

Below is my email.php. The code runs well until I try to calculate the number of rows for laptop warranty expired. The number of rows can be print out, but it does not contain the description for each laptop as stated in the code. May I know what should I do to fix this code ?

Edited :

For example, the content of email will be like this :

Dear IT Infra,

Total 125 of laptop are going to expire in less than 45 days.

Status : Loan

Laptop serial number: R8VMGFH

Date of expired:2013-01-17

Etc....(another 124 description of laptop)

It should displaying all these, but it does not now.

Email.php :

<?php
   // ======== get database data =======
   include 'MasConnectDB.php';

   $count = 0;
   $sql = "SELECT Lap_Status, Lap_SN, Lap_War_Expiry FROM laptop WHERE
          Lap_War_Expiry!='0000-00-00' AND Lap_War_Expiry!=''  ";
   $result = mysql_query( $sql ) or die('Query failed. ' . mysql_error() ); 

   while( $row = mysql_fetch_array( $result ) ) {
   $Lap_Status = $row['Lap_Status'];
   $Lap_SN = $row['Lap_SN'];
   $Lap_War_Expiry = $row['Lap_War_Expiry'];

   date_default_timezone_set("Asia/Kuala_Lumpur");
   $today_date = date('Y-m-d'); 

   $expiry_date = $Lap_War_Expiry; 
   $timestamp = strtotime($expiry_date);
   $warning_days = 45; 
   $seconds_diff = $warning_days * 24 * 3600; 
   $warning_timestamp = $timestamp - $seconds_diff;
   $warning_date = date('Y-m-d', $warning_timestamp);

   if ( $today_date >= $warning_date ) {
          $message =  $message." <div style='margin:30px 0px;'>
              Status : $Lap_Status<br />
              Laptop serial number: $Lap_SN<br />
              Date of expired:$Lap_War_Expiry <br /></div>";
      $count++;
   }    
    }
    $message ="Dear IT Infra,<br/><br /> Total $count of laptop are going to expire in
less than 45 days.<br /><br />";
    require_once('class.phpmailer.php');

    //Email goes here
Tepken Vannkorn
  • 9,648
  • 14
  • 61
  • 86
user2810332
  • 29
  • 2
  • 9
  • [Please, don't use mysql_* functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) in new code. They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about prepared statements instead, and use [pdo](https://wiki.php.net/rfc/mysql_deprecation) or [mysqli](http://stackoverflow.com/questions/tagged/mysqli). – zessx Oct 09 '13 at 07:30

1 Answers1

0

You can short-circuit your code by using the mysql datediff function.

SELECT Lap_Status, Lap_SN, Lap_War_Expiry
  FROM laptop
   WHERE Lap_War_Expiry = DATE(DATE_ADD(NOW(), INTERVAL 45 DAY))
crafter
  • 6,246
  • 1
  • 34
  • 46
  • I think you have misunderstand my question. I have edited my question. Hope you can see and able to help me. Thanks – user2810332 Oct 09 '13 at 07:32