的情况
我有一个MySQL数据库表,里面有很多日期。
现在我需要从这个表条目创建一个表,它应该看起来像这样:
May | |
22.05.2014 | Person 1 | Person 2
23.05.2014 | Person 1 | Person 2
June | |
02.06.2014 | Person 1 | Person 2
25.06.2014 | Person 1 | Person 2
等等。。
代码
我现在正试图创建一个遍历所有查询结果的循环。
我有这样的东西:
在"while"(获取assoc)循环中,我有这样的:
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$date = $row['pp_date'];
$month = date('m', strtotime($date));
$entryarray = array();
}
您可以看到,我首先将日期的月份保存到数组中。现在我不知道如何创建一个数组来保存数组中每个月的所有行,从而最终将结果输出到表中。
我假设您的表格报告只使用一年。如果是这样,您可以将月份用作数组的索引键。
$table = array();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$month = date('m', strtotime($row['pp_date']));
if(!isset($table[$month]))
{
$table[$month] = array();
}
$table[$month][] = $row; //... add a table row
}
现在,您可以通过迭代数组的键来回显$table
数组。它们将按照创建顺序进行分组。所以这取决于您的SQL查询。
foreach($table as $month=>$rows)
{
echo $month;
foreach($rows as $row)
{
print_r($row);
}
}
您可以尝试以下操作:
$entryarray = array();
$stmt="";//you statement here
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
{
$date = $row['pp_date'];
$month = date('m', strtotime($date));
$entryarray[$month][] =$row['data1'];//populate your array by the required data indexed by the month
$entryarray[$month][] =$row['data2'];
$entryarray[$month][] =$row['data3'];
}
然后,为了获得你的数据,你可以做这样的事情:
foreach ($entryarray as $key => $value) {
print $key; //you will get the key of your array
foreach ($value as $key => $value){print $value; }//the value of the required key
}