Advertisement
Vladimir3261

Untitled

Dec 28th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.25 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: vovas
  5.  * Date: 12/28/2017
  6.  * Time: 1:43 PM
  7.  */
  8.  
  9. $data = [
  10.     [
  11.         "id" => 1,
  12.         "users" => 100,
  13.     ], [
  14.         "id" => 2,
  15.         "users" => 108,
  16.     ], [
  17.         "id" => 3,
  18.         "users" => 114,
  19.     ], [
  20.         "id" => 4,
  21.         "users" => 98,
  22.     ], [
  23.         "id" => 5,
  24.         "users" => 105,
  25.     ], [
  26.         "id" => 6,
  27.         "users" => 110,
  28.     ], [
  29.         "id" => 7,
  30.         "users" => 140,
  31.     ], [
  32.         "id" => 8,
  33.         "users" => 141,
  34.     ],
  35. ];
  36.  
  37.  
  38. function getPercent($prev, $current)
  39. {
  40.     return ($current - $prev) / $prev * 100;
  41. }
  42.  
  43. function getPrev($data, $currentIndex, $backNumber)
  44. {
  45.     $index = $currentIndex - $backNumber;
  46.     return $index > 0 ? $data[$index] : $data[0];
  47. }
  48.  
  49. $totalUsers = $data[0]['users'];
  50. foreach ($data as $index => &$item) {
  51.     $item['per_day'] = getPercent($totalUsers, $item['users']);
  52.     $back3Days = getPrev($data, $index, 2);
  53.     $back7Days = getPrev($data, $index, 6);
  54.     $item['per_3_days'] = getPercent($back3Days['users'], $item['users']);
  55.     $item['per_7_days'] = getPercent($back7Days['users'], $item['users']);
  56.     $totalUsers = $item['users'];
  57. }
  58. ?>
  59. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
  60.       integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
  61. <table class="table">
  62.     <thead>
  63.     <tr>
  64.         <th>#</th>
  65.         <th>Total users</th>
  66.         <th>Per day</th>
  67.         <th>Per 3 days</th>
  68.         <th>Per 7 days</th>
  69.     </tr>
  70.     </thead>
  71.     <tbody>
  72.     <?php foreach ($data as $d) { ?>
  73.         <?php
  74.         $perDay = round($d['per_day'], 2);
  75.         $per3Days = round($d['per_3_days'], 2);
  76.         $per7Days = round($d['per_7_days'], 2);
  77.         ?>
  78.         <tr>
  79.             <th scope="row"><?= $d['id'] ?></th>
  80.             <td><?= $d['users'] ?></td>
  81.             <td class="<?= $perDay > 0 ? 'success' : 'danger' ?>"><?= $perDay ?> %</td>
  82.             <td class="<?= $per3Days > 0 ? 'success' : 'danger' ?>"><?= $per3Days ?> %</td>
  83.             <td class="<?= $per7Days > 0 ? 'success' : 'danger' ?>"><?= $per7Days ?> %</td>
  84.  
  85.         </tr>
  86.     <?php } ?>
  87.     </tbody>
  88. </table>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement