PHP jqGrid 表格数据更新帮助代码

来源:互联网 发布:西安高新区软件公寓 编辑:程序博客网 时间:2024/04/28 09:23

数据操作类:

<?phpclass DBHelper{public $conn;/** * 得到数据库连接 成功返回true,失败返回false * Enter description here ... * @param unknown_type $user * @param unknown_type $pwd * @param unknown_type $dbname * @param unknown_type $host */function getConn($user,$pwd,$dbname,$host = "localhost"){    $this->conn = mysql_connect($host,$user,$pwd);    if (!$this->conn)    {    return false;    }mysql_select_db($dbname,$this->conn);return true;    }/** * 返回查询的result * @param unknown_type $table 待查询的表 * @param unknown_type $cols 待查询的列的数组 如array("col1","col2"); * 如果想给列 重命名 则使用 array("newcol1"=>"col1","newcol2"=>"col2") ;  * null 表示查询所有 * @param unknown_type $conditions where后的条件数组按如此格式来 array("col"=>array("> xx"=>"AND","< yy"=>"")) 表示 col 列 大于 xx  * 并且小于yy的结果 。array("col"=>array("LIKE 'xx%'"=>"")) 模糊搜索 * 注意xx,yy 为字符串时要用''扩住,中间的条件才能用AND 或者OR,末尾条件用""表示 * null 表示无条件 * @param unknown_type $order 排序数组 如 array("col1"=>"asc","col2"=>"desc"); null 表示默认排序 * @param unknown_type $limit 数据条目,如 array(1=>10) 表示limit 1,10;  null 表示默认 */function getRow($table,$cols,$conditions,$order,$limit){$sql = "SELECT ";$tmp = "";if($cols != null){while((list($k,$v) = each($cols)) !== false){$tmp =$tmp.$v.",";if(!is_numeric($k)){$tmp = substr($tmp, 0,strlen($tmp)-1)." as {$k},";}}$tmp = substr($tmp, 0,strlen($tmp)-1)." FROM {$table}";}else {$tmp = "* FROM {$table}";}$sql = $sql.$tmp;if($conditions != null){$tmp = " WHERE ";while((list($k,$v) = each($conditions)) !== false){if((list($k1,$v1) = each($v)) !== false){$tmp = $tmp."{$k} {$k1} {$v1}"; }}$tmp = rtrim($tmp);$sql = $sql.$tmp;}if($order != null){$tmp = " ORDER BY ";while((list($k,$v) = each($order)) !== false){$tmp = $tmp."{$k} {$v},"; }$tmp = substr($tmp, 0,strlen($tmp)-1);$sql = $sql.$tmp;}if($limit != null){$tmp = " limit ";if((list($k,$v) = each($limit)) !== false){$tmp = $tmp."{$k},{$v}";}$sql = $sql.$tmp;}//echo $sql;$result = mysql_query($sql);return $result;}function closeDb(){if($this->conn != null)mysql_close($this->conn);}/** *  * 返回mysql标准时间字符串 * 注意这个date的获得和php的配置有关系,而php默认返回的UTC * 如果不符合你的标准,需要在php.ini中将配置做相应的更改 */function getDateStr(){return date("Y-m-d H:i:s");}}?>

response封装:

<?phpinclude 'dbhelper.php';class Response{public $total = 0;public $records = 0;public $page; // get the requested pagepublic $rows =array();private  $limit; // get how many rows we want to have into the gridprivate  $sidx; // get index row - i.e. user click to sortprivate  $sord; // get the directionfunction Response($page,$rows,$sidx,$sord){$this->page = $page;$this->limit = $rows;$this->sidx = $sidx;$this->sord = $sord;if(!$this->sidx)$this->sidx = 1;}/** * 通过指定条件查询将结果填入response中 * @param unknown_type $table 表名 * @param unknown_type $cols 待查询的列的数组 如array("col1","col2"); * 如果想给列 重命名 则使用 array("newcol1"=>"col1","newcol2"=>"col2") 为null 表示查询所有 * @param unknown_type $conditions where后的条件数组按如此格式来 array("col"=>array("> xx"=>"AND","< yy"=>"")) 表示 col 列 大于 xx  * 并且小于yy的结果。array("col"=>array("LIKE 'xx%'"=>"")) 模糊搜索 * 注意xx,yy 为字符串时要用''扩住,中间的条件才能用AND 或者OR,末尾条件用""表示 * null 表示无条件 *     ;null 表示无条件 */ public function fillResponse($table,$cols,$conditions){$helper = new DBHelper();if($helper->getConn("root", "root", "dbcd")){$result = $helper->getRow($table, null, $conditions,null,null);if($result){$count = mysql_num_rows($result);$this->records = $count;if($count > 0){$this->total = ceil($count/$this->limit);}else{$this->total = 0;}if($this->page > $this->total)$this->page = $this->total;$start = $this->limit*$this->page - $this->limit;if($start < 0) $start = 0;$result = $helper->getRow($table, $cols, $conditions, array($this->sidx=>$this->sord), array($start=>$this->limit));$i = 0;while(($row=mysql_fetch_array($result,MYSQL_ASSOC))!=false){$this->rows[$i] = $row;$i++;}}else{// 查询失败}$helper->closeDb();}else {// 未成功连接数据库}}}?>

使用:

include 'php/response.php';$conditon = null;$response = new Response($_GET['page'], $_GET['rows'], $_GET['sidx'], $_GET['sord']);if(filter_has_var(INPUT_GET, "searchField")){$conditon = array($_GET['searchField']=>array("LIKE '{$_GET['searchString']}%'"=>""));}$response->fillResponse("factory",null, $conditon);print_r(json_encode($response));


As mentioned above posting search data differs from custom and toolbar searching. When the find button is clicked, jqGrid adds three parameters to the url (again with _search=true), in name=value pairs:

  • sField: the 'searchField', the value comes from the index in colModel
  • sValue: the 'searchString', the value is the entered value
  • sOper: the 'searchOper', the value is the type of search - see sopt array

For example if the field index is invid, operation is equal, and the searched value is 123, then the string that is posted to the server look like:

http://localhost/demo35/server.php?...&searchField=invid&searchString=123&searchOper=eq

详见:http://www.trirand.com/jqgridwiki/doku.php?id=wiki:singe_searching