Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $host = 'localhost';
- $db = '***';
- $user = '***';
- $pass = '***';
- $charset = 'utf8';
- $dsn = "mysql:host=$host;dbname=$db;charset=$charset";
- $settings = [
- PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
- PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
- PDO::ATTR_EMULATE_PREPARES => false,
- ];
- $pdo = new PDO($dsn, $user, $pass, $settings);
- <?php
- include_once 'db.php';
- class FoxyCRUD {
- /* Properties */
- private $conn;
- private $table;
- private $where;
- /* Get database access */
- public function __construct(PDO $pdo)
- {
- $this->conn = $pdo;
- }
- /* Get database access */
- public function __destruct()
- {
- $this->conn = null;
- }
- /* Fetch db.table -> column names & type */
- public function get_header_elements($table)
- {
- // Set table name
- $this->table = $table;
- $query = $this->conn->prepare('SHOW FULL COLUMNS FROM '.$this->table);
- $query->execute();
- return $query->fetchAll(PDO::FETCH_ASSOC);
- }
- /* Fetch db.table -> column names & type */
- public function where()
- {
- $query = $this->conn->prepare('SHOW FULL COLUMNS FROM '.$this->table);
- $query->execute();
- $check_where = $query->fetchAll(PDO::FETCH_ASSOC);
- return $check_where[0]['Field'];
- }
- /* Fetch all data */
- public function get_table_data()
- {
- $query = $this->conn->prepare('SELECT * FROM '.$this->table);
- $query->execute();
- return $query->fetchAll();
- }
- /* Fetch specific data */
- public function get_specific_data($where)
- {
- // Set where value
- $this->where = $where;
- $query = $this->conn->prepare('SELECT * FROM '.$this->table.' WHERE '.$this->where().' = '.$this->where);
- $query->execute();
- return $query->fetchAll();
- }
- }
- <?php
- include_once 'classes.php';
- $foxy_crud = new FoxyCRUD($pdo);
- ?>
- <table border="1">
- <?php
- // Generate table header
- $columns = $foxy_crud->get_header_elements('login');
- $foxy_crud->where();
- foreach ($columns as $column_header => $column_value)
- {
- echo '<th>'.$column_value['Field'].'</th>';
- }
- // Populate table with users data
- $users = $foxy_crud->get_table_data();
- foreach ($users as $user_data)
- {
- echo '<tr>';
- // Get data using generated table header names
- foreach ($columns as $column_data => $column_value)
- {
- echo '<td>'.$user_data[$column_value['Field']].'</td>';
- }
- echo '</tr>';
- }
- ?>
- </table>
- <br>
- <br>
- <?php
- // Prepare form fields
- $columns_type = $foxy_crud->get_header_elements('login');
- /* echo '<pre>';
- print_r($columns_type); */
- // Fetch specific data
- $user = $foxy_crud->get_specific_data('2');
- // Fromating fields
- foreach ($columns_type as $column_header_type => $column_value)
- {
- // Identify Primary ID
- $id_key = $column_value['Key'];
- // Generate label names
- $name = $column_value['Field'];
- foreach ($user as $user_data)
- {
- $value = $user_data[$name];
- }
- $name = str_replace('_', ' ', $name);
- $name = ucwords($name);
- // Determine field type and maxlength
- list( $type, $max ) = explode ('(', $column_value['Type']);
- $max = explode (')', $max)[0];
- // Specify how to format each field type
- switch ($type)
- {
- case 'int':
- $type = 'hidden';
- $disable = 'disabled';
- $placeholder = '';
- $br = '';
- break;
- case 'varchar':
- $type = 'text';
- $disable = '';
- $placeholder = $name;
- $br = '<br>';
- break;
- case 'datetime':
- $type = 'date';
- $disable = '';
- $placeholder = '';
- $br = '<br>';
- break;
- case 'tinyint':
- $type = 'tel';
- $disable = '';
- $placeholder = $name;
- $br = '<br>';
- break;
- }
- // Determine if field is type password - only works if tablea header name is `password`
- switch ($name)
- {
- case 'Password':
- $type = 'Password';
- $disable = '';
- $placeholder = $name;
- $br = '<br>';
- break;
- }
- // Determine if field is primary ID
- switch ($id_key)
- {
- case 'PRI':
- $label = '';
- break;
- default:
- $label = '<label for="'.$name.'">'.$name.' ('.$max.')</label><br>';
- break;
- }
- // Generate labels
- echo $label;
- // Generate form fields
- echo '<input type="'.$type.'" name="'.$name.'" id="'.$name.'" placeholder="'.$placeholder.'" value="'.$value.'" maxlength="'.$max.'" '.$disable.'>'.$br;
- }
- ?>
Add Comment
Please, Sign In to add comment