Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [MySQL][PHP]Grupowanie danych według roku i miesiąca - Datetime
Tidude
post
Post #1





Grupa: Zarejestrowani
Postów: 34
Pomógł: 0
Dołączył: 10.08.2011

Ostrzeżenie: (0%)
-----


Cześć.
Próbuję zrobić sobie tabelkę z informacjami o ilości zarejestrowanych użytkowników.
Chciałbym, żeby wyglądała ona tak:
  1. 2012:
  2. styczeń 10
  3. luty 5
  4. marzec 11


Gdzie liczby 10,5,11 to ilość osób zarejestrowanych w danym miesiącu.
Udało mi się napisać skrypt, który tworzy następującą tabelę.
  1. 2012:
  2. styczeń
  3. luty
  4. marzec

I mam problem z wyciągnięciem ilości użytkowników z danego miesiąca. Według poniższego kodu, pokazuje mi że w danym miesiącu zarejestrował się jeden użytkownik, choć było ich więcej. Doszedłem do wniosku, że to przez linijkę $ilosc = $row['ilosc']; bo dodałem ją w złym miejscu, a nie wiem jak ją dodać w innym.

  1. $query = "select count(*) as ilosc, DATE_FORMAT(data_rejestracji,'%Y-%m') as month, DATE_FORMAT(data_rejestracji,'%Y') as year FROM uzytkiwnicy GROUP BY month ORDER BY data_rejestracji";
  2. $result = mysql_query($query) or die(mysql_error());
  3.  
  4. $storage_array = array();
  5. while($row = mysql_fetch_assoc($result)) {
  6. $year = $row['year'];
  7. $month = $row['month'];
  8. $ilosc = $row['ilosc'];
  9. $storage_array[$year][] = $month;
  10. }
  11.  
  12. foreach ($storage_array as $year => $month_array){
  13. echo "<ul class='year'><li><a>{$year}</a>";
  14. foreach ($month_array as $month){
  15. echo "<ul class='months'><li><a>{$month} {$ilosc}</a></li></ul>
  16. ";
  17.  
  18. }
  19. echo "</li></ul>";
  20. }


Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
Tidude
post
Post #2





Grupa: Zarejestrowani
Postów: 34
Pomógł: 0
Dołączył: 10.08.2011

Ostrzeżenie: (0%)
-----


ten kod nie do końca wyświetla to co bym chciał, bo z tych danych chcę zrobić tabelkę.
I chcę żeby rok, miesiąc i ilość były wyświetlane oddzielnie.

Hmm, a wiesz jak inaczej to wyświetlić?

Podobny kod znalazłem w phpbb, ale nie wiem jak go przerobić by działał mi.
  1. if ( !defined('IN_PHPBB') )
  2. {
  3. die('Hacking attempt');
  4. }
  5.  
  6. $statistics_module = true;
  7.  
  8. /***************************************************************************
  9.  * module.php
  10.  * -------------------
  11.  * begin : Tuesday, Sep 03, 2002
  12.  * copyright : (C) 2002 Meik Sievertsen
  13.  * email : acyd.burn@gmx.de
  14.  *
  15.  ***************************************************************************/
  16.  
  17. /***************************************************************************
  18.  *
  19.  * This program is free software; you can redistribute it and/or modify
  20.  * it under the terms of the GNU General Public License as published by
  21.  * the Free Software Foundation; either version 2 of the License, or
  22.  * (at your option) any later version.
  23.  *
  24.  ***************************************************************************/
  25.  
  26. //
  27. // Modules should be considered to already have access to the following variables which
  28. // the parser will give out to it:
  29.  
  30. // $return_limit - Control Panel defined number of items to display
  31. // $module_info['name'] - The module name specified in the info.txt file
  32. // $module_info['email'] - The author email
  33. // $module_info['author'] - The author name
  34. // $module_info['version'] - The version
  35. // $module_info['url'] - The author url
  36. //
  37. // To make the module more compatible, please do not use any functions here
  38. // and put all your code inline to keep from redeclaring functions on accident.
  39. //
  40.  
  41. //
  42. // All your code
  43. //
  44. // New users by month
  45. //
  46.  
  47. if (!$statistics->result_cache_used)
  48. {
  49. // Init Cache -- tells the Stats Mod that we want to use the result cache
  50. $result_cache->init_result_cache();
  51.  
  52. $sql = "SELECT YEAR(FROM_UNIXTIME(user_regdate)) as aar, MONTH(FROM_UNIXTIME(user_regdate)) as mnd, COUNT(*) AS ant
  53. FROM " . USERS_TABLE . "
  54. WHERE (user_id <> " . ANONYMOUS . " )
  55. GROUP BY YEAR(FROM_UNIXTIME(user_regdate)), MONTH(FROM_UNIXTIME(user_regdate))
  56. ORDER BY user_regdate";
  57.  
  58. if ( !($result = $db->sql_query($sql)) )
  59. {
  60. message_die(GENERAL_ERROR, 'Couldn\'t retrieve users data', '', __LINE__, get_module_fd_name(__FILE__), $sql);
  61. }
  62.  
  63. $user_count = $db->sql_numrows($result);
  64. $user_data = $db->sql_fetchrowset($result);
  65.  
  66. for ($i = 0; $i < $user_count; $i=$i+$k)
  67. {
  68. $class = ( !($i+1 % 2) ) ? $theme['td_class2'] : $theme['td_class1'];
  69.  
  70. $year = $user_data[$i]['aar'];
  71. $k = 0;
  72. for ($j = 0; $j < 12; $j++)
  73. {
  74. $m[$j+1] = 0;
  75. }
  76. for ($j = 0; $j < 12; $j++)
  77. {
  78. if ($year == $user_data[$i+$j]['aar'])
  79. {
  80. $month = $user_data[$i+$j]['mnd'];
  81. $m[$month] = $user_data[$i+$j]['ant'];
  82. $k = $k + 1;
  83. }
  84. }
  85. $template->assign_block_vars('signup', array(
  86. 'CLASS' => $class,
  87. 'YEAR' => $year,
  88. 'M01' => $m[1],
  89. 'M02' => $m[2],
  90. 'M03' => $m[3],
  91. 'M04' => $m[4],
  92. 'M05' => $m[5],
  93. 'M06' => $m[6],
  94. 'M07' => $m[7],
  95. 'M08' => $m[8],
  96. 'M09' => $m[9],
  97. 'M10' => $m[10],
  98. 'M11' => $m[11],
  99. 'M12' => $m[12])
  100. );
  101.  
  102. $result_cache->assign_template_block_vars('signup');
  103. }
  104. }
  105. else
  106. {
  107. for ($i = 0; $i < $result_cache->block_num_vars('signup'); $i++)
  108. {
  109. // Method 1: We are assigning the block variables from the result cache to the template. ;)
  110. $template->assign_block_vars('signup', $result_cache->get_block_array('signup', $i));
  111.  
  112. }
  113.  
  114. }
  115. $template->assign_vars(array(
  116. 'L_SIGNUPBYMONTH' => $lang['Signup_month'],
  117. 'L_YEAR' => $lang['Year'],
  118. 'L_MONTH' => $lang['Month'],
  119. 'L_NUMBER' => $lang['Number'],
  120. 'L_JAN' => $lang['Month_jan'],
  121. 'L_FEB' => $lang['Month_feb'],
  122. 'L_MAR' => $lang['Month_mar'],
  123. 'L_APR' => $lang['Month_apr'],
  124. 'L_MAY' => $lang['Month_may'],
  125. 'L_JUN' => $lang['Month_jun'],
  126. 'L_JUL' => $lang['Month_jul'],
  127. 'L_AUG' => $lang['Month_aug'],
  128. 'L_SEP' => $lang['Month_sep'],
  129. 'L_OCT' => $lang['Month_oct'],
  130. 'L_NOV' => $lang['Month_nov'],
  131. 'L_DEC' => $lang['Month_dec'])
  132. );


