Skip to main content
added 7 characters in body
Source Link

I have written my own little auto-generated table class, and I was wondering if you guys can give me some tips on how to make this code more elegant and secure and maybe compact it so I use only one master function to bind everything when I call it out!

I have written my own little auto-generated table class, and I was wondering if you guys can give me some tips on how to make this code more elegant and secure and maybe compact it so I use only one function to bind everything when I call it out!

I have written my own little auto-generated table class, and I was wondering if you guys can give me some tips on how to make this code more elegant and secure and maybe compact it so I use only one master function to bind everything when I call it out!

deleted 194 characters in body
Source Link
<?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$data = $foxy_crud->get_table_data();
    
    foreach ($users$data as $user_data$data_text) 
    {
        echo '<tr>';
        
            // Get data using generated table header names
            foreach ($columns as $column_data => $column_value) 
            {
                echo '<td>'.$user_data[$column_value['Field']]$data_text[$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 where ID
    $user$fetch_data = $foxy_crud->get_specific_data('2');
    
    // Fromating fields
    foreach ($columns_type$columns as $column_header_type => $column_value) 
    {
        
        // Identify Primary ID
        $id_key = $column_value['Key'];
        
        // Generate label names
        $name = $column_value['Field'];
        
        foreach ($user$fetch_data as $user_data$data_text) 
        {
            $value = $user_data[$name];$data_text[$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 passwordPassword - only works if tablea header name is `password`
        switch ($name) 
        {
            
            case 'Password':
            $type = 'Password';
            $disable = '';
            $placeholder = $name;
            $br = '<br>';
            break;
            
        }
        
        // Determine if fieldlabel text is primaryfrom Primary ID -> hide
        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;
        
    }
    
?>
<?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;
        
    }
    
?>
<?php

include_once 'classes.php';

$foxy_crud = new FoxyCRUD($pdo);

?>

<table border="1">

    <?php 
    
    // Generate table header
    $columns = $foxy_crud->get_header_elements('login');
    
    foreach ($columns as $column_header => $column_value) 
    {
        echo '<th>'.$column_value['Field'].'</th>';
    }
    

    // Populate table with data
    $data = $foxy_crud->get_table_data();
    
    foreach ($data as $data_text) 
    {
        echo '<tr>';
        
            // Get data using generated table header names
            foreach ($columns as $column_data => $column_value) 
            {
                echo '<td>'.$data_text[$column_value['Field']].'</td>';
            }
            
        echo '</tr>';
    }

    ?>
    
    
</table>

<br>
<br>

<?php
    
    // Fetch specific data where ID
    $fetch_data = $foxy_crud->get_specific_data('2');
    
    // Fromating fields
    foreach ($columns as $column_header_type => $column_value) 
    {
        
        // Identify Primary ID
        $id_key = $column_value['Key'];
        
        // Generate label names
        $name = $column_value['Field'];
        
        foreach ($fetch_data as $data_text) 
        {
            $value = $data_text[$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 label text is from Primary ID -> hide
        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;
        
    }
    
?>
deleted 55 characters in body; edited tags; edited title; edited tags; edited tags; edited tags
Source Link
200_success
  • 145.6k
  • 22
  • 191
  • 481

Auto generate database table code formating

I know this is just the barebones, but I am trying to make this school project fully functioning, and I want to format the code as correctly and as simplisticsimply as possible. Thank you in advance and sorry for my bad English!

Auto generate database table code formating

I know this is just the barebones, but I am trying to make this school project fully functioning, and I want to format the code as correctly and as simplistic as possible. Thank you in advance and sorry for my bad English!

Auto generate database table

I know this is just the barebones, but I am trying to make this school project fully functioning, and I want to format the code as correctly and as simply as possible.

Source Link
Loading