views:

225

answers:

4

Okay, basically... i am trying to store the dates and month name from my database in this format. I am aiming store the month and then count the entries in that month and then store the count. This is my loop, but I have trouble formatting the array properly.

while ( $row = mysql_fetch_assoc($res) ) {
 if ($row['Date'] != $prev_date) {
  $values=array(
   $row['Date'] => $count,
  );

  $prev_date = $row['Date'];

 }

 $count++;

}
print_f($values);

You can see that I will always overwrite my previous array and I am not really adding entries into the array. I couldn't figure out how to do it. I'm basically trying to see the number of entries per month.

OLD Update: Currently learning the MYSQL thing that one commenter mentioned. I'll update when I get it.

A: 

Not sure I understand what you're trying to do. How about this?

$values[$row['Date']]++;
Tim Sylvester
+2  A: 

You can make this using group by in sql, some like this: select date, count(*) from table group by date

Edit: If you need only count by month use some like this: select MONTH(dateField) as NewDate, count(*) FROM table GROUP BY NewDate

Cesar
If that's what you're trying to accomplish, it will be *much* more efficient to do it in the query than in PHP.
Tim Sylvester
Okay, let me try this instead
Doug
I couldn't get this to work the way I want to. I don't know when does the date start in my database and I need to count the amount of entries for each month. I can only count it the amount of each month if I can define WHERE Month = 'x'. Maybe I am missing something.
Doug
Do you need a counter for a year/month or only for a month? Show me the SQL.
Cesar
+1  A: 

I almost got it!

$values=array();
    while ( $row = mysql_fetch_assoc($res) ) {
     if ($row['Date'] != $prev_date) {
      $values[$row['Date']] =  $count;

      $prev_date = $row['Date'];
      $count = 0;
     }

     $count++;

    }

Update: Need a little help with this part.

My output:

Array ( [9] => [10] => 999 [11] => 986 )

999 was supposed to be stored in 9 and 986 was supposed to be stored in 10.

Update: Here's my new code, I would appreciate it if someone can show me a more efficient way.

 $sql = "SELECT MONTH(AddDate) AS Date, column_name FROM table ORDER BY AddDate ASC";
 $res = mysql_query($sql) or die(mysql_error());
 $prev_date = null;

$values=array();

    while ( $row = mysql_fetch_assoc($res) ) {
     if ( $row['Date'] != $prev_date) {
      $month = $row['Date'];
      $sql = "SELECT count(MONTH(AddDate)) AS EntryAmount FROM `table` WHERE MONTH(AddDate)=$month ";
      $countResults = mysql_query($sql) or die(mysql_error());
      if( $entryAmount = mysql_fetch_array($countResults) ) {
       $values[$row['Date']] =  $entryAmount['EntryAmount'];
      }
      $prev_date = $row['Date'];
     }
    }

Output:

Array ( [9] => 999 [10] => 986 [11] => 264 )
Doug
great for you! .
thephpdeveloper
Show us the original query?
Mark E
@Mark Updated the original query
Doug
A: 

How about looping on the result of a query something like this:

SELECT COUNT(MONTH(AddDate)) AS EntryAmount, MONTH(AddDate) as MonthValue FROM table GROUP BY MONTH(AddDate)
Mark E