PHP Classes

Simple CRUD with MySQL 2: Output SQL and CRUD forms based on remoteDB class

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 569 This week: 1All time: 5,388 This week: 41Up
Version License PHP version Categories
crud-class 4GNU General Publi...5HTML, PHP 5, Databases
Description 

Author

This class can generate SQL and CRUD forms based on remoteDB class.

It is a class derived from the original work by Mahboobz. This version was improved in the following ways:

Version 4:
Update: replaced irp_commonSQL.php (obsolete) with common_pdo.php

Version 3:
- Added 2 more callback: before_save(), before_updated()
- Tested on Master-detail tables: see example pages
- Tested on view tables.

Version 3: added 2 new hooks, and master->detail capability
added 2 functions: setEdit() and setDelete() to control buttons
Version 2:
- Added $this->index array to store one or more PK names (in place of one pre-defined PK='id')
- Added get_where(), get_hidden($record) to handle multiple PKs
- Added optional hook and callback to customize the CRUD page:
1) special input fields: select, radio... (crud_get_input)
2) special edit fields: select, radio... (crud_get_edit)
3) special show fields: links, references... (crud_get_show)
4) more actions in table (crud_action_hook)
- Added 3 static utility: make_select, make_radio and make_checkbox for input/edit fields
- Added CONFIRM to delete
- Added $extrasql parameter to renderVertically(), to extend the basic sql: "SELECT * FROM ".$this->table
- Cosmetic minor variations, use of css file (in: remoteDB/css/style.css)

Picture of Marco Sillano
  Performance   Level  
Name: Marco Sillano <contact>
Classes: 4 packages by
Country: Italy Italy
Age: ???
All time rank: 201279 in Italy Italy
Week rank: 53 Up3 in Italy Italy Up
Innovation award
Innovation award
Nominee: 3x

Winner: 1x

Recommendations

What is the best PHP pdo mysql class?
PHP Project using PDO MySQL Class

What is the best PHP crud class?
How to create CRUD with PHP

doing all crud operations
doing all crud operations with one class

class for add tags category
i need a add tag when enter article in mysql table

master detail form read create edit delete forms
i want to create add records into a master detail form

Example

<?php
/*
CRUD3 EXAMPLE GENERIC PAGE.... CHANGE for customization
*/
//
$r=dirname(__FILE__);
include(
"$r/lib/crudClass3.php");
require_once (
"$r/lib/irp_commonSQL.php");

if (isset(
$_GET['id_cliente'])){ // CHANGE: here the PK
  
$_POST = $_GET; // POST/GET compatible
  
}
echo
'<html><head><meta content="text/html; charset=UTF_8" http-equiv="content-type">';
echo
StyleSheet();
echo
"</head><body>";
//
echo "<h1> Tavola <b>Clienti</b>: <i>add/edit/delete records</i></h1>";// CHANGE: page Title

echo "<div class='note' align='center'>
Questa tabella definisce i vari attributi dei <b>clienti</b>.
</div>"
; // CHANGE: intro boz
//-------------------------------------------------- CALLBACKS (if required)
// callbacks use examples:
/*
//callback for show fields (view)
function crud_get_show($field, $value) {
    $code = $value; // general case
// custom special cases:
  // change id_user on users.name
   if ($field == 'id_user'){
            $code = sqlValue("SELECT name FROM users WHERE $field = $value");
       }
 // to center value in cell
   if ($field == 'unit'){
            $code = "<CENTER>$value</CENTER>";
      }
     
  return $code;
}
*/
/*
// callback for input fields (new)
function crud_get_input($field){
  $code = "$field: <input type='text' name='$field' /><br>"; // general case
  // custom special cases
 // choose id_user from a user.name list
  if ($field == 'id_user'){
       $code = crudClass::make_select($field, "SELECT id_user, name FROM users order by name"); // users.name list
         }
// date input field
  if ($field == 'arrival'){
     $code = "$field: <input type='date' name='$field' /><br>";
  }
 
  return $code;
}
*/
/*
//callback for input fields (edit)
function crud_get_edit($field, $value){
  $code = "$field: <input type='text' name='$field' value='$value' /><br>"; // general case
   // custom special cases
  // make id_town field readonly
  if ($field == 'id_town'){
         "$field: <input type='text' name='$field' value='$value' readonly /><br>";
         }
  // use radio buttons for ENUM:
  if ($field == 'unita_misura'){
         $optionlist= 'Kg,Nr.';
         $code = crudClass::make_radio($field, $optionlist, $value); // radio
         }

   return $code;
}
*/
/*
//callback add action (view)
function crud_action_hook($record){
 // ad action button 'test_user'
    $code = "<td><form action='test_user.php' mode='POST'>";
    $code .= "<input type='hidden' name='id_user' value=".$record['id_user'].">";
    $code .= "<input type='submit' name='test_user' value='USER TEST'></form></td>";
    return $code;
}
*/
/*
//callback before create (new)
function before_create($values){
// $values[2] is three times $values[1]
    $values[2]= 3 * $values[1] ;
return $values;
*/
/*
function before_update($strings){
// note: strings are like: $strings[2] => "`id_user` = '16'"
// make id_user = 22
  $forced = 22;
  $strings[2]="`id_user` = '$forced'";
return $strings;
*/

// -------------------------------------------------- END CALLBACKS

$crud = new crudClass('clienti','ragione_sociale,piva,cf,indirizzo,cap,id_citta,contatto,telefono,fax,email,destinazione,indirizzo2,cap2,id_citta2,telefono2,commissione,note','id_cliente' );// CHANGE: Initiate the class with table information: table-name, fields, pk

// ================= don't change
if (isset($_POST['submit'])){
   
$create_sql = $crud->create();//Fetch INSERT query
   
sql($create_sql);
}
if (isset(
$_POST['update'])){
   
$update_sql = $crud->update();//Fetch UPDATE query
  
sql($update_sql);
}
if (isset(
$_POST['delete'])){
   
$delete_sql = $crud->delete();//Fetch DELETE query
   
sql($delete_sql);
}
// -------------
if (isset($_POST['edit'])){
// edit
   
echo "<div class='note' align='right'>";
    echo
$crud->renderEditor();//Prepare data edit form
   
echo '</div>' ;
    } else {
// or insert
   
echo "<div class='note' align='right'>";
    echo
$crud->create_form();//Prepare data entry form
   
echo '</div>';
    }
 
// table
 // =============== don't change ends
echo $crud->renderVertically(' ORDER BY `ragione_sociale`');// CHANGE: for WHERE or ORDER or LIMIT

echo '<hr><center> <a href="javascript:history.go(-1)"><<< back </a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href="index.html">home</a> </center><br>'; // CHANGE: end page menu
echo "</body></html>";

?>


  Files folder image Files (12)  
File Role Description
Files folder imageexamples (4 files)
Files folder imageremoteDB (2 files, 1 directory)
Accessible without login Plain text file common_pdo.php Aux. Update for commonSQL (obsolete)
Plain text file crudClass2.php Class Updated CRUDclass
Plain text file crudClass3.php Class new version master->detail
Plain text file crudClass4..php Class Update version
Accessible without login Plain text file irp_commonSQL.php Aux. old SQL common

  Files folder image Files (12)  /  examples  
File Role Description
  Accessible without login Plain text file crud_basic.php Example for the user
  Accessible without login Plain text file crud_details.php Example for the user
  Accessible without login Plain text file crud_master.php Example for the user
  Accessible without login Plain text file crud_view.php Example for the user

  Files folder image Files (12)  /  remoteDB  
File Role Description
Files folder imagecss (1 file)
  Accessible without login Plain text file irp_commonSQL.php Aux. mySQL access library
  Accessible without login Plain text file irp_config.php Aux. mySQL access data

  Files folder image Files (12)  /  remoteDB  /  css  
File Role Description
  Accessible without login Plain text file style.css Data CSS file

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 0%
Total:569
This week:1
All time:5,388
This week:41Up