Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- final class Init
- {
- private $host = 'localhost';
- private $dbs = 'test_dbs';
- private $user = 'root';
- private $password = 55555678;
- private $db;
- /**
- * Init constructor.
- */
- public function __construct()
- {
- try {
- $this->db = new \PDO("mysql:host=$this->host;dbname=$this->dbs", $this->user, $this->password);
- $this->db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
- $this->create();
- if(!$this->get()) {
- $this->fill();
- }
- } catch (\PDOException $e) {
- file_put_contents('/tmp/PDOErrors.txt', date('d-m-Y H:i:s ') . $e->getMessage() . PHP_EOL, FILE_APPEND);
- echo 'There are PDO Exception';
- } finally {
- /* some finally code */
- }
- }
- /** Create table `test`
- * @return int
- */
- private function create()
- {
- $db = $this->db;
- $sql = "CREATE TABLE IF NOT EXISTS `test` (
- `id` INT(11) NOT NULL AUTO_INCREMENT,
- `script_name` CHAR(25) NOT NULL,
- `start_time` INT(11) NOT NULL,
- `end_time` INT(11) NOT NULL,
- `result` ENUM('normal','illegal','failed','success') NOT NULL,
- PRIMARY KEY (`id`)
- )
- ENGINE=InnoDB DEFAULT CHARSET=utf8";
- $db->exec($sql);
- }
- /**
- * Fill in table `test`
- * @return int
- */
- private function fill()
- {
- $db = $this->db;
- $sql = "INSERT INTO `test` (`script_name`, `start_time`, `end_time`, `result`) VALUES (?, ?, ?, ?)";
- $j = 0;
- $db->beginTransaction();
- $sth = $db->prepare($sql);
- foreach (self::rowGenerator() as $value) {
- ++$j;
- $sth->execute($value);
- if (0 === ($j % 500)) {
- $db->commit();
- $db->beginTransaction();
- }
- }
- $db->commit();
- }
- private function rowGenerator()
- {
- for ($i = 1; $i <= 11111; $i++) {
- $script = 'script' . $i;
- $start = strtotime('-1 months') + $i * 24 * 60 * 60;
- $end = strtotime('-1 month') + ($i + 1) * 24 * 60 * 60;
- $resultVariants = ['normal', 'illegal', 'failed', 'success'];
- $result = $resultVariants[array_rand($resultVariants, 1)];
- yield [$script, $start, $end, $result];
- }
- }
- /**
- * @return array
- */
- public function get()
- {
- $db = $this->db;
- $sql = "SELECT * FROM `test` WHERE `result` IN ('success', 'normal')";
- return $db->query($sql)->fetchAll();
- }
- }
- ?>
- <h3>Create new Init()</h3>
- <?php $init = new Init(); ?>
- <br>
- <h3>Calling Init::get()</h3>
- <?php $rows = $init->get() ?>
- <table border="" style="width: 100%" align="center">
- <thead>
- <th>ID</th>
- <th>script_name</th>
- <th>start_time</th>
- <th>end_time</th>
- <th>result</th>
- </thead>
- <tbody>
- <?php foreach ($rows as $row): ?>
- <?php $background = $row['result'] === 'success' ? 'green' : 'yellow' ?>
- <tr style="background-color: <?php echo $background ?>">
- <td><?php echo $row['id'] ?></td>
- <td><?php echo $row['script_name'] ?></td>
- <td><?php echo date('d-m-Y', $row['start_time']) ?></td>
- <td><?php echo date('d-m-Y', $row['end_time']) ?></td>
- <td><?php echo $row['result'] ?></td>
- </tr>
- <?php endforeach; ?>
- </tbody>
- </table>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement