Advertisement
Guest User

Leper's Colony - Day of month checker

a guest
Jan 29th, 2014
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.16 KB | None | 0 0
  1. <?php
  2. /*
  3.     filename: days.php
  4.     [there's a <form> tag with the action "days.php" (this file) so replace that if you use another filename]
  5.     don't host this on your website, it's not protected against SQL injection
  6.  */
  7. // this line is eventually used to convert month numbers into month names
  8. $month_array = array(
  9.         "1"  => "January",
  10.         "2"  => "February",
  11.         "3"  => "March",
  12.         "4"  => "April",
  13.         "5"  => "May",
  14.         "6"  => "June",
  15.         "7"  => "July",
  16.         "8"  => "August",
  17.         "9"  => "September",
  18.         "10" => "October",
  19.         "11" => "November",
  20.         "12" => "December",
  21. );
  22. if (!is_numeric($_GET['width']))
  23. {
  24.     $width = 500; // if you specify a non-numeric value for width, it defaults to 500
  25. }
  26. elseif($_GET['width'] == "")
  27. {
  28.     $width = 500; // same if you specify nothing at all
  29. }
  30. else
  31. {
  32.     $width = $_GET['width']; // this is when you specify width
  33. }
  34. ?>
  35. <html>
  36. <head>
  37. <title>Leper's Colony &bull; Day of month checker</title>
  38. <style type="text/css">
  39. body
  40. {
  41.     background-color: black;
  42.     color: white;
  43. }
  44. div
  45. {
  46.     height: 22px;
  47. }
  48. </style>
  49. </head>
  50. <body>
  51. <?php
  52. // now to create forms
  53. // there's a lot of C/P code so things won't be explained again
  54.     // Connect to MySQL database
  55.     $db = mysqli_connect("localhost","USERNAME","PASSWORD","sa") or die("The database has stopped working. :(");
  56.     $sql = "SELECT DISTINCT type FROM lc ORDER BY type ASC"; // this query eliminates all duplicates
  57.     $result = mysqli_query($db,$sql);
  58. ?>
  59. <form action="days.php" method="get">
  60. <label>Type:
  61. <select name="type">
  62. <option value=''>None</option>
  63. <?php
  64. // this while loop creates an option for every request type (probation, ban, etc.) that exists
  65.     while($row = mysqli_fetch_assoc($result))
  66.     {
  67.         extract($row);
  68.         ?>
  69.             <option value='"<?php echo $row['type']; ?>"'><?php echo $row['type']; ?></option>
  70.         <?php
  71.     }
  72.     mysqli_close($result); // close the connection and then we can do the new one
  73. // the BANS + AUTOBANS option is hard-coded instead
  74. ?>
  75. <option value='1'>BANS + AUTOBANS</option>
  76. </select>
  77. </label>
  78. <?php
  79.     // Connect to MySQL database again
  80.     $db = mysqli_connect("localhost","USERNAME","PASSWORD","sa") or die("The database has stopped working. :(");
  81.     $sql = "SELECT DISTINCT requestor FROM lc ORDER BY requestor ASC"; // query still eliminates all duplicates
  82.     $result = mysqli_query($db,$sql);
  83. ?>
  84. <label>Horrible mod:
  85. <select name="mod">
  86. <option value=''>None</option>
  87. <?php
  88. // now to create every option for every mod to grace the Leper's Colony ever
  89.     while($row = mysqli_fetch_assoc($result))
  90.     {
  91.         extract($row);
  92.         ?>
  93.             <option value='"<?php echo $row['requestor']; ?>"'><?php echo $row['requestor']; ?></option>
  94.         <?php
  95.     }
  96.     mysqli_close($result);
  97. ?>
  98. </select>
  99. </label>
  100. <?php
  101.     $db = mysqli_connect("localhost","USERNAME","PASSWORD","sa") or die("The database has stopped working. :(");
  102.     $sql = "SELECT DISTINCT month FROM lc ORDER BY month ASC";
  103.     $result = mysqli_query($db,$sql);
  104. ?>
  105. <label>Month:
  106. <select name="month">
  107. <option value=''>None</option>
  108. <?php
  109.     while($row = mysqli_fetch_assoc($result))
  110.     {
  111.         extract($row);
  112.         // what's happening here is month numbers become month names (ex. 7 => July)
  113.         $month2 = $row['month'];
  114.         $month  = $month_array[$month2];
  115.         ?>
  116.             <option value='<?php echo $row['month']; ?>'><?php echo $month; ?></option>
  117.         <?php
  118.     }
  119.     mysqli_close($result);
  120. ?>
  121. </select>
  122. </label>
  123. <?php
  124.     $db = mysqli_connect("localhost","USERNAME","PASSWORD","sa") or die("The database has stopped working. :(");
  125.     $sql = "SELECT DISTINCT year FROM lc ORDER BY year ASC";
  126.     $result = mysqli_query($db,$sql);
  127. ?>
  128. <label>Year:
  129. <select name="year">
  130. <option value=''>None</option>
  131. <?php
  132.     while($row = mysqli_fetch_assoc($result))
  133.     {
  134.         extract($row);
  135.         ?>
  136.             <option value='<?php echo $row['year']; ?>'><?php echo 2000 + $row['year']; ?></option>
  137.         <?php
  138.     }
  139.     mysqli_close($result);
  140.     /*
  141.     QUICK NOTE C/P: if you're wondering,
  142.     2000 + row['year'] will convert something like 7 to 2007,
  143.     simply putting 20 before the <?php echoing ?> the variable will interpret 7 as 207
  144.      */
  145. ?>
  146. </select>
  147. </label>
  148. <label>Graph width:
  149. <input type="text" name="width" value="500" size="4" />
  150. </label>
  151. <input type="submit" value="Search!" />
  152. </form>
  153. <?php
  154.    
  155.     /*
  156.     End of form input fields. Now enter the actual data table!
  157.     Hey, Lowtax, do you still understand what's going on?
  158.      */
  159. ?>
  160. <table>
  161.     <tr>
  162.         <th>DAY</th>
  163.         <th>NO. OF BANS</th>
  164.         <th>&nbsp;</th>
  165.     </tr>
  166. <?php
  167.     $db = mysqli_connect("localhost","USERNAME","PASSWORD","sa") or die("The database has stopped working. :(");
  168.     // a bunch of variables are being set up for upcoming loops
  169.     $i = 0;
  170.     $x = 0;
  171.     /*
  172.     the upcoming loop tries to find the highest result that will eventually be shown
  173.     this is to measure graph length
  174.  
  175.     if I don't start using capitals soon, Abe's gonna probate me like the internet tough guy he is
  176.     then Ozma (oxma fat) is gonna up it to a ban!
  177.      */
  178.     while($i < 31)
  179.     {
  180.         ++$i; // apparently this is faster than $i++. oh well
  181.         if($_GET['type'] == 1)
  182.         {
  183.             $sql = "SELECT * FROM lc WHERE day = ".$i." AND type NOT LIKE 'P%'"; // counts bans and autobans only
  184.         }
  185.         else
  186.         {
  187.             $sql = "SELECT * FROM lc WHERE day = ".$i; // sets up for upcoming AND queries
  188.             if ($_GET['type'] != "")
  189.             {
  190.                 $sql .= " AND type = ".$_GET['type']; // otherwise it's individual
  191.             }
  192.         }
  193.         if ($_GET['mod'] != "")
  194.         {
  195.             $sql .= " AND requestor = ".$_GET['mod'];
  196.         }
  197.         if ($_GET['month'] != "")
  198.         {
  199.             $sql .= " AND month = ".$_GET['month'];
  200.         }
  201.         if ($_GET['year'] != "")
  202.         {
  203.             $sql .= " AND year = ".$_GET['year'];
  204.         }
  205.         $result = mysqli_query($db,$sql);
  206.         // if the result here is bigger than variable x, variable x is overwritten to define the new result
  207.         $num = mysqli_num_rows($result);
  208.         if ($num > $x)
  209.         {
  210.             $x = $num;
  211.         }
  212.         mysqli_close($result); // I totally forgot what this does! honest!
  213.     }
  214.     // redefine variable i for the next loop
  215.     $i = 0;
  216.     // now we're gonna graph this shit!
  217.     while($i < 31)
  218.     {
  219.         ?><tr><?php
  220.         ++$i;
  221.         if($_GET['type'] == 1)
  222.         {
  223.             $sql = "SELECT * FROM lc WHERE day = ".$i." AND type NOT LIKE 'P%'"; // bans + autobans
  224.         }
  225.         else
  226.         {
  227.             $sql = "SELECT * FROM lc WHERE day = ".$i; // sets up for upcoming AND queries
  228.             if ($_GET['type'] != "")
  229.             {
  230.                 $sql .= " AND type = ".$_GET['type']; // otherwise individual type, mod, etc.
  231.             }
  232.         }
  233.         if ($_GET['mod'] != "")
  234.         {
  235.             $sql .= " AND requestor = ".$_GET['mod'];
  236.         }
  237.         if ($_GET['month'] != "")
  238.         {
  239.             $sql .= " AND month = ".$_GET['month'];
  240.         }
  241.         if ($_GET['year'] != "")
  242.         {
  243.             $sql .= " AND year = ".$_GET['year'];
  244.         }
  245.         $result = mysqli_query($db,$sql);
  246.         // okay, now we should get the results!
  247.         $num = mysqli_num_rows($result);
  248.         /*
  249.         that huge line over there is the bar.
  250.         colour is determined by length, and  length / greatest result  (variable x) becomes a percent.
  251.         &nbsp; is a blank space; it's there to make the bar show up.
  252.          */
  253.         ?>
  254.         <td><?php echo $i; ?></td>
  255.         <td><?php echo $num; ?></td>
  256.         <td style="width: <?php echo $width; ?>px;"><div style="width: <?php echo ($num / $x)*100; ?>%; background-color: #<?php echo dechex(16777215 - $num); ?>">&nbsp;</div></td><?php echo "\n";
  257.         ?></tr><?php echo "\n";
  258.         mysqli_close($result);
  259.     }
  260. ?>
  261. </table>
  262. </body>
  263. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement