Retrieve and Edit Example
Code
<?php
include("Formitable.class.php");
$newForm = new Formitable( mysql_connect("server","user","pass"),"db","table" );
$newForm->setPrimaryKey("ID");
$newForm->setEncryptionKey("g00D_3nCr4p7");
// hide primary key field, force a few field types
$newForm->hideField("ID");
$newForm->hideField("toon");
$newForm->forceTypes(array("foods","day_of_week"),array("checkbox","radio"));
// get data pairs from another table
$newForm->normalizedField("toon","formitable_toons","ID","name","pkey ASC");
// set custom field labels
$newForm->labelFields(
array("f_name","l_name","description","pets","foods","color","day_of_week","b_day","toon"),
array("First Name","Last Name","About Yourself","Your Pets","Favorite Foods","Favorite Color",
"Favorite Day","Birthday","Favorite Cartoon")
);
// call submit method if form has been submitted
if( isset($_POST['submit']) ){
print '<p class="action center"><a href='.$_SERVER['PHP_SELF'].'>Back to List</a></p>';
}
// test if primary key is present in query string
else if( isset($_GET[$newForm->pkey]) )
{
$pkey = $_GET[$newForm->pkey];
// delete a record?
if( isset($_POST['del']) && $pkey ){
$result = $newForm->query('DELETE FROM '.$table.' WHERE `'.$newForm->pkey.'`="'.$pkey.'"');
print str_replace( 'update', 'delete', ($result?$newForm->msg_updateSuccess:$newForm->msg_updateFail) );
print '<p class="action center"><a href='.$_SERVER['PHP_SELF'].'>Back to List</a></p>';
// otherwise retrieve a record for update if ID is not 0
} else {
if($pkey) $newForm->getRecord($pkey);
$newForm->printForm();
}
}
// otherwise print existing records
else {
$printHeader = true;
$headerHtml = $rowHtml = $delHtml = $rowsHtml = '';
$rows = $newForm->query('SELECT ID,f_name,l_name,color,b_day FROM '.$table.' LIMIT 10', MYSQL_ASSOC);
foreach($rows as $row){
$rowHtml = '';
foreach($row as $key=>$value){
if($key == $newForm->pkey){
$headerHtml = $printHeader ? '<th class="edit"></th>'.$headerHtml : $headerHtml;
$rowHtml = '<td class="edit"><a href="?'.$key.'='.$value.'">Edit</a></td>'.$rowHtml;
$delHtml = '<td class="del"><form action="?'.$key.'='.$value.'" method="post">'.
'<button type="submit" name="del" onclick="return verify()">Delete</button></form></td>';
} else {
$headerHtml .= $printHeader ? '<th>'.$newForm->getFieldLabel($key).'</th>' : '';
$rowHtml .= $value?"<td>$value</td>":'<td class="empty">-</td>';
}
}
if($printHeader) $printHeader = false;
$rowsHtml .= "\n<tr>$rowHtml$delHtml</tr>";
}
print '<style>.boxed{ width:27em; }</style>';
print "<table>\n<tr>$headerHtml<th></th></tr>\n$rowsHtml\n</table>\n<p class=action><a href=?".$newForm->pkey."=0>Create a new record</a></p>";
}
?>
MySQL Tables
CREATE TABLE formitable_demo (
ID tinyint(3) unsigned NOT NULL auto_increment,
f_name varchar(50) default NULL,
l_name varchar(50) default NULL,
description text,
pets set('Dog','Cat','Fish','Horse','Frog','Rodent','Reptile','None') default NULL,
foods set('pizza','pasta','salad','sandwich','hamburger') default NULL,
color enum('Red','Blue','Green','Purple','Yellow','Other') default NULL,
day_of_week enum('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday') default NULL,
b_day date default NULL,
PRIMARY KEY (ID)
);