Saturday, 7 September 2013

editable dynamic php table

editable dynamic php table

I am creating a table dynamically that is editable. It works great,
perhaps too good for what i need. With my table i have a edit column with
a change button for each row and is automatically created at the end of
the table. And you have to click a change button on the row to edit it. I
would like to get rid of the edit buttons/column(that way the user can
click on the cell button and it will always be editable) and make it to
where the user can only edit the row that is assigned to them with their
specific id. Also i cannot figure out how to create a box that pops up
when the cell button is clicked, the user will have options within the
popup box that when the user chooses the option it loads the data to the
cell. Thanks for any help you can provide.
<?php
session_start();
$tbvbr= $_SESSION['gamecode'];
$pn = $_POST['playername'];
function stripslashes2( $string )
{
if ( get_magic_quotes_gpc() ) {
return stripslashes( $string );
} else {
return $string;
}
}
function display_db_query( $tablename, $header_bool , $border )
{
// find out the number of columns in result
$result = mysql_query( "SHOW FIELDS FROM $tablename" );
while ( $row = mysql_fetch_assoc( $result ) ) {
if ( $row['Key'] == "PRI" )
$primarykey = $row['Field'];
else
$field[$row['Field']] = array( $row['Type'] );
}
if ( isset( $_POST["update$tablename"] ) ) {
$sql = sprintf( "update $tablename SET " );
$sqlfields = array();
foreach( $field AS $k => $v ) {
if ( !empty( $_POST["edit"][$k] ) )
$sqlfields[] = "$k='" . mysql_real_escape_string(
stripslashes2( $_POST["edit"][$k] ) ) . "'";
else
$sqlfields[] = "$k = NULL ";
}
if ( count( $sqlfields ) > 0 ) {
$sql .= implode( " , " , $sqlfields ) . " WHERE $primarykey=" .
intval( $_POST["updateid"] ) ;
mysql_query( $sql ) OR DIE( mysql_error() );
if ( mysql_affected_rows() > 0 )
print "Updated succesfully<br />";
}
else
echo "No change<br />";
}
// perform the database query
$result_id = mysql_query( "SELECT * from $tablename" )
or die( "display_db_query:" . mysql_error() );
if ( $header_bool ) {
echo "<table width='850' $border align='center' cellpadding='5'
cellspacing='1' class='entryTable'>";
echo "<tr class='entryTableHeader'>";
foreach( $field AS $k => $v )
print( "<td><center><b>$k</b></center></td>" );
print( "<td><center><b>Edit</b></center></td>
</tr>\n" );
} else
echo "<table width='850' $border align='center' cellpadding='5'
cellspacing='1' class='entryTable'> ";
while ( $row = mysql_fetch_assoc( $result_id ) ) {
print( "<tr>" );
if ( isset( $_GET["editmode"] ) AND $_GET["editmode"] ==
$row[$primarykey] ) {
$editmodeison = true;
echo "<form method=\"post\" action=\"{$_SERVER["PHP_SELF"]}\">";
} else
$editmodeison = false;
foreach( $field AS $k => $v ) {
if ( $editmodeison )
print( "<td class='content' align='center'><input
type=\"button\" name=\"edit[$k]\" value=\"" . ( !empty(
$row[$k] )?htmlspecialchars( $row[$k] ) : htmlspecialchars( ''
) ) . "\" /></td>\n" );
else
print( "<td class='content' align='center'>" . ( !empty(
$row[$k] )?htmlspecialchars( $row[$k] ) : htmlspecialchars(
'change' ) ) . "</td>\n" );
}
if ( $editmodeison )
print( "<td class='content' align='center'><input type=\"hidden\"
name=\"updateid\" value=\"{$row[$primarykey]}\"><input
type=\"submit\" name=\"update$tablename\"
value=\"update\"></form></td>\n" );
else
print( "<td class='content' align='center'><a
href=\"{$_SERVER["PHP_SELF"]}? editmode=" . $row[$primarykey] .
"\">change</a></td>\n" );
print( "</tr>\n" );
}
print( "</table>\n" );
}
?>
<HTML><HEAD><TITLE>Products Orderable table</TITLE></HEAD>
<BODY>
<TABLE><TR><TD>
<?php
/* DB info */
$dbhost = "localhost";
$dbuser = "placeholder";
$dbpass = "placeholder";
$dbname = "placeholder";
mysql_connect( $dbhost, $dbuser, $dbpass ) or die ( "Unable to connect to
MySQL server" );
mysql_select_db( "$dbname" );
mysql_query( "SET NAMES utf8" );
$table = $tbvbr;
display_db_query( $table, // $global_dbh,
true, "border='2'" );
?>
</TD></TR></TABLE></BODY></HTML>

No comments:

Post a Comment