Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- final class Init
- {
- private $host;
- private $dbName;
- private $user;
- private $password;
- private $connection;
- /**
- * Init constructor.
- */
- public function __construct($host, $dbName, $user, $password)
- {
- $this->host = $host;
- $this->dbName = $dbName;
- $this->user = $user;
- $this->password = $password;
- try {
- $this->connection = new \PDO("mysql:host=$this->host;dbname=$this->dbName", $this->user, $this->password);
- $this->connection->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
- $this->create();
- $this->fill();
- } catch (\PDOException $e) {
- echo 'There are PDO Exception';
- } finally {
- /* some finally code */
- }
- }
- /** Create table `test`
- * @return int
- */
- private function create()
- {
- $db = $this->connection;
- $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->connection;
- $sql = "INSERT INTO `test` (`script_name`, `start_time`, `end_time`, `result`) VALUES (?, ?, ?, ?)";
- $j = 0;
- $db->beginTransaction();
- $sth = $db->prepare($sql);
- 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]);
- }
- };
- foreach (rowGenerator() as $value) {
- ++$j;
- $sth->execute($value);
- if (0 === ($j % 500)) {
- $db->commit();
- $db->beginTransaction();
- }
- }
- $db->commit();
- }
- /**
- * @return array
- */
- public function get()
- {
- $db = $this->connection;
- $sql = "SELECT * FROM `test` WHERE `result` IN ('success', 'normal')";
- return $db->query($sql)->fetchAll();
- }
- }
- ?>
- <h3>Create new Init()</h3>
- <?php $init = new Init('localhost', 'test_dbs', 'root', 55555678); ?>
- <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