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
- PHP jqGrid 表格数据更新帮助代码
- jqgrid 表格数据导出
- jqGrid清除界面表格数据
- Jqgrid获取所有表格数据数据
- jQuery表格插件jqGrid之JSON数据
- jQuery 表格插件jqGrid 本地数据
- Jqgrid入门-操作表格的数据(二)
- 实现jqgrid表格数据上下滚动
- Jqgrid入门-操作表格的数据(二)
- php导出表格数据
- javascript表格jqGrid下拉ComboBox,jqGrid弹出窗jquery.dialog,jqGrid回车换行编辑切换单元格,jqGrid提交全部表格数据ajax
- jqGrid表格插件——从servlet获得数据
- 关于JqGrid + MVC 提交表格数据,提交多行数据
- Jqgrid入门-使用模态对话框编辑表格数据(三)
- jqGrid中文文档(9)–subGrid(子数据表格)
- jqgrid中对表格数据操作的方法
- 数组数据转换成json形式,用于jqgrid初始化表格
- jqgrid中对表格数据操作的方法
- 末日首发
- Mysql 修改字符集
- iOS 声明属性关键字讲解
- elf文件格式与动态链接库
- iphone App的国际化
- PHP jqGrid 表格数据更新帮助代码
- ACdream 1038 (想法+排序)
- webservice快速入门-使用wsimport生成ws服务端(二)
- Android接口定义语言(AIDL)
- C++逗号表达式
- 12个有趣的C语言面试题
- 如何成为一名程序员
- 内存管理
- Poj 3348 Cows (凸包面积)