简体   繁体   English

可编辑的动态php表

[英]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. 我想摆脱编辑按钮/列(这样,用户可以单击单元格按钮,它将始终是可编辑的),并使其移到用户只能编辑为其特定行分配给它们的行。 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>

You can use HTML5 Content Editable too. 您也可以使用HTML5 Content Editable。 Here is a simple fiddle illustrating it. 这是一个简单的小提琴进行说明。

dom.contentEditable = true;

Set that and any DOM object can be edited. 设置它,任何DOM对象都可以编辑。 You may probably use the click event to enable or disable content editable property. 您可能使用click事件来启用或禁用内容可编辑属性。

to answer my own question i edited out 回答我自己编辑的问题

    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" ); 

and i set 然后我开始

 $_GET["editmode"] 

to the variable id that was assigned to the user. 分配给用户的变量ID。

I also used the following Javascript 我还使用了以下Javascript

   <script type="text/javascript">
 function reply_click(clicked_name)
 {
var clicked_name;
document.location="http://www.localhost.com/update.php?location=" + clicked_name;

  }
  </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM