2016-08-17 39 views
0

我得到记录:日期比较,以从MySQL数据库与此PHP函数更新记录

function someFunction($date){ 
    // all distinct records 
    $query = "select count(distinct column_name) as alias from table_name where DATE(date_column) = '$date'"; 
    $result = $connection->query($query); 
    $row = $result->fetch_assoc(); 
    return $row['alias']; 
    // end of all distinct records 
} 

现在下面的PHP代码所做的是什么,得到了一天的日期,计算的一周它属于它的月份并将其存储为一个数组。

//while fetch_assoc returns records 
//$result1 query: "select * from table_name where DATE(date) between '$first_date' and date_add('$end_date',interval 1 day)" 

while ($row1 = $result1->fetch_assoc()) { 
    $date = $row1['date']; 
    $start = 1; 
    $end = 7; 
    for ($i = 1; $i <= 5; $i++) { 
    if ((int) date('d', strtotime($date)) >= $start && (int) date('d', strtotime($date)) <= $end) { 
     if (!isset($arr1[$i]) || !isset($arr2[$i])) { 
     $arr1[$i] = 0; 
     $arr2[$i] = 0; 
     } 
     ++$arr1[$i]; 

     $arr2[$i] = someFunction(date('Y-m-d', strtotime($date))); 
    } 
    $start += 7; 
    $end += 7; 
    } 
} 

考虑第一,第二和第三属于同一周,第一有3个记录,第二有4个和第3具有1 while循环将迭代7次,由someFunction返回的每个值()覆盖$arr2[$i]中的值。

所以我的问题是,我将如何能够检查以前的迭代日期值是否等于当前的日期值?

回答

0

所以我的问题是,我将如何能够检查以前的迭代日期值是否等于当前的日期值?

伪代码:

$lastValue = …; // initialization with a value that does not occur in the actual values, 
       // such as NULL, empty string, … 

while(…) { 
    if($currentValue == $lastValue) { 
    // do something 
    } 
    else { 
    // do something else 
    } 

    // … 

    $lastValue = $currentValue; // set current value for next loop interation 
}