$query4 = "SELECT bill.agent_id, bill.date, SUM(bill.amount + bill.cheque) AS total, bill_agents.id,bill_agents.name ".
"FROM bill, bill_agents ".
"WHERE bill.agent_id = bill_agents.id AND YEAR(date) = YEAR(CURDATE()) AND MONTH(date) = MONTH(CURDATE()) ".
"GROUP BY bill.date , bill.agent_id ".
"ORDER BY bill.date ASC";
テーブル構造 : http://i.stack.imgur.com/38tKh.jpg
現在の結果
+---------+----+----+----+-----+----+
| | 1 | 2 | 3 | 4 | 5 |
+---------+----+----+----+-----+----+
| Agent 1 | 35 | 0 | 0 | 0 | 0 |
| Agent 2 | 0 | 10 | 0 | 0 | 0 |
| Agent 1 | 0 | 0 | 12 | 0 | 0 |
| Agent 3 | 0 | 0 | 0 | 100 | 0 |
| Agent 6 | 0 | 0 | 0 | 9 | 0 |
| Agent 2 | 0 | 0 | 0 | 9 | 14 |
+---------+----+----+----+-----+----+
しかし、私は次のようになりたい
+---------+----+----+----+-----+----+
| | 1 | 2 | 3 | 4 | 5 |
+---------+----+----+----+-----+----+
| Agent 1 | 35 | 0 | 12 | 0 | 0 |
| Agent 2 | 0 | 10 | 0 | 0 | 14 |
| Agent 3 | 0 | 0 | 0 | 100 | 0 |
| Agent 6 | 0 | 0 | 0 | 9 | 0 |
+---------+----+----+----+-----+----+
Phpコード
<?php
$query4 = "SELECT bill.agent_id, bill.date, SUM(bill.amount + bill.cheque) AS total, bill_agents.id,bill_agents.name ".
"FROM bill, bill_agents ".
"WHERE bill.agent_id = bill_agents.id AND YEAR(date) = YEAR(CURDATE()) AND MONTH(date) = MONTH(CURDATE()) ".
"GROUP BY bill.date , bill.agent_id ".
"ORDER BY bill.date ASC";
$result4 = mysql_query($query4) or die('Error, query failed1');
if (mysql_num_rows($result4)>0){
mysql_data_seek($result4, 0);
?>
<?php while($row4 = mysql_fetch_array($result4, MYSQL_ASSOC)){?>
<?php $date = $row4['date'];
$var = $date;
$date = date("d-m-Y", strtotime($var) );
$date=substr($date, 0, -8);
echo $date;
?>
<tr>
<td><?php echo $row4['name']; ?></td>
<td><?php if ($date == 01) echo $row4['total']; ?></td>
<td><?php if ($date == 02) echo $row4['total']; ?></td>
<td><?php if ($date == 03) echo $row4['total']; ?></td>
<td><?php if ($date == 04) echo $row4['total']; ?></td>
<td><?php if ($date == 05) echo $row4['total']; ?></td>
<td><?php if ($date == 06) echo $row4['total']; ?></td>
<td><?php if ($date == 07) echo $row4['total']; ?></td>
<td><?php if ($date == 08) echo $row4['total']; ?></td>
<td><?php if ($date == 09) echo $row4['total']; ?></td>
<td><?php if ($date == 10) echo $row4['total']; ?></td>