Advertisement
Guest User

Untitled

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