Familiar with general database operations-front-end easyui-datagrid, form (php), easyuidatagrid_PHP tutorial

Source: Internet
Author: User
Familiar with common database operations-front-end easyui-datagrid, form (php), and easyuidatagrid. First recognized general database operation class -- Front-end easyui-datagrid, form (php), easyuidatagrid first recognized general database operation class -- Front-end easyui-datagrid, form (php ), the implementation code is better than the common database operation class-front-end easyui-datagrid, form (php), easyuidatagrid

Familiar with common database operations-front-end easyui-datagrid, form (php), the implementation code is relatively simple, the specific implementation steps are described below.

Functions:

The left-side datagrid displays brief information, the right-side displays details of the selected row, and the database adds, deletes, modifies, and deletes the selected row.

(1) click the selected row and the details are displayed on the right. the "add", "modify", and "delete" buttons are available, and the "save" button is disabled.

(2) click add, modify, delete, and save.

(3) Click modify, add, and delete.

Difficulties: insert and update methods in general database operations

Finally:

The front-end functions are not very complete, and the logic between buttons is still a bit problematic. Finally, add the front-end code.

Formain. php checks the value passed by the front end and calls actSQL. class. php to obtain the result.

Simple code

As follows:

<? Php require ('include/mysql_connect/actSQL. class. php '); $ key = $ _ REQUEST ['key']; $ a = new actSQL ('localhost', 'root', '123', 'tps '); // Obtain information if ($ key = '1') {$ a-> getAllData ('t_ prekeychart');} if ($ key = '2 ') {$ objectstr = $ _ REQUEST ['object']; if ($ a-> insertData ($ objectstr, 't_ prekeychar ')) {echo json_encode ("true");} else {echo json_encode ("false");} // test // $ test = '{"keychartid": "2 ", "keyid": "2", "keychartna Me ":" 2 "," level ":" 2 "," showtype ":" 2 "," helptips ":" 2 "," keylevel ":" 2 ", "ishmap": "2"} '; // $ a-> insertData ($ test, 't_ prekeychart');} if ($ key = '3 ') {$ prekey = $ _ REQUEST ['keychartid']; $ prekeyname = 'keychartname'; if ($ a-> delData ($ prekey, $ prekeyname, 't_prekeychart') {echo json_encode ("true");} else {echo json_encode ("false") ;}} if ($ key = '4 ') {$ objectstr = $ _ REQUEST ['object']; $ prekeyname = 'keychartid'; if ($-> UpdData ($ objectstr, $ prekeyname, 't_ prekeychart') {echo json_encode ("true");} else {echo json_encode ("false") ;}}?>

Let's take a look at the methods of the classes in Formain. php.

Where

GetAllData ($ tablename) is used to obtain all information about a table. this method is simple. you can use a simple SQL statement and return the result in json format.

DelData ($ prekey, $ prekeyname, $ tablename) deletes the specified information. this is simpler, so we won't talk about it anymore.

InsertData ($ objectstr, $ tablename), where $ objectstr is a json string, $ tablename table name,

The difficulty is to splice the statement into insert into $ tablename (...) values (...).

Solution:

(1) obtain all the column names based on $ tablename, convert the array of column names to strings, and prepare for the last SQL statement. In addition, the column names can be obtained for another purpose.

(2) convert the string $ objectstr in json format to an associated array and call the json_decode () method.

Supplement the json_decode () method

Mixed json_decode (string $ json [, bool $ assoc = false [, int $ depth = 512 [, int $ options = 0])
Accept a JSON string and convert it to a PHP variable. assoc returns an associated array when this parameter is set to TRUE.

(3) query data in the order of queried column names. if the data is NULL, the value is NULL, and the result is saved to an array to prevent the database from inserting a wrong value (another reason for obtaining the column name)

(4) convert the result of (3) to a string and call the implode () method.

Supplement implode () method:

String implode (string glue, array pieces );
This function combines the content of the array into a string. the glue parameter is the delimiter between words.

(5) piece together the SQL statement string and insert it into the database.

The updData ($ objstr, $ prekeyname, $ tablename) method is difficult because the strings of SQL statements are pieced together. the format should be as follows:

Update $ tablename set... where $ prekeyname = $ data [$ prekeyname]

The first two steps are the same as insertData ()

(3) traverse the character array of the column name to obtain the column name value of the non-primary key name, and store the column name value in the array in the string format of "column name = column name value". here is the incomplete string after the set

(4) convert (3) the result to a string and separate the array elements by commas (,). This is the final string format after the set: "xx = xx, xx = xx"

(5) splice SQL strings and update the database

The insertData () and updData () functions are as follows:

/** Add information * @ param: $ objstr: json-style database insert information string * $ tablename: table name */function insertData ($ objstr, $ tablename) {$ dbc = $ this-> conData (); if ($ dbc) {$ columnname = array (); $ columnname = $ this-> getColumns ($ tablename ); // echo $ columnname [0]; $ clos = implode (',', $ columnname); // Convert the column name array to a string // echo $ clos; $ data = json_decode ($ objstr, true); // Convert a json string to an associated array // echo $ value ['keychartname']; $ values = array (); foreach ($ columnname as $ value) {// query data based on the queried column name. if the data is empty, the value is NULL, prevents misplacement of database insert values // echo $ data [$ value]."
"; If (isset ($ data [$ value]) {array_push ($ values, $ data [$ value]);} else {$ data [$ value] = NULL; array_push ($ value, $ data [$ value]) ;}}$ strvalue = implode (',', $ values); // echo $ strvalue;/** SQL: insert into $ tablename ($ clos) values (...) */$ SQL = < ConData (); if ($ dbc) {$ columnname = array (); $ columnname = $ this-> getColumns ($ tablename); // $ clos = implode (', ', $ columnname); // Convert the column name array to a string $ data = json_decode ($ objstr, true ); // Convert a json string to an associated array $ sets = array (); foreach ($ columnname as $ value) {// The column name is not equal to the value obtained by the primary key name if ($ prekeyname! = $ Value) {// set $ value = $ data [$ value]; array_push ($ sets, "$ value = $ data [$ value]"); // set statement part} // $ sets array is converted to string $ stringsets = implode (',', $ sets); // echo $ stringsets; /** SQL: update $ tablename set ..... where $ prekeyname = $ data [$ prekeyname]; **/$ SQL = <

The above is all the content in this article. I hope you will like it.

Middleware (php), easyuidatagrid first recognized the general database operation class-front-end easyui-datagrid, form (php), to achieve code ratio...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.