Advertisement
Guest User

Test

a guest
Mar 2nd, 2015
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.87 KB | None | 0 0
  1. <?php
  2. class crud extends PDO
  3. {
  4.     private $engine;
  5.     private $host;
  6.     private $database;
  7.     private $user;
  8.     private $pass;
  9.  
  10.     private $result;
  11.  
  12.     public function __construct()
  13.     {
  14.         $this->engine   = 'mysql';
  15.         $this->host     = 'localhost';
  16.         $this->database = 'dips';
  17.         $this->user     = 'root';
  18.         $this->pass     = '';
  19.  
  20.         $dns = $this->engine . ':dbname=' . $this->database . ";host=" . $this->host;
  21.         parent::__construct($dns, $this->user, $this->pass);
  22.     }
  23.  
  24.     /*
  25.      * Insert values into the table
  26.      */
  27.     public function insert($table, $rows = null)
  28.     {
  29.         $command = 'INSERT INTO ' . $table;
  30.         $row     = null;
  31.         $value   = null;
  32.         foreach ($rows as $key => $nilainya) {
  33.             $row .= "," . $key;
  34.             $value .= ", :" . $key;
  35.         }
  36.  
  37.         $command .= "(" . substr($row, 1) . ")";
  38.         $command .= "VALUES(" . substr($value, 1) . ")";
  39.  
  40.  
  41.         $stmt = parent::prepare($command);
  42.         $stmt->execute($rows);
  43.         $rowcount = $stmt->rowCount();
  44.         return $rowcount;
  45.     }
  46.  
  47.     /*
  48.      * Delete records from the database.
  49.      */
  50.     public function delete($tabel, $where = null)
  51.     {
  52.         $command = 'DELETE FROM ' . $tabel;
  53.  
  54.         $list      = Array();
  55.         $parameter = null;
  56.         foreach ($where as $key => $value) {
  57.             $list[] = "$key = :$key";
  58.             $parameter .= ', ":' . $key . '":"' . $value . '"';
  59.         }
  60.         $command .= ' WHERE ' . implode(' AND ', $list);
  61.  
  62.         $json  = "{" . substr($parameter, 1) . "}";
  63.         $param = json_decode($json, true);
  64.  
  65.         $query = parent::prepare($command);
  66.         $query->execute($param);
  67.         $rowcount = $query->rowCount();
  68.         return $rowcount;
  69.     }
  70.  
  71.     /*
  72.      * Uddate Record
  73.      */
  74.     public function update($tabel, $fild = null, $where = null)
  75.     {
  76.         $update = 'UPDATE ' . $tabel . ' SET ';
  77.         $set    = null;
  78.         $value  = null;
  79.         foreach ($fild as $key => $values) {
  80.             $set .= ', ' . $key . ' = :' . $key;
  81.             $value .= ', ":' . $key . '":"' . $values . '"';
  82.         }
  83.         $update .= substr(trim($set), 1);
  84.         $json  = '{' . substr($value, 1) . '}';
  85.         $param = json_decode($json, true);
  86.  
  87.         if ($where != null) {
  88.             $update .= ' WHERE ' . $where;
  89.         }
  90.  
  91.         $query = parent::prepare($update);
  92.         $query->execute($param);
  93.         $rowcount = $query->rowCount();
  94.         return $rowcount;
  95.     }
  96.  
  97.  
  98.     /*
  99.      * Selects information from the database.
  100.      */
  101.     public function select($table, $rows, $where = null, $order = null, $limit = null)
  102.     {
  103.         $command = 'SELECT ' . $rows . ' FROM ' . $table;
  104.         if ($where != null)
  105.             $command .= ' WHERE ' . $where;
  106.         if ($order != null)
  107.             $command .= ' ORDER BY ' . $order;
  108.         if ($limit != null)
  109.             $command .= ' LIMIT ' . $limit;
  110.  
  111.         $query = parent::prepare($command);
  112.         $query->execute();
  113.  
  114.         $posts = array();
  115.         while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
  116.             $posts[] = $row;
  117.         }
  118.         return $this->result = $posts;
  119.     }
  120.  
  121.     /*
  122.      * Returns the result set
  123.      */
  124.     public function getResult()
  125.     {
  126.         return $this->result;
  127.     }
  128.  
  129.  
  130. }
  131.  
  132. ?>
  133.  
  134. <?php
  135.  
  136. $dbpro = new crud();
  137.  
  138. $protabel = "pro";
  139. $profield = "*";
  140.  
  141. $dbpro->select($protabel, $profield);
  142. $result = $dbpro->getResult();
  143.  
  144. ?>
  145. <table border="1" style="border-color:‪#‎FFC‬;">
  146. <thead>
  147.     <tr>
  148.         <th>Code</th>
  149.         <th>Province</th>
  150.     </tr>
  151. </thead>
  152. <tbody>
  153.     <?php foreach ($result as $key => $value) : ?>
  154.     <tr>
  155.         <td><?php echo $value['idpro']; ?></td>
  156.         <td><?php echo $value['pronm']; ?></td>
  157.     </tr>
  158.     <?php endforeach; ?>
  159. </tbody>
  160. </table>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement