PHP Classes

File: example1.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in Bookmark in
  Classes of Sasa Radovanovic  >  Table Data Management  >  example1.php  >  Download  
File: example1.php
Role: Example script
Content type: text/plain
Description: Example for usage of the main tabledatamanagement methods (load, delete, save and update)
Class: Table Data Management
Interface to manipulate data in database tables
Author: By
Last change:
Date: 10 years ago
Size: 2,906 bytes


Class file image Download
* Example of how to use the tabledatamanagement object on a specific table.
* The table we use is a table named content, with columns: id (int), content (text), creationdate (date).
* This script shows you how you can use the tabledatamanagement object to handle your actions to your tables.
* You could also extend the tabledatamanagement object, so that certain methods are extended or disabled (by placing
* an empty method (for f.e. delete)).
* This example show how:
* - posted data can be used to store the data into the table
* What's cool (IMHO), is that you can paste the whole POST array to the object and the object it self
* nows which data to extract from it and also how it must be stored into the database (also using the necessary
* escapes)
* - a specific content page can be deleted
* - a specific content page can be loaded
* The object it self knows which column to use for a load (since the tablekey is "calculated" during initialization)
* @author S.Radovanovic (
* @uses TableDataManagement, MySQLConnection, TableColumn

/** BEGIN: Include the necessary files **/
/** END: Include the necessary files **/

/** BEGIN: Initialize objects **/
//Create a connection to the database (fill in the yours)
$objMySQLConnection = new MySQLConnection("yourdatabase", "yourusername", "yourpassword", "yourdbhost");

//Initialize our content object (specifying that we use the content table)
$objContent = new TableDataManagement($objMySQLConnection, "content");
/** END: Initialize objects **/

//See if a contentId was specified
$contentId = $_POST["contentId"];
//Set the contentId into the content object (for usage in save, update, delete and load)
$objContent->set("id", $contentId);

//Check to see which frmEvent was called
if($frmEvent == "save") {
    //Load the POST array into the content object (needed for the save action)
//check to see if we must save or update (if contentId = 0, it means we have a new content page)
if($contentId > 0) {
trigger_error("Your changes have been updated.", MESSAGE);
    } else {
        //When saving, we store also the original creationdate
$objContent->set("creationdate", date("Y-m-d H-i-s"));
trigger_error("The new content has been stored.", MESSAGE);

} else if(
$frmEvent == "delete") {
trigger_error("The content has been removed", MESSAGE);
} else {
    //Load the specified content

//Retrieve the loaded content from the database
$theContent = $objContent->get("content");