Advertisement
Guest User

Untitled

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