The situation
I have a MySQL Database Table with many dates in it.
Now I need to create a table from this tableentries which should look like this:
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
etc..
The code
I am now trying to create a loop through all my query results to do so. And right there I stuck.
I am having something like this:
In the "while" (fetch assoc) loop I have this:
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$date = $row['pp_date'];
$month = date('m', strtotime($date));
$entryarray = array();
}
You can see, that I first save the month of the date into the array. And now I don't know, how to create the array to hold all rows for each month in the array to finally output the result into the table.
I assume your table report is for just one year. If so, you can just use the month as an index key on an 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
}
You can now echo the $table
array by iterating over the keys of the array. They will be grouped in the order they were created. So that depends upon your SQL query.
foreach($table as $month=>$rows)
{
echo $month;
foreach($rows as $row)
{
print_r($row);
}
}
You can try something like this:
$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'];
}
then to get your data you can do something like this:
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
}