PHP Classes

File: example.php

Recommend this page to a friend!
  Classes of Andrew Sullivan  >  Ajax Table Edit  >  example.php  >  Download  
File: example.php
Role: Example script
Content type: text/plain
Description: Contains an example usage
Class: Ajax Table Edit
Edit data in an HTML table using AJAX
Author: By
Last change:
Date: 16 years ago
Size: 2,188 bytes


Class file image Download
//ini_set("display_errors", "1");
//ini_set("error_reporting", "E_ALL");

$at = new TableEditor;

if (isset(
$_POST['new'])) {
//You can choose to handle the data processing part of your script
    //this way, where it is all in one page.
    //Just make sure that you either return nothing on success,
    //or you echo out "true", otherwise it will error

//the script returns three values:
    //$rowid: the id for the row
    //$colid: the value for the column
    //$new: the value that was typed into the text input
$data = file_get_contents("./tester.txt");
$data = unserialize($data);

$data[$rowid][$colid] = $new;

//if this were a database, you could use the following SQL statement:
    //UPDATE $tablename SET `$colid` = '$new' WHERE id = '$rowid'
    //you should do some verification on the data input by the user.
$open = fopen("./tester.txt", 'w');
fwrite($open, serialize($data));
} else {
//the display properties for the odd and even rows
$odd = array('style' => 'background-color: #CCCCCC;');
$even = array('style' => 'background-color: #EEEEEE;');

//the display properties for the overall table
$table = array('align' => 'center', 'cellpadding' => '3', 'cellspacing' => '0', 'width' => '50%');

//table column header information
$headers = array("Col 0", "Col 1", "Col 2", "Col 3");
$headerattrib = array('style' => 'background-color: skyblue');
$data = file_get_contents("./tester.txt");
$data = unserialize($data);
//echo "<pre>";


//comment out everything above, and uncomment this to rebuild the sample data

for ($a = 0; $a < 20; $a++) {
    for ($b = 0; $b < 4; $b++) {
        $n[$a]["row" . $b] = "Row $a column $b";

$open = fopen("./tester.txt", 'w');
fwrite($open, serialize($n));