Advertisement
Guest User

Untitled

a guest
Mar 24th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 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. function rowGenerator() {
  66. for ($i = 1; $i <= 11111; $i++) {
  67. $script = 'script' . $i;
  68. $start = strtotime('-1 months') + $i * 24 * 60 * 60;
  69. $end = strtotime('-1 month') + ($i + 1) * 24 * 60 * 60;
  70. $resultVariants = ['normal', 'illegal', 'failed', 'success'];
  71. $result = $resultVariants[array_rand($resultVariants, 1)];
  72.  
  73. (yield [$script, $start, $end, $result]);
  74. }
  75. };
  76.  
  77. foreach (rowGenerator() as $value) {
  78.  
  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. * @return array
  91. */
  92. public function get()
  93. {
  94. $db = $this->connection;
  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('localhost', 'test_dbs', 'root', 55555678); ?>
  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