Guest User

Untitled

a guest
Oct 15th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.94 KB | None | 0 0
  1. <?php
  2. $host = 'localhost';
  3. $db = '***';
  4. $user = '***';
  5. $pass = '***';
  6. $charset = 'utf8';
  7.  
  8. $dsn = "mysql:host=$host;dbname=$db;charset=$charset";
  9.  
  10. $settings = [
  11. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  12. PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  13. PDO::ATTR_EMULATE_PREPARES => false,
  14. ];
  15.  
  16. $pdo = new PDO($dsn, $user, $pass, $settings);
  17.  
  18. <?php
  19.  
  20. include_once 'db.php';
  21.  
  22. class FoxyCRUD {
  23.  
  24. /* Properties */
  25. private $conn;
  26. private $table;
  27. private $where;
  28.  
  29. /* Get database access */
  30. public function __construct(PDO $pdo)
  31. {
  32. $this->conn = $pdo;
  33. }
  34.  
  35. /* Get database access */
  36. public function __destruct()
  37. {
  38. $this->conn = null;
  39. }
  40.  
  41.  
  42.  
  43. /* Fetch db.table -> column names & type */
  44. public function get_header_elements($table)
  45. {
  46. // Set table name
  47. $this->table = $table;
  48.  
  49. $query = $this->conn->prepare('SHOW FULL COLUMNS FROM '.$this->table);
  50. $query->execute();
  51. return $query->fetchAll(PDO::FETCH_ASSOC);
  52. }
  53.  
  54. /* Fetch db.table -> column names & type */
  55. public function where()
  56. {
  57. $query = $this->conn->prepare('SHOW FULL COLUMNS FROM '.$this->table);
  58. $query->execute();
  59. $check_where = $query->fetchAll(PDO::FETCH_ASSOC);
  60. return $check_where[0]['Field'];
  61. }
  62.  
  63. /* Fetch all data */
  64. public function get_table_data()
  65. {
  66. $query = $this->conn->prepare('SELECT * FROM '.$this->table);
  67. $query->execute();
  68. return $query->fetchAll();
  69. }
  70.  
  71. /* Fetch specific data */
  72. public function get_specific_data($where)
  73. {
  74. // Set where value
  75. $this->where = $where;
  76.  
  77. $query = $this->conn->prepare('SELECT * FROM '.$this->table.' WHERE '.$this->where().' = '.$this->where);
  78. $query->execute();
  79. return $query->fetchAll();
  80. }
  81. }
  82.  
  83. <?php
  84.  
  85. include_once 'classes.php';
  86.  
  87. $foxy_crud = new FoxyCRUD($pdo);
  88.  
  89. ?>
  90.  
  91. <table border="1">
  92.  
  93. <?php
  94.  
  95. // Generate table header
  96. $columns = $foxy_crud->get_header_elements('login');
  97. $foxy_crud->where();
  98.  
  99. foreach ($columns as $column_header => $column_value)
  100. {
  101. echo '<th>'.$column_value['Field'].'</th>';
  102. }
  103.  
  104.  
  105. // Populate table with users data
  106. $users = $foxy_crud->get_table_data();
  107.  
  108. foreach ($users as $user_data)
  109. {
  110. echo '<tr>';
  111.  
  112. // Get data using generated table header names
  113. foreach ($columns as $column_data => $column_value)
  114. {
  115. echo '<td>'.$user_data[$column_value['Field']].'</td>';
  116. }
  117.  
  118. echo '</tr>';
  119. }
  120.  
  121. ?>
  122.  
  123.  
  124. </table>
  125.  
  126. <br>
  127. <br>
  128.  
  129. <?php
  130.  
  131. // Prepare form fields
  132. $columns_type = $foxy_crud->get_header_elements('login');
  133.  
  134. /* echo '<pre>';
  135. print_r($columns_type); */
  136.  
  137. // Fetch specific data
  138. $user = $foxy_crud->get_specific_data('2');
  139.  
  140. // Fromating fields
  141. foreach ($columns_type as $column_header_type => $column_value)
  142. {
  143.  
  144. // Identify Primary ID
  145. $id_key = $column_value['Key'];
  146.  
  147. // Generate label names
  148. $name = $column_value['Field'];
  149.  
  150. foreach ($user as $user_data)
  151. {
  152. $value = $user_data[$name];
  153. }
  154.  
  155. $name = str_replace('_', ' ', $name);
  156.  
  157. $name = ucwords($name);
  158.  
  159. // Determine field type and maxlength
  160. list( $type, $max ) = explode ('(', $column_value['Type']);
  161.  
  162. $max = explode (')', $max)[0];
  163.  
  164. // Specify how to format each field type
  165. switch ($type)
  166. {
  167.  
  168. case 'int':
  169. $type = 'hidden';
  170. $disable = 'disabled';
  171. $placeholder = '';
  172. $br = '';
  173. break;
  174.  
  175. case 'varchar':
  176. $type = 'text';
  177. $disable = '';
  178. $placeholder = $name;
  179. $br = '<br>';
  180. break;
  181.  
  182. case 'datetime':
  183. $type = 'date';
  184. $disable = '';
  185. $placeholder = '';
  186. $br = '<br>';
  187. break;
  188.  
  189. case 'tinyint':
  190. $type = 'tel';
  191. $disable = '';
  192. $placeholder = $name;
  193. $br = '<br>';
  194. break;
  195.  
  196. }
  197.  
  198. // Determine if field is type password - only works if tablea header name is `password`
  199. switch ($name)
  200. {
  201.  
  202. case 'Password':
  203. $type = 'Password';
  204. $disable = '';
  205. $placeholder = $name;
  206. $br = '<br>';
  207. break;
  208.  
  209. }
  210.  
  211. // Determine if field is primary ID
  212. switch ($id_key)
  213. {
  214.  
  215. case 'PRI':
  216. $label = '';
  217. break;
  218.  
  219. default:
  220. $label = '<label for="'.$name.'">'.$name.' ('.$max.')</label><br>';
  221. break;
  222.  
  223. }
  224.  
  225. // Generate labels
  226. echo $label;
  227.  
  228.  
  229. // Generate form fields
  230. echo '<input type="'.$type.'" name="'.$name.'" id="'.$name.'" placeholder="'.$placeholder.'" value="'.$value.'" maxlength="'.$max.'" '.$disable.'>'.$br;
  231.  
  232. }
  233.  
  234. ?>
Add Comment
Please, Sign In to add comment