Guest User

Untitled

a guest
Oct 19th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. <?php
  2. $all_table = 'all_statistics';
  3. $all_by_sub_id_table = 'all_by_sub_id_statistics';
  4. $db = 'torrents';
  5. $pass = '';
  6. $host = 'localhost';
  7. $user = 'root';
  8.  
  9. function update_statistics($date) {
  10. global $all_by_sub_id_table;
  11. $query =
  12. "SELECT DATE(p.`payment_date`) AS the_date, ".
  13. "u.`sub_id`, ".
  14. "SUM(cost) AS cost, ".
  15. "SUM(u.`subscribed`) AS registered_cnt, ".
  16. "SUM(CASE WHEN (DATE (u.`subscribed_at`) = DATE (payment_date)) THEN 1 ELSE 0 END) AS subscriptions_cnt, ".
  17. "COUNT(u.`unsubscribed_at`) AS unsubscriptions_cnt, ".
  18. "SUM(`pay_status`) AS rebills ".
  19. "FROM ".
  20. "payments p JOIN users u ON p.`user_id` = u.user_id ".
  21. "WHERE DATE(payment_date) = '$date' ".
  22. "GROUP BY u.`sub_id` ";
  23.  
  24.  
  25. $result = mysql_query($query);
  26.  
  27. if (!$result) return;
  28.  
  29. while ($row = mysql_fetch_assoc($result)) {
  30. $s = "SELECT * FROM `$all_by_sub_id_table` WHERE the_date = '${row['the_date']}' AND sub_id = '{$row['sub_id']}'";
  31. $res = mysql_query($s);
  32. if (mysql_numrows($res) == 0) {
  33. //если нету добавить
  34. $sql = "INSERT INTO `$all_by_sub_id_table` (the_date, sub_id, cost, registered_cnt, subscriptions_cnt, unsubscriptions_cnt, rebills) ".
  35. "VALUES ('{$row['the_date']}','{$row['sub_id']}','{$row['cost']}',{$row['registered_cnt']},{$row['subscriptions_cnt']},{$row['unsubscriptions_cnt']},{$row['rebills']})";
  36. mysql_query($sql);
  37. } else {
  38. // иначе обновить
  39. $sql = "UPDATE `$all_by_sub_id_table` SET the_date = '{$row['the_date']}', sub_id = '{$row['sub_id']}', cost = '{$row['cost']}', registered_cnt = {$row['registered_cnt']}, subscriptions_cnt = {$row['subscriptions_cnt']}, unsubscriptions_cnt = {$row['unsubscriptions_cnt']}, rebills = {$row['rebills']} WHERE (the_date = '${row['the_date']}') AND (sub_id = '{$row['sub_id']}')";
  40. $r = mysql_query($sql) or die(mysql_error());
  41. }
  42. }
  43.  
  44. update_day_total_statistics($date);
  45. }
  46.  
  47.  
  48. function update_day_total_statistics($date = null) {
  49. global $all_table;
  50.  
  51. if ($date == null) $date = date('Y-m-d');
  52.  
  53. $day_statistics_query = "SELECT the_date, SUM(registered_cnt) as registered_cnt, SUM(subscriptions_cnt) as subscriptions_cnt, SUM(unsubscriptions_cnt) as unsubscriptions_cnt, SUM(rebills) as rebills, SUM(cost) as cost FROM all_by_sub_id WHERE the_date = '$date'";
  54.  
  55. $result = mysql_query($day_statistics_query);
  56.  
  57. $day_statistics = mysql_fetch_assoc($result);
  58.  
  59. if ($day_statistics['the_date'] == null) return;
  60.  
  61. $s = "SELECT * FROM `$all_table` WHERE the_date = '$date'";
  62.  
  63. $result = mysql_query($s);
  64. // Если нету в статистике, то добавить
  65. if (mysql_numrows($result) == 0) {
  66.  
  67. $query = "INSERT INTO `$all_table` ".
  68. "(the_date, cost, registered_cnt, subscriptions_cnt, unsubscriptions_cnt, rebills) ".
  69. "VALUES ('{$day_statistics['the_date']}','{$day_statistics['cost']}', {$day_statistics['registered_cnt']}, {$day_statistics['subscriptions_cnt']} , {$day_statistics['unsubscriptions_cnt']}, {$day_statistics['rebills']})";
  70. $result = mysql_query($query);
  71. } else {
  72. //иначе обновить
  73. mysql_query("UPDATE `$all_table` SET cost = '{$day_statistics['cost']}', registered_cnt = {$day_statistics['registered_cnt']}, subscriptions_cnt = {$day_statistics['subscriptions_cnt']}, unsubscriptions_cnt = {$day_statistics['unsubscriptions_cnt']}, rebills = {$day_statistics['rebills']} WHERE the_date = {$day_statistics['the_date']}");
  74. }
  75. }
  76.  
  77. function fill_statistics_tables() {
  78. $sql = "SELECT DISTINCT DATE(first_visit_at) FROM users";
  79. $result = mysql_query($sql) or die (mysql_error());
  80. while ($row = mysql_fetch_array($result)) {
  81. update_statistics($row[0]);
  82. update_day_total_statistics($row[0]);
  83. }
  84. }
  85. mysql_connect($host,$user,$pass);
  86. @mysql_select_db($db) or die( "Unable to select database");
  87. //update_statistics();
  88. fill_statistics_tables();
Add Comment
Please, Sign In to add comment