I need to query data in order to display number of registered users for each day of the current month, in order to pass json array to jQuery Flot.
This is what I have so far:
for ($i = 1; $i <= date('t'); $i++) {
$date = date('Y') . '-' . date('m') . '-' . $i;
$sql = $db->first("SELECT COUNT(*) AS total FROM users WHERE (DATE(created) = '" . $date . "' LIMIT 1) GROUP BY DAY(created)");
($sql) ? $jdata['regs']['data'][] = array($i, (int)$sql->total) : $jdata['regs']['data'][] = array($i, 0);
$jdata['xaxis'][] = array($i, date('j', strtotime($date)));
}
print json_encode($jdata);
The above works fine, except that it makes 30 queries due to the for loop. I would like to be able to optimize it, by eliminating for loop and use it on $sql array instead.
SELECT day(created), COUNT(*) FROM users GROUP BY day(created);