我正在尝试使用 PHPwhile
循环计算我的 MySQL 表中某些日期的出现次数,并将日期和重复次数放在array
. 我能够正确地将日期添加到数组中,但我似乎无法添加重复次数。
例子:
function counter() {
//The 'timestamp' column uses the MySQL timestamp type
$query = mysql_query("SELECT timestamp FROM table1 ORDER BY timestamp DESC");
$date_c;
$counter = 0;
$date_array();
while($row = mysql_fetch_array($query)) {
//gets the year, month, and day from the timestamp
$year = substr($row['timestamp'], 0, 4);
$month = substr($row['timestamp'], 5, 2);
$day = substr($row['timestamp'], 8, 2);
$date = $month.'/'.$day.'/'.$year;
if($date == $date_c) {
$counter += 1;
} else {
array_push($date_array, $date, $counter);
$counter = 0;
}
$date_c = $date;
但是,当我echo
成为数组的一部分时,计数器不会更新。这是一个使用第一个重复日期的示例table1
:
>>> echo $date;
06/15/2012
>>> echo $counter;
25
>>> echo $date_array[0];
06/15/2012
>>> echo $date_array[1];
0
我已经玩了一段时间了,但我似乎找不到我的错误。有谁知道我做错了什么?