A za wyświetlanie odpowiada .tpl
  1. <table border="0" width="100%" cellpadding="2" cellspacing="1" class="forumline">
  2. <tr>
  3. <td class="catHead" align="center" colspan="13">
  4. <span class="cattitle">{L_SIGNUPBYMONTH}</span>
  5. </td>
  6. </tr>
  7.  
  8. <tr>
  9. <th class="thCornerL" align="center" ><strong>{L_YEAR}</strong></th>
  10. <th class="thTop" align="center"><strong>{L_JAN}</strong></th>
  11. <th class="thTop" align="center"><strong>{L_FEB}</strong></th>
  12. <th class="thTop" align="center"><strong>{L_MAR}</strong></th>
  13. <th class="thTop" align="center"><strong>{L_APR}</strong></th>
  14. <th class="thTop" align="center"><strong>{L_MAY}</strong></th>
  15. <th class="thTop" align="center"><strong>{L_JUN}</strong></th>
  16. <th class="thTop" align="center"><strong>{L_JUL}</strong></th>
  17. <th class="thTop" align="center"><strong>{L_AUG}</strong></th>
  18. <th class="thTop" align="center"><strong>{L_SEP}</strong></th>
  19. <th class="thTop" align="center"><strong>{L_OCT}</strong></th>
  20. <th class="thTop" align="center"><strong>{L_NOV}</strong></th>
  21. <th class="thCornerR" align="center"><strong>{L_DEC}</strong></th>
  22. </tr>
  23.  
  24.  
  25. <!-- BEGIN signup -->
  26. <tr>
  27. <td class="{signup.CLASS}" align="center" ><span class="gen">{signup.YEAR}</span></td>
  28. <td class="{signup.CLASS}" align="center" valign="middle"><span class="gen">{signup.M01}</span></td>
  29. <td class="{signup.CLASS}" align="center" valign="middle"><span class="gen">{signup.M02}</span></td>
  30. <td class="{signup.CLASS}" align="center" valign="middle"><span class="gen">{signup.M03}</span></td>
  31. <td class="{signup.CLASS}" align="center" valign="middle"><span class="gen">{signup.M04}</span></td>
  32. <td class="{signup.CLASS}" align="center" valign="middle"><span class="gen">{signup.M05}</span></td>
  33. <td class="{signup.CLASS}" align="center" valign="middle"><span class="gen">{signup.M06}</span></td>
  34. <td class="{signup.CLASS}" align="center" valign="middle"><span class="gen">{signup.M07}</span></td>
  35. <td class="{signup.CLASS}" align="center" valign="middle"><span class="gen">{signup.M08}</span></td>
  36. <td class="{signup.CLASS}" align="center" valign="middle"><span class="gen">{signup.M09}</span></td>
  37. <td class="{signup.CLASS}" align="center" valign="middle"><span class="gen">{signup.M10}</span></td>
  38. <td class="{signup.CLASS}" align="center" valign="middle"><span class="gen">{signup.M11}</span></td>
  39. <td class="{signup.CLASS}" align="center" valign="middle"><span class="gen">{signup.M12}</span></td>
  40. </tr>
  41. <!-- END signup -->


Ten post edytował Tidude 2.07.2013, 00:00:54
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 6.10.2025 - 09:37