First common database operation class--front-end Easyui-datagrid,form (PHP) _php instance

Source: Internet
Author: User
Tags json string format

First common database operation class-front-end Easyui-datagrid,form (PHP), the implementation of the code is relatively simple, the specific implementation steps please see below.

Implementation features:

Left DataGrid displays a brief message, the right side shows the selected row details, database additions and deletions change

(1) Click on the selected row, display the details on the right, where the "add", "Modify", "delete" button available, "Save" button disabled

(2) Click the "Add" button, "Modify", "delete" button disabled, "Save" button enabled

(3) Click the "Modify" button, "Add", "delete" button disabled

Difficulty: Insert method and Update method in common database operation class

Final Effect Diagram:

The front end function is not very perfect, the logic between the buttons is still a little problem, finally supplemental front-end code

In which formain.php to the front end of the value of judgment, and call actSQL.class.php to obtain results

The code is relatively simple

As follows:

<?php require (' include/mysql_connect/actsql.class.php ');
  $key =$_request[' key '];
  $a =new actsql (' localhost ', ' root ', ' 1234 ', ' TPSS ');
  Get information if ($key = = ' 1 ') {$a->getalldata (' T_prekeychart ');   
   } if ($key = = ' 2 ') {$objectstr =$_request[' object '];
   if ($a->insertdata ($objectstr, ' T_prekeychart ')) {echo Json_encode ("true");
   }else{echo Json_encode ("false"); }//test//$test = ' {"Keychartid": "2", "keyID": "2", "Keychartname": "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 ($a->upddata ($objectstr, $prekeyname, ' T_prekeychart ')) {echo Json_encoDe ("true");
   }else{echo Json_encode ("false");
 }}?>

Look at the various methods of the classes that appear in the formain.php, and simply think about

which

Getalldata ($tablename) Get all the information about the table, this method is simpler, a simple SQL statement can be done, and finally return the result JSON format

Deldata ($prekey, $prekeyname, $tablename) Delete the specified information, this is simpler, not to say

InsertData ($objectstr, $tablename), where $objectstr is a JSON-formatted string, $tablename table name,

The difficulty is to put together the insert into $tablename (...) values (...)

Solution:

(1) Get all the column names according to $tablename, and convert the column array group to a string, prepare for the final piece of SQL statement, in addition to getting the column name there is a purpose, look down

(2) Converting a JSON-formatted string $OBJECTSTR to an associative array, calling the Json_decode () method

Supplemental Json_decode () method

Mixed Json_decode (String $json [, bool $assoc = False [, int $depth = [, int $options = 0]]])
Accepts a JSON-formatted string and converts it to a PHP variable, where assoc, which returns an associative array when the argument is TRUE.

(3) According to the query to the column name order query data, the data is empty, the assignment is null, the results are saved to the array, to prevent the database from inserting numerical dislocation (get column name for another reason)

(4) Convert the result of (3) to a string and invoke the implode () method

Supplemental Implode () method:

String implode (string glue, array pieces);
This function combines the contents of the array into a string, and the argument glue is the delimiter between words

(5) Piecing together the SQL statement string and inserting it into the database

Upddata ($objstr, $prekeyname, $tablename) method difficulty is also in the string patchwork of SQL statements, the patchwork format should be as follows

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

The first two steps with InsertData ()

(3) Iterate through the array of column names, get the column name value of the non primary key name, and save the array in the string format of "Column name = Column Name Value", here is the incomplete string in the later part of set

(4) converts (3) The result to a string, separating the elements of the array by ', ', which is the last string format after set as "Xx=xx,xx=xx"

(5) Piecing together SQL strings and then updating 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);
     Converts a column array group to a string//echo $clos; $data =json_decode ($objstr, true);
     Converts a string in JSON format to an associative array//echo $value [' Keychartname '];
     $values =array (); foreach ($columnname as $value) {//query data according to the queried column name, the data is null, the assignment is null, and the database is prevented from inserting a numeric dislocation//echo $data [$value]. "
       <br> ";
       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 =<<<sql INSERT INTO $tablename ($clos) values ($strvalue);
SQL;     Echo $sql;
     $res =mysqli_query ($DBC, $sql);
     if ($res) {return true;
     }else{return false;
    }else{echo "Connection Error!";  /* * Update information * @param: $OBJSTR: JSON-style database update information string * $tablename: Table name * $prekeyname: PRIMARY KEY Name */function
    Upddata ($objstr, $prekeyname, $tablename) {$dbc = $this->condata ();
     if ($DBC) {$columnname =array (); 
     $columnname = $this->getcolumns ($tablename); $clos =implode (', ', $columnname); Converts the column array group to a string $data =json_decode ($objstr, true);
     Converts a string in JSON format to an associative array $sets =array (); foreach ($columnname as $value) {//Column name is not equal to primary key name get value if ($prekeyname!= $value) {//set $value = $dat
        a[$value]; Array_push ($sets, "$value = $data [$value]"); the Set statement part}//$sets array is converted to a string $stringsets =implode (', ', $se
     TS);
     Echo $stringsets; 
     * * Sql:update $tablename set ... where $prekeyname = $data [$prekeyname]; * * * */$sql =&LT;<<sql Update $tablename set $stringsets where $prekeyname = $data [$prekeyname];
     SQL;
     $res =mysqli_query ($DBC, $sql);
     if ($res) {return true;
     }else{return false;
    }else{echo "Connection error"; * * Get all column names of the table * @param: $tablename: Table name/function GetColumns ($tablename) {$dbc = @mysqli_connect (' Local
   Host ', ' root ', ' 1234 ', ' information_schema ');
   if (! $dbc) {echo "Connect Error". Mysqli_connect_error ($DBC);
    }else {//connection succeeded, get all column names from table columns $sql = "SELECT column_name from Columns where table_name= ' $tablename '";
    $res = @mysqli_query ($dbc, $sql);
    $items =array ();
      if ($res) {while ($row =mysqli_fetch_array ($res, Mysqli_assoc)) {$columnname = $row [' column_name '];
     Array_push ($items, $columnname);
    return $items;
    Mysqli_close ($DBC);
     else{echo "Query failed, please check SQL statement!";
 }
   }
  }

The above is the entire content of this article, I hope you like.

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.