PHP Classes

PHP Form Database Processor: Create and process forms to manage database data

Recommend this page to a friend!
  Info   View files Example   View files View files (24)   DownloadInstall with Composer Download .zip   Reputation   Support forum (1)   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 314 All time: 7,267 This week: 455Up
Version License PHP version Categories
formprocessor 1.0.8GNU General Publi...5HTML, PHP 5, Databases, Validation
Description 

Author

This package can create and process forms to manage database data.

It provides several classes that can be used to provide a Web based HTML user interface to manage data stored in database table records. The package classes can:

- Read the structure of a given table from a MySQL database and build a form to add or edit records in that table
- Retrieve the data sent by the form and processes them in order to save files onto the server and data in the database
- Show the content of the table providing means to sort, edit and delete records

Picture of Marco Gasi
  Performance   Level  
Name: Marco Gasi <contact>
Classes: 1 package by
Country: Italy Italy
Age: 62
All time rank: 3790152 in Italy Italy
Week rank: 360 Up16 in Italy Italy Up

Example

<?php
error_reporting
(E_ALL);
ini_set('display_errors', 'on');
header('Content-type: text/html; charset=utf-8');
require_once
'FormProcessor/FormProcessor.class.php';
?>
<!DOCTYPE html>
<html lang="en">
    <head>
    <title>Add</title>
        <style>
    fieldset{
        width: 35%;
      border: none;
      margin: 20px 0;
    }
    fieldset label{
        text-align: left;
      margin-right: 10px;
    }
        fieldset input:not([type="checkbox"]):not([type="radio"]),
        fieldset select,
        fieldset textarea{
            float: right;
        }
    input[type="submit"]{
      border: none;
      background: none;
      background-color: #136007;
      padding: 6px 10px;
      color: #fff!important;
      font-weight: bold;
      border-radius: 6px;
      min-width: 80px;
      cursor: pointer;
    }
    a.btn-cancel{
      background-color: #eee;
      padding: 6px 10px;
      color: #000!important;
      border-radius: 6px;
      min-width: 80px;
      margin-left: 10px;
      cursor: pointer;
    }
      .previewer{
        width: 100%;
        display: block;
        min-height: 100px;
        max-height: 200px;
        height: auto;
        overflow: auto;
      }
      #pictures-uploader .previewer img{
        margin-right: 4px;
        position: relative;
        width: 150px;
        text-align: center;
        padding: 4px;
      }
      #documents-uploader .previewer img{
        margin-right: 4px;
        position: relative;
        width: 100px;
        text-align: center;
        padding: 4px;
      }
      #documents-uploader .previewer p{
        font-size: .8em;
        max-width: 100px;
        word-wrap: break-word;
        text-align: center;
      }
      .controls{
        display: block;
        position: absolute;
        bottom: 4px;
        left: 0;
        width: 100%;
        text-align: center;
      }
      .controls p{
        text-align: center;
        width:100%;
      }
      .img-wrapper,
      .icon-wrapper{
        float: left;
        height: 160px;
        margin-right: 4px;
        position: relative;
        width: 120px;
        text-align: center;
        padding: 4px;
        box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2)
      }
      .img-wrapper img{
        width: 100%;
      }
      .uploader{
        clear:both;
      }
      .icon-wrapper img{
        margin-right: 4px;
        position: relative;
        width: 100px;
        text-align: center;
        padding: 4px;
      }
      .icon-wrapper p{
        font-size: .8em;
        word-wrap: break-word;
      }
    </style>
    </head>
    <body>
        <header id="header">
        </header>
        <section id="content">
            <div class="container">
                <div class="center">
                    <?php
    $fp
= new FormProcessor('products');
    echo
$fp->buildForm();
   
?>
</div>
            </div>
        </section>
    <script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
    <script>
      <?php echo $fp->printJsForAddPage(); ?>
</script>
    </body>
</html>


Details

FormProcessor

A class to perform CRUD ops easily and quickly

FormProcessor is a php class which allows to perform CRUD operations. More precisely, it allows the user to add, edit and delete records to/from a specific table.

You can visit <a href="https://formprocessor.codingfix.com">this page</a> to get more details.

Update 10 september 2018

  • Added a new class just to manage the database connection a singleton
  • Migrated DbModel and FormModel classes from mysqli to PDO
  • Added the new setting 'dbDriver' to config.php in order to allow to choose which database driver must be used
  • Converted all queries to prepared statements to improve security

Update 7 september 2018

