This ghetto ass code is in cloningjesus.com. i dont reccomend anyone trying to use it b/c it knows no transaction state, assumes too fucking much (naming conventions and "limited needs" etc), but for my purposes it works and will save me from having to write shit load of redumdant crap ... for no at least. im sure ill have to add some special case shit for some stuff.
This is my ghetto object maker - extending this will give you magic ability to not have to ever write a form to persist an object
<pre>
<?
require_once ($_SERVER['DOCUMENT_ROOT'] . "/inc/db.php");
class Object
{
var $fields = array();
var $table;
var $big = array();
function save ()
{
if ($this->fields['id'] != null || $this->fields['id'] != "")
{
$this->update ();
}
else
{
$this->insert ();
}
}
function insert ()
{
easyInsert ($this->table, $this->fields, "table");
}
function update ()
{
easyUpdate ($this->table, $this->fields, "table");
}
function delete()
{
easyDelete ($this->table, $this->fields['id']);
}
function setFields ($fields)
{
$this->fields = $fields;
}
function getEditFields ()
{
$html .= "<form action="form.php" method="post">";
$html .= "<table cellpadding="4">";
foreach ($this->fields as $k => $v)
{
$html .= "<tr>";
if (! isset ($this->big[$k]))
{
$html .= " <td><b>$k:</b></td>";
$html .= " <td><input type="text" name="$k" value="" . stripslashes ((isset ($v) ? $v : "")) . "" />";
}
else
{
$html .= " <td colspan="2"><b>$k:</b><br />";
$html .= " <textarea rows="25" cols="100" name="$k">" . stripslashes ((isset ($v) ? $v : "")) . "</textarea>";
$html .= "</td>";
}
$html .= "</tr>";
}
$html .= "<tr>";
$html .= "<td colspan="2"><input type="reset" value="reset" /> <input type=submit value="Save" /></td>";
$html .= "</tr>";
$html .= "</table>";
$html .= "<input type="hidden" name="table" value="$this->table" />";
$html .= "</form>";
return $html;
}
function form()
{
echo $this->getEditFields ();
}
function getById ($recId)
{
$sql = "select * from $this->table where id = '$recId'";
$res = mysql_query ($sql);
$row = mysql_fetch_assoc ($res);
foreach ($row as $k => $v)
{
$this->fields[$k] = $v;
}
}
}
?>
</pre>
the ghetto object maker submit to this ghetto thing ... this ghetto page will either save an object or show u a form to edit one
<pre>
<?
// ok ... we're either inserting or updating an object
if ($_POST['table'])
{
$table = $_POST['table'];
require_once ($_SERVER['DOCUMENT_ROOT'] . "/inc/$table.php");
$type = ucfirst($table);
$evalStr = "\$object = new $type" . "();";
eval ($evalStr);
$object->setFields ($_POST);
$object->save();
}
// not inserting or updating - lets start with a blank form
else
{
$type = $_GET['type'];
require_once ($_SERVER['DOCUMENT_ROOT'] . "/inc/$type.php");
$type = ucfirst($type);
$evalStr = "\$object = new $type" . "();";
echo $evalStr;
eval ($evalStr);
if (isset ($_GET['id']))
{
$object->getById ($_GET['id']);
}
$object->form();
}
?>
</pre>
This is my ghetto object maker - extending this will give you magic ability to not have to ever write a form to persist an object
<pre>
<?
require_once ($_SERVER['DOCUMENT_ROOT'] . "/inc/db.php");
class Object
{
var $fields = array();
var $table;
var $big = array();
function save ()
{
if ($this->fields['id'] != null || $this->fields['id'] != "")
{
$this->update ();
}
else
{
$this->insert ();
}
}
function insert ()
{
easyInsert ($this->table, $this->fields, "table");
}
function update ()
{
easyUpdate ($this->table, $this->fields, "table");
}
function delete()
{
easyDelete ($this->table, $this->fields['id']);
}
function setFields ($fields)
{
$this->fields = $fields;
}
function getEditFields ()
{
$html .= "<form action="form.php" method="post">";
$html .= "<table cellpadding="4">";
foreach ($this->fields as $k => $v)
{
$html .= "<tr>";
if (! isset ($this->big[$k]))
{
$html .= " <td><b>$k:</b></td>";
$html .= " <td><input type="text" name="$k" value="" . stripslashes ((isset ($v) ? $v : "")) . "" />";
}
else
{
$html .= " <td colspan="2"><b>$k:</b><br />";
$html .= " <textarea rows="25" cols="100" name="$k">" . stripslashes ((isset ($v) ? $v : "")) . "</textarea>";
$html .= "</td>";
}
$html .= "</tr>";
}
$html .= "<tr>";
$html .= "<td colspan="2"><input type="reset" value="reset" /> <input type=submit value="Save" /></td>";
$html .= "</tr>";
$html .= "</table>";
$html .= "<input type="hidden" name="table" value="$this->table" />";
$html .= "</form>";
return $html;
}
function form()
{
echo $this->getEditFields ();
}
function getById ($recId)
{
$sql = "select * from $this->table where id = '$recId'";
$res = mysql_query ($sql);
$row = mysql_fetch_assoc ($res);
foreach ($row as $k => $v)
{
$this->fields[$k] = $v;
}
}
}
?>
</pre>
the ghetto object maker submit to this ghetto thing ... this ghetto page will either save an object or show u a form to edit one
<pre>
<?
// ok ... we're either inserting or updating an object
if ($_POST['table'])
{
$table = $_POST['table'];
require_once ($_SERVER['DOCUMENT_ROOT'] . "/inc/$table.php");
$type = ucfirst($table);
$evalStr = "\$object = new $type" . "();";
eval ($evalStr);
$object->setFields ($_POST);
$object->save();
}
// not inserting or updating - lets start with a blank form
else
{
$type = $_GET['type'];
require_once ($_SERVER['DOCUMENT_ROOT'] . "/inc/$type.php");
$type = ucfirst($type);
$evalStr = "\$object = new $type" . "();";
echo $evalStr;
eval ($evalStr);
if (isset ($_GET['id']))
{
$object->getById ($_GET['id']);
}
$object->form();
}
?>
</pre>
chimay:
Greek to me.