Jump to content

finding the count() of months


paddy_fields
Go to solution Solved by Barand,

Recommended Posts

Hi, I'm trying to make a bar chart that shows how many views a job has per month. For the chart i need 12 seperate variables with the count for each stored... like $countJanuary, $countFebruary etc.

 

I've managed to work this out so far,

$query = "SELECT COUNT(*) AS `month_count`, DATE_FORMAT(viewDate, '%m') AS `month` FROM jobViews GROUP BY `month` ORDER BY `month` ASC";
$result = $db->query($query);

while($row = $result->fetch_assoc()) {
    
  echo $row['month']." - ".$row['month_count'];
  echo "</br>";

}

This produces the following (for testing purposes) but it obviously misses out months that have no matches...

01 - 3
03 - 1
05 - 1
12 - 7

I need the query to assign 0 to months with no matching results, so for February '02 = 0'

 

Does any have any advice on the logic of how I can get to the stage where I have (as in the example above) $countJanuary = 3, $countMarch = 1 etc..

 

Any help would be great

 

Link to comment
Share on other sites

one way

$sql = "CREATE TEMPORARY TABLE months (month int)";
$db->query($sql);

$sql = "INSERT INTO months VALUES (1),(2),(3),(4),(5),(6),(7),(,(9),(10),(11),(12)";
$db->query($sql);

$query = "SELECT m.month , COUNT(*) AS `month_count` 
FROM months
    LEFT JOIN jobViews ON m.month = MONTH(viewDate)
GROUP BY month";
Link to comment
Share on other sites

Thank you very much, that's such a great idea of how to do it.

 

I've tried to implement your code I'm getting no output when I print_r the $row? Am I handling this wrong? I'll read up more and play about with the code tomorrow night when I've actually slept, but for now this is my attempt!

$sql = "CREATE TEMPORARY TABLE months (month int)";
$db->query($sql);

$sql = "INSERT INTO months VALUES (1),(2),(3),(4),(5),(6),(7),(,(9),(10),(11),(12)";
$db->query($sql);

$query = "SELECT m.month , COUNT(*) AS `month_count` 
FROM months
    LEFT JOIN jobViews ON m.month = MONTH(viewDate)
GROUP BY month";

if($result = $db->query($query)){
  $row = $result->fetch_assoc();
}

print_r($row);

I'm getting a notice saying $row is an undefined variable

Edited by paddyfields
Link to comment
Share on other sites

You get this notice because $row is not defined.. By this I mean, it is ONLY defined in your IF statement and no where else, put $row=null; above your if statement and your notice will be gone.

 

The 'm' would throw an SQL error, not a PHP Notice.

Edited by Zane
Link to comment
Share on other sites

Yes, thank you it was the 'm' that was causing the problem - I should have noticed that.

 

The query is now running, but for some reason if there are 'no views' on a certain month then the count is 1 instead of 0?

 

In my test data I only have views for January - April. The counts on these are correct (they're not 1 too many), but the rest should say 0

$sql = "CREATE TEMPORARY TABLE months (month int)";
$db->query($sql);

$sql = "INSERT INTO months VALUES (1),(2),(3),(4),(5),(6),(7),(,(9),(10),(11),(12)";
$db->query($sql);

$query = "SELECT month , COUNT(*) AS `month_count` 
FROM months
    LEFT JOIN jobViews ON month = MONTH(viewDate)
GROUP BY month";

$result = $db->query($query);
  while($row = $result->fetch_assoc()){
    echo $row['month']." - ".$row['month_count']."<br>";
}
 

Output:

 

1 - 5
2 - 3
3 - 3
4 - 1
5 - 1
6 - 1
7 - 1
8 - 1
9 - 1
10 - 1
11 - 1
12 - 1

 

I understand how the code works but can't see why 5-12 would have a count of 1??

Edited by paddyfields
Link to comment
Share on other sites

  • Solution

Plan B if you need an array of the totals

$totals = array_fill_keys(range(1,12), 0);

$query = "SELECT MONTH(viewDate) as month , COUNT(*) AS `month_count` 
        FROM jobViews
        GROUP BY month";

$result = $db->query($query);
  while($row = $result->fetch_assoc()){
    $totals[$row['month']] = $row['month_count'];
}

echo '<pre>',print_r($totals, true),'</pre>';

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.