Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [MySQL][PHP]Grupowanie danych według roku i miesiąca - Datetime
Forum PHP.pl > Forum > Przedszkole
Tidude
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. }


b4rt3kk
Jak już musisz tak, to zrób tablicę 3 wymiarową:

  1. while($row = mysql_fetch_assoc($result)) {
  2. $year = $row['year'];
  3. $month = $row['month'];
  4. $ilosc = $row['ilosc'];
  5. $storage_array[$year][$month] = $ilosc;
  6. }
  7.  
  8. foreach ($storage_array as $year => $item) {
  9.  
  10. foreach ($item as $month => $item1) {
  11. echo $year . '-' . $month . ': ' . $item1 . '<br/>';
  12. }
  13.  
  14. }
Tidude
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 -->
b4rt3kk
To inaczej zbuduj strukturę swojej tablicy:

  1. foreach ($storage_array as $year => $item) {
  2.  
  3. foreach ($item as $month => $item1) {
  4. $output[] = array('year' => $year, 'month' => $month, 'value' => $item1);
  5. }
  6.  
  7. }


I teraz będzie Ci to łatwiej sobie wyświetlać.
Tidude
Dalej nie mogę sobie poradzić z tym by wyświetlało mi w tabeli 12 miesięcy, i te w których nikt się nie zarejestrował by pokazywało wynik 0.

Znalazłem pewien kod, który niby rozwiązuje mój problem. Lecz nie mogę dojść z nim do ładu, nie wiem jak wykonać do niego zapytanie.


Chciałbym otrzymać właśnie taką tabelę jak ta poniżej.
  1. |Jan | Feb | Mar | Apr | May | Jun | Jul | Aug | Sep | Oct | Nov | Dec |
  2. --------+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  3. 2001 | 0 | 0 | 0 | 0 | 0 | 0 | 100$ | 0 | 0 | 0 | 0 | 50$ |
  4. --------+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  5. 2002 | 30$ | 0 | 0 | 0 | 90$ | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
  6. --------+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  7. 2003 | 0 | 0 | 0 | 0 | 0 | 80$ | 0 | 0 | 20$ | 0 | 0 | 0 |


Ja bym chciał, żeby zamiast dolarów wyświetlało mi ilość zarejestrowanych użytkowników w danym miesiącu, mam tabelę "uzytkownicy" z kolumną "data_rejestracji".

  1. $array = array ("7/2001:100$", "12/2001:50$" , "1/2002:30$" , "5/2002:90$" , "6/2003:80$","9/2003:20$" );
  2.  
  3. $prefill = array_fill(1,12,0);
  4. $years_array = array();
  5.  
  6. foreach($array as $value){
  7. list($date , $amount) = explode(":" , $value);
  8. list($month , $year) = explode("/" , $date);
  9.  
  10. if(!$years_array[$year]) $months_data = $prefill;
  11. else $months_data = $years_array[$year];
  12.  
  13. $months_data[$month] = $amount;
  14. $years_array[$year] = $months_data;
  15. }
  16.  
  17. echo "| Jan | Feb | Mar | Apr | May | Jun | Jul | Aug | Sep | Oct | Nov | Dec | <br>";
  18. ksort($years_array);
  19. foreach($years_array as $year => $year_row){
  20. echo $year." | ";
  21. echo implode(" | " , $year_row);
  22. echo "<br>";
  23. }
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.