Fixed residual underscore names in some file (delitem.php, delete.php, edit.php etc.) Changed example logic: before, example files used the classes in 'lib' folder but this made me implement wrong logic for config file. Now in example folder you find a FormProcessor folder which contains classes used by the example files (like normally we'd have in real world. I hope to be able to publish soon a small website to illustrate the package in detail.

Update 20 august 2018

  • removed FormProcessor and FormProcessor classes, merged into the FormProcessor one.
  • fixed bug deleting pictures or documents from an existing record
  • fixed a bug adding pictures or documents to an existing record
  • added code to delete all file associated with a given record when this record is deleted

Update 16 august 2018

  • fixed a bug which prevent to create multiple selects from external tables
  • removed table_name from the config file: now you must pass table_name to the constructor: this makes the class more flexible.

General description

FormProcessor class

FormProcessor has been originated by the need to easily create a form reading the table structure from a mysql database. Currently, only a little number of MySql field types are supported: - varchar - text - int - decimal - tinyint - bit I also added some configuration options int a config file which allows to manage checkboxes, radiobuttons and selects (these ones in 2 different ways).

FormProcessor gives you the ability to save the form data and upload the supported files types with few lines of code.

FormTable class

FormTable builds a simple html table to show data stored in the mysql table. The resulting table allows to sort records in ascending or descending order clicking on each column name; in addition, each record has 2 action buttons, 'Edit' end 'Delete'. This class is included only to make it easy to show how FromProcessor works and it is not really part of the package.

FormModel class

FormModel is used to perform database operations.

Usage

To illustrate the use of these classes I have included a full working example. To see it in action you have to use the 2 sql files provided in the folder 'db' and create the 2 table 'products' and 'category' in your local or remote server. Then you have to upload to your server the content of the 'example' folder and finally just set the values required for the database connection in the config.php file and the correct siteUrl. Now you're ready to go to yourserver.com/manage.php.

I plan to write a more detailed description in the next weeks and even to create a mini website, even if I prefer to work to improve the class itself (and there is a lot of work to do to improve at least security and flexibility).

If you want to see an example of the code required to create a form, here you go:

Add a new record

require_once "../lib/FormProcessor.class.php";
$fb = new FormProcessor();
echo $fb->build_form();

Save a new record

require_once "../lib/FormProcessor.class.php";
$fs = new FormProcessor();
echo $fs->save();

Edit an existing record

require_once "../lib/FormProcessor.class.php";
$fb = new FormProcessor(123);//the id value for the record to edit
echo $fb->build_form();

Save an updated record

require_once "../lib/FormProcessor.class.php";
$item_id = filter_input(INPUT_POST, 'item_id', FILTER_SANITIZE_NUMBER_INT);
$fs = new FormProcessor($item_id); //you can use this code even to save a new record: if $item_id is null there is no problem
echo $fs->save();

Conclusions

That's all folks. Hope you can find it useful and don't hexitate to contact me for criticism and suggestions... okay, even for congrats, if you really think I deserve them :)

You can find me at my blog codingfix.com, on Twitter, Facebook and Instagram.


  Files folder image Files  
File Role Description
Files folder imagedb (2 files)
Files folder imageexample (8 files, 1 directory)
Files folder imagelib (6 files)
Plain text file LICENSE Lic. License text
Plain text file README.md Doc. Documentation

  Files folder image Files  /  db  
File Role Description
  Plain text file category.sql Data Auxiliary data
  Plain text file products.sql Data Auxiliary data

  Files folder image Files  /  example  
File Role Description
Files folder imageFormProcessor (6 files)
  Plain text file add.php Example Example script
  Plain text file delete.php Aux. Auxiliary script
  Plain text file deleteDocument.php Example Example script
  Plain text file deletePicture.php Example Example script
  Plain text file delitem.php Example Example script
  Plain text file edit.php Example Example script
  Plain text file manage.php Example Example script
  Plain text file save.php Example Example script

  Files folder image Files  /  example  /  FormProcessor  
File Role Description
  Plain text file config.php Aux. Auxiliary script
  Plain text file dbConnection.class.php Class - Added a new class just to manage the database connection a singleton - Migrated DbModel and FormModel classes from mysqli to PDO - Added the new setting 'dbDriver' to config.php in order to allow to choose which database driver must be used - Converted all queries to prepared statements to improve security
  Plain text file DbModel.class.php Class Class source
  Plain text file FormModel.class.php Class Class source
  Plain text file FormProcessor.class.php Class Class source
  Plain text file FormTable.class.php Class Class source

  Files folder image Files  /  lib  
File Role Description
  Plain text file config.php Aux. Auxiliary script
  Plain text file dbConnection.class.php Class - Added a new class just to manage the database connection a singleton - Migrated DbModel and FormModel classes from mysqli to PDO - Added the new setting 'dbDriver' to config.php in order to allow to choose which database driver must be used - Converted all queries to prepared statements to improve security
  Plain text file DbModel.class.php Class - Changed naming convention converting everything to camelCase; - Refactored FormProcessor class, splitting buildForm() function into several smaller functions to make the code more easily mantainable; - renamed several functions in FormProcessor class to make more clear their goal - added DbModel class: now FormModel class extends DbModel
  Plain text file FormModel.class.php Class Class source
  Plain text file FormProcessor.class.php Class Added to delete documents from an existing record
  Plain text file FormTable.class.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:314
This week:0
All time:7,267
This week:455Up