the purpose of this is to generate a create or update form based on the table from the db, on pages that require such action.

<?php
// create, update generator
function form_gen($act,$id=''){
	global $db; global $view; global $cats;
	$result = $db->query("SELECT * FROM {$view}");
	$fields = $db->num_fields($result);
	$rows   = $db->num_rows($result);
	$table  = $db->field_table($result, 0);		
	$o = '<h3>'.$act.' '.$table.' page</h3>
	<form method="post" action="'. link_to($table,$id) .'">
	<table id="blog_post">';
	for ($i=0; $i < $fields; $i++) {
		$type  = $db->field_type($result, $i);
		$name  = $db->field_name($result, $i);
		$len   = $db->field_len($result, $i);
		$flags = $db->field_flags($result, $i);
		// get rid of id and table name
		if ($name == 'id' || $name == 'table') null;	 
		else {	// see if it edit 
			if ($act == 'edit') {
				$r = $db->query("SELECT * FROM {$table} WHERE id= {$id} LIMIT 1");
				$row = $db->fetch_assoc($r);
				extract($row);
			} else $$name = '';
		// start page html 
		$o .= "<tr>";
		$o .= ($act == 'new' && $name == 'updated_at') ? '' : "<th><div id='data'>".ucfirst(preg_replace('/_/',' ',$name))."</div></th>"; 			
			if ($type == 'blob') {
				$o .= "<td><div id='data'><textarea cols='50' rows='10' name='{$name}'>".$$name."</textarea></div></td>";
			}
			elseif ($type == 'timestamp') { // means this is created_at
				if ($act == 'edit') {
				 $o .= "<td><div id='data'><input type='text' name='{$name}' maxlength='{$len}' value='".$$name."' /></div></td>";
				}
				else {
				 $o .= "<td><div id='data'><input type='text' name='{$name}' maxlength='{$len}' value='".date('Y-m-d h:i:s')."' /></div></td>";
				}
			}
			elseif ($type == 'datetime') {
				if ($act == 'edit') {
					if ($$name == '0000-00-00 00:00:00') $$name = date('Y-m-d h:i:s');
				$o .= "<td><div id='data'><input type='text' name='{$name}' maxlength='{$len}' value='".$$name."' /></div></td>";
				} else { null; }
			}
			elseif ($name == 'cat') {

			$o .= "<td><div id='data'><select name='{$name}'>";
			$selected = '';	$output = '';						
			while($item = each($cats)): 
				if ($item['key'] == $$name) { 
					$selected = "<option value='".$item['key']."'>".$item['value']."</option>";
				unset($item); } 
				else {$output .= "<option value='".$item['key']."'>".$item['value']."</option>";}
			endwhile;
			$selected .= $output;
			$o .= $selected;
			$o .= "</select></div></td>";					
		}
		else {
		$o .= "<td><div id='data'><input type='text' name='{$name}' maxlength='{$len}' value='".$$name."' /></div></td>";
		}
		$o .= "</tr>";
	}
}
$o .= '<tr>
<th><div id="data">Username: &nbsp;&nbsp; <input type="text" name="username" /></div></th>
<th><div id="data">Password: &nbsp;&nbsp; <input type="password" name="password" /></div></th>
</tr>';
$o .= '<tr>
	<td>
	<div id="data">
	<input type="button" value="Cancel" class="button"
	 onClick="window.location.href=\''.link_to($table,$id).'\';" /> ';		 
	if ($act == 'edit') { 
		$o .= '&nbsp;&nbsp;
		 <input type="button" class="button" value="Destroy" 
		onClick="f=prompt(\'Enter Password\')
		 if (isset(f)) { window.location.href=\''.link_to($table,$id).'&act=destroy&p=\'+f; }" />';
	}
	$o .= '</div></td>';
	if ($act == 'edit') {
		$o .= '<td>
			<div id="data">
			<input type="hidden" name="submit" value="Update" />
			<input type="submit" name="submit" value="Update" class="button" />
			</div>
			</td>';
	 } else {
		$o .= '<td>
		<div id="data">
		<input type="hidden" name="submit" value="Post" />
		<input type="submit" name="submit" value="Post" class="button" />
		</div>
		</td>';
	}
$o .= '</tr> ';
$o .= '</table></form>';
return $o;
}
?>
    5 days later

    So this is a form creator/updater?

    Looks more like the creator since there's not update method in the function and seems pretty messy man to be honest.

    But take it for what it's worth. =O)

      its just a function that creates the form inside table with rows etc. Based upon the database table you have. It doesnt do any action but sumbit the data and if its and Update then it fills the input fields with data for you. I have 2 other functions which are called upon that do the actual Create/Update this is more of like a trigger.

      I have also user verification added to this. If you can see at the bottom with user and password. I will try to make it not as messy right now i am studying somethings. I may in the end write a whole class. To be able to build a whole app through another app like it will populate the template and have the architecture and structure built in with all the helper functions or methods if its a class.

      Like I can remove the created_at and updated_at and make them automatic. And then make a helper function for the figuring out data types from the db and for those takes give back appropriate html tag such as <input /> or <select> or <textarea>

        Just some food for thought: you might want to take a look at PEAR::HTML_QuickForm2 for an object-oriented implementation. It might be all you need, something you could modify, or just an inspiration for writing your own ooPHP implementation.

          Write a Reply...