Advertisement
Guest User

Untitled

a guest
Mar 24th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 KB | None | 0 0
  1. <?php
  2.  
  3. final class Init
  4. {
  5. private $host;
  6. private $dbName;
  7. private $user;
  8. private $password;
  9. private $connection;
  10.  
  11. /**
  12. * Init constructor.
  13. */
  14. public function __construct($host, $dbName, $user, $password)
  15. {
  16. $this->host = $host;
  17. $this->dbName = $dbName;
  18. $this->user = $user;
  19. $this->password = $password;
  20.  
  21. try {
  22. $this->connection = new \PDO("mysql:host=$this->host;dbname=$this->dbName", $this->user, $this->password);
  23. $this->connection->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
  24. $this->create();
  25. $this->fill();
  26.  
  27. } catch (\PDOException $e) {
  28. echo 'There are PDO Exception';
  29. } finally {
  30. /* some finally code */
  31. }
  32. }
  33.  
  34.  
  35. /** Create table `test`
  36. * @return int
  37. */
  38. private function create()
  39. {
  40. $db = $this->connection;
  41. $sql = "CREATE TABLE IF NOT EXISTS `test` (
  42. `id` INT(11) NOT NULL AUTO_INCREMENT,
  43. `script_name` CHAR(25) NOT NULL,
  44. `start_time` INT(11) NOT NULL,
  45. `end_time` INT(11) NOT NULL,
  46. `result` ENUM('normal','illegal','failed','success') NOT NULL,
  47. PRIMARY KEY (`id`)
  48. )
  49. ENGINE=InnoDB DEFAULT CHARSET=utf8";
  50. $db->exec($sql);
  51. }
  52.  
  53. /**
  54. * Fill in table `test`
  55. * @return int
  56. */
  57. private function fill()
  58. {
  59. $db = $this->connection;
  60. $sql = "INSERT INTO `test` (`script_name`, `start_time`, `end_time`, `result`) VALUES (?, ?, ?, ?)";
  61. $j = 0;
  62. $db->beginTransaction();
  63. $sth = $db->prepare($sql);
  64.  
  65. $rowGenerator = function()
  66. {
  67. for ($i = 1; $i <= 11111; $i++) {
  68. $script = 'script' . $i;
  69. $start = strtotime('-1 months') + $i * 24 * 60 * 60;
  70. $end = strtotime('-1 month') + ($i + 1) * 24 * 60 * 60;
  71. $resultVariants = ['normal', 'illegal', 'failed', 'success'];
  72. $result = $resultVariants[array_rand($resultVariants, 1)];
  73.  
  74. yield [$script, $start, $end, $result];
  75. }
  76. };
  77.  
  78. foreach ($rowGenerator as $value) {
  79. ++$j;
  80. $sth->execute($value);
  81. if (0 === ($j % 500)) {
  82. $db->commit();
  83. $db->beginTransaction();
  84. }
  85. }
  86. $db->commit();
  87. }
  88.  
  89.  
  90. // private function rowGenerator()
  91. // {
  92. // for ($i = 1; $i <= 11111; $i++) {
  93. // $script = 'script' . $i;
  94. // $start = strtotime('-1 months') + $i * 24 * 60 * 60;
  95. // $end = strtotime('-1 month') + ($i + 1) * 24 * 60 * 60;
  96. // $resultVariants = ['normal', 'illegal', 'failed', 'success'];
  97. // $result = $resultVariants[array_rand($resultVariants, 1)];
  98. //
  99. // yield [$script, $start, $end, $result];
  100. // }
  101. // }
  102.  
  103.  
  104. /**
  105. * @return array
  106. */
  107. public function get()
  108. {
  109. $db = $this->connection;
  110. $sql = "SELECT * FROM `test` WHERE `result` IN ('success', 'normal')";
  111.  
  112. return $db->query($sql)->fetchAll();
  113. }
  114. }
  115.  
  116. ?>
  117.  
  118. <h3>Create new Init()</h3>
  119.  
  120. <?php $init = new Init('localhost', 'test_dbs', 'root', 55555678); ?>
  121. <br>
  122.  
  123. <h3>Calling Init::get()</h3>
  124.  
  125. <?php $rows = $init->get() ?>
  126.  
  127.  
  128. <table border="" style="width: 100%" align="center">
  129. <thead>
  130. <th>ID</th>
  131. <th>script_name</th>
  132. <th>start_time</th>
  133. <th>end_time</th>
  134. <th>result</th>
  135. </thead>
  136.  
  137. <tbody>
  138. <?php foreach ($rows as $row): ?>
  139. <?php $background = $row['result'] === 'success' ? 'green' : 'yellow' ?>
  140. <tr style="background-color: <?php echo $background ?>">
  141. <td><?php echo $row['id'] ?></td>
  142. <td><?php echo $row['script_name'] ?></td>
  143. <td><?php echo date('d-m-Y', $row['start_time']) ?></td>
  144. <td><?php echo date('d-m-Y', $row['end_time']) ?></td>
  145. <td><?php echo $row['result'] ?></td>
  146. </tr>
  147. <?php endforeach; ?>
  148. </tbody>
  149. </table>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement