Viewing file: table.inc (4.65 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php function disp_table($conn, $table) { global $PHP_SELF, $HTTP_GET_VARS;
$query = "select oid,* from ".$table; if ($HTTP_GET_VARS["order_by"]) { $query.=" order by ".$HTTP_GET_VARS["order_by"]." desc"; $order_by=','.$order_by; } $query.=';'; $result = pg_Exec($conn, $query);
$rows=pg_numrows($result);
echo "<FORM method=post>"; echo "<TABLE border=1 width=600>\n"; echo "<CAPTION>".$query."</CAPTION>\n"; echo "<TR><TH><input type=submit name=action value=new>"; // "<input type=submit name=action value=preferences>";
for($field=1;$field<pg_numfields($result);$field++) { $fieldname=pg_fieldname($result,$field); echo "<TH><A HREF=\"".$PHP_SELF."?order_by=";
$orders=""; $orders[]=$fieldname; $a=strtok($HTTP_GET_VARS["order_by"],","); while($a) { if ($a!=$fieldname) $orders[]=$a; $a=strtok(","); }
echo urlencode(implode($orders,",")), '">',$fieldname,"</A>\n"; }
for($row=0;$row<$rows;$row++) { echo "<TR><TD>"; $obj=pg_Fetch_Row($result,$row); echo "<input type=submit name=action value=\"delete ".$obj[0]."\"><br>"; echo "<input type=submit name=action value=\"edit ".$obj[0]."\"><br>"; echo "<input type=checkbox name=oid[] value=".$obj[0].">"; echo implode($obj, "<TD>"); echo "</TR>\n"; }
/* echo '<FORM method=post>';
echo "\n<TR>";
for($field=0;$field<pg_numfields($result);$field++) { $fieldname=pg_fieldname($result,$field); echo "<TD><input type=text name=\"",$fieldname,"\" value=\"",$HTTP_POST_VARS[$fieldname],"\">\n"; } */ echo("</TABLE>"); echo("</FORM>"); }
function edit_dialog($result,$oid) { $obj=pg_fetch_row($result,0); echo "<form method=post>\n"; echo "<input type=hidden name=oid[] value=".$oid.">\n"; echo "<table border=1 cellpadding=0 cellspacing=0 width=600>\n"; for($field=0;$field<pg_numfields($result);$field++) { echo "<tr><th align=right>".pg_fieldname($result,$field).":"; echo "<td>"; if (strchr($obj[$field],"\n") || strchr($obj[$field],"\r")) { echo "<textarea name=\"fields[".pg_fieldname($result,$field)."]\" rows=10 cols=80>\n"; echo htmlentities($obj[$field]); echo "</textarea>"; } else { echo "<input type=text size=".strlen($obj[$field])." name=\"fields[".pg_fieldname($result,$field)."]\""; echo " value=\"".htmlentities($obj[$field])."\">"; } echo "</tr>\n"; } echo "<tr><td><th><input type=submit name=action value=update> <input type=submit name=action value=cancel>"; echo "</table>\n"; echo "</form>\n"; }
function new_dialog($result) { echo "<form method=post>\n"; echo "<table border=1 width=600>\n"; for($field=0;$field<pg_numfields($result);$field++) { echo "<tr><th align=right>".pg_fieldname($result,$field).":"; echo "<td><input type=text size=40 name=\"fields[".pg_fieldname($result,$field)."]\"></tr>\n"; } echo "<tr><td><th><input type=submit name=action value=insert> <input type=submit name=action value=cancel>"; echo "</table>\n"; echo "</form>\n"; }
function handle_table($conn,$table) { global $HTTP_POST_VARS;
if (isset($HTTP_POST_VARS[action])) {
if ($p=strpos($HTTP_POST_VARS[action]," ")) { $HTTP_POST_VARS[oid][]=substr($HTTP_POST_VARS[action],$p+1); $HTTP_POST_VARS[action]=substr($HTTP_POST_VARS[action],0,$p); }
switch ($HTTP_POST_VARS[action]) { case "delete": if(pg_exec($conn,"delete from ".$table." where oid in (".join($HTTP_POST_VARS[oid],", ").");")) disp_table($conn,$table); break; case "edit": if ($result=pg_exec($conn,"select * from ".$table." where oid=".current($HTTP_POST_VARS[oid]).";")) edit_dialog($result,current($HTTP_POST_VARS[oid])); break; case "new": $result=pg_exec($conn,"select * from ".$table." where oid=1;"); new_dialog($result); break; case "insert": unset($k); unset($v); while ($a=each($HTTP_POST_VARS[fields])) { if ($a[value]!="") { $k[]=$a[key]; $v[]="'".addslashes($a[value])."'"; } } if (sizeof($k)==0) { echo "Empty form - not inserting."; exit(); } if (pg_exec($conn,"insert into ".$table." (".join($k,", ").") VALUES (".join($v,", ").");")) { // $result=pg_exec($conn,"select * from ".$table." where oid=1;"); // new_dialog($result); disp_table($conn,$table); } break; case "update": unset($q); while (list($key, $value)=each($HTTP_POST_VARS[fields])) { if ($value=="") $q[]="\"".$key."\" = null"; else $q[]="\"".$key."\" = '".$value."'"; } if (pg_exec($conn,"update ".$table." set ".join($q,", ")." where oid=".current($HTTP_POST_VARS[oid]).";")) disp_table($conn,$table); break; case "order": break; default: disp_table($conn,$table); } } else disp_table($conn,$table); } ?>
|