Home >Backend Development >PHP Tutorial >Front-end easyui-datagrid and form (php)

Front-end easyui-datagrid and form (php)

PHPz
PHPzOriginal
2016-05-16 20:09:081627browse

First introduction to the general database operation class - front-end easyui-datagrid, form (php), the implementation code is relatively simple, please see below for the specific implementation steps.

Function implementation:

The datagrid on the left displays brief information, the right displays detailed information on the selected row, database additions, deletions and modifications

(1) Click to select the row, and detailed information will be displayed on the right. The [New], [Modify], and [Delete] buttons are available, and the [Save] button is disabled

(2) Click [New] ] button, [Modify], [Delete] buttons are disabled, [Save] button is enabled

(3) Click the [Modify] button, [Add], [Delete] button is disabled

Difficulty: insert method and update method in general database operation class

Final rendering:

The front-end function is not very complete, and the logic between buttons is still There is a bit of a problem. Finally, the front-end code is added

. Formain.php judges the value passed to the front-end and calls actSQL.class.php to obtain the result.

The code is relatively simple

As follows:

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 Formain.php and briefly think about them

getAllData($tablename) All the information in the table, this method is relatively simple, it can be done with a simple sql statement, and finally the result can be returned in json format

delData($prekey,$prekeyname,$tablename) to delete the specified information, this is more Simple, I won’t go into details

insertData( $objectstr,$tablename ), where $objectstr is a string in json format, $tablename table name,

The difficulty is to piece it together into insert into $ tablename (......) values(........) This kind of statement

Solution:

(1) Get all the table names based on $tablename Column names, and convert the column name array into a string to prepare for the piecing together of the final sql statement. In addition to obtaining the column name, there is another purpose. See below

(2) Convert the string in json format Convert $objectstr to an associative array, call json_decode() method

Supplementary json_decode() method

mixed json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [ , int $options = 0 ]]] )
Accepts a JSON format string and converts it into a PHP variable, where assoc, when the parameter is TRUE , will return an associative array .

(3) Query the data in the order of the queried column names. When the data is empty, assign the value to NULL and store the result in an array to prevent misaligned values ​​inserted into the database (another reason for obtaining column names)

(4) Convert the result of (3) into a string and call the implode() method

Supplement the implode() method:

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

(5) Piece together the sql statement string, and then insert

into the database The difficulty of the updData($objstr,$prekeyname,$tablename) method is also the string assembly of the SQL statement. The assembly format should be as follows

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

The first two steps are the same as insertData()

(3) Traverse the column name character array, obtain the column name value of the non-primary key name, and follow the string of "column name = column name value" The format is stored in the array, here is the incomplete string

after the set (4) Convert the result of (3) into a string, separate the array elements with ',' , this is the last part after the set The string format is "xx=xx,xx=xx"

(5) Piece together the sql string, and then update the database

insertData() and updData() functions are as follows

/*
   * 添加信息
   * @param:$objstr:json风格的数据库插入信息字符串
   *   $tablename:表名
   */
  function insertData($objstr,$tablename)
  {
    $dbc=$this->conData();
    if($dbc)
    { 
     $columnname=array();
     $columnname=$this->getColumns($tablename);
     //echo $columnname[0];
     $clos=implode(',',$columnname); //将列名数组转换为字符串
     //echo $clos;
     $data=json_decode($objstr,true); //将json格式的字符串转换为关联数组
     //echo $value['keychartname'];
     $values=array();
     foreach($columnname as $value)
     {
       //按照查询到的列名查询数据,数据为空的,赋值为NULL,防止数据库插入数值错位
       //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=<<

That’s the entire content of this article, I hope you all like it. For more related tutorials, please visit A complete set of video tutorials on PHP programming from entry to mastery

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Related articles

See more