<?php
require_once('jsondb.init.php');
if ($_REQUEST['process'] and $_REQUEST['dbName']) {
$dbName = $_REQUEST['dbName'];
$dbData = $jdb->rDataBase($dbName);
switch ($_REQUEST['action']) {
case 'createDB':
$jdb->cDataBase($dbName);
break;
case 'updateDB':
if (empty($_REQUEST['id'])) {
//adding record
$id = $jdb->nextRecord($dbData);
$dbData[$id]['title'] = (!empty($_REQUEST['title'])) ? $_REQUEST['title'] : $dbData[$id]['title'];
$dbData[$id]['author'] = (!empty($_REQUEST['author'])) ? $_REQUEST['author'] : $dbData[$id]['author'];
} else {
$id = $_REQUEST['id'];
if (empty($_REQUEST['title']) and empty($_REQUEST['author'])) {
//removing record
foreach ($dbData as $key => $value) {
if ($key == $id) {
continue;
}
foreach ($dbData[$key] as $field => $value) {
$dbNew[$key][$field] = $value;
}
}
$dbData = $dbNew;
} else {
//changing record
$dbData[$id]['title'] = (!empty($_REQUEST['title'])) ? $_REQUEST['title'] : $dbData[$id]['title'];
$dbData[$id]['author'] = (!empty($_REQUEST['author'])) ? $_REQUEST['author'] : $dbData[$id]['author'];
}
}
$jdb->uDataBase($dbName, $dbData);
break;
case 'deleteDB':
$jdb->dDataBase($dbName);
unset($dbData);
break;
}
} else {
$dbName = 'test.jdb';
$dbData = $jdb->rDataBase($dbName);
}
?>
<!DOCTYPE HTML>
<html>
<head>
<title>jsonDB Test Script</title>
</head>
<body>
<form method="post">
Database Name: <input type="text" name="dbName" value="<?php
echo HtmlSpecialChars($dbName);
?>" /><br />
<input type="radio" name="action" value="createDB" /> Create<br />
<input type="radio" name="action" value="deleteDB" /> Delete
<hr />
Record ID: <input type="text" name="id" /> <em>(Leave blank to use next ID number)</em><br />
Title: <input type="text" name="title" /><br />
Author: <input type="text" name="author" /><br />
<input type="radio" name="action" value="updateDB" /> Update<br />
<em>(Providing a record id without a title or author will delete that record)</em>
<hr />
<input type="hidden" name="process" value="1" />
<input type="submit" name="formSubmit" value="Submit" />
</form>
<hr />
<?php
var_dump($dbData);
?>
</body>
</html>
|