Guest User

jqgrid

a guest
Jul 8th, 2011
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.97 KB | None | 0 0
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>My First Grid</title>
  6.  
  7. <link rel="stylesheet" type="text/css" media="screen" href="css/ui-lightness/jquery-ui-1.8.14.custom.css" />
  8. <link rel="stylesheet" type="text/css" media="screen" href="css/ui.jqgrid.css" />
  9.  
  10. <script src="js/jquery-1.5.2.min.js" type="text/javascript"></script>
  11. <script src="js/i18n/grid.locale-en.js" type="text/javascript"></script>
  12. <script src="js/jquery.jqGrid.min.js" type="text/javascript"></script>
  13.  
  14. <style>
  15. html, body {
  16.     margin: 0;
  17.     padding: 0;
  18.     font-size: 75%;
  19. }
  20. </style>
  21.  
  22. <script type="text/javascript">
  23. $(function(){
  24.   $("#list").jqGrid({
  25.     url:'index.php',
  26.     datatype: 'xml',
  27.     mtype: 'GET',
  28.     colNames:['Inv No','Date', 'Amount','Tax','Total','Notes'],
  29.     colModel :[
  30.       {name:'invid', index:'invid', width:55},
  31.       {name:'invdate', index:'invdate', width:90},
  32.       {name:'amount', index:'amount', width:80, align:'right'},
  33.       {name:'tax', index:'tax', width:80, align:'right'},
  34.       {name:'total', index:'total', width:80, align:'right'},
  35.       {name:'note', index:'note', width:150, sortable:false}
  36.     ],
  37.     pager: '#pager',
  38.     rowNum:10,
  39.     rowList:[10,20,30],
  40.     sortname: 'invid',
  41.     sortorder: 'desc',
  42.     viewrecords: true,
  43.     gridview: true,
  44.     caption: 'My first grid'
  45.   });
  46. });
  47. </script>
  48.  
  49. </head>
  50.  
  51. <body>
  52.  
  53. <table id="list"><tr><td/></tr></table>
  54. <div id="pager"></div>
  55.  
  56.  
  57. </body>
  58. </html>
  59.  
  60.  
  61.  
  62. <?php
  63. //include the information needed for the connection to MySQL data base server.
  64. // we store here username, database and password
  65. //include("dbconfig.php");
  66.  
  67. // to the url parameter are added 4 parameters as described in colModel
  68. // we should get these parameters to construct the needed query
  69. // Since we specify in the options of the grid that we will use a GET method
  70. // we should use the appropriate command to obtain the parameters.
  71. // In our case this is $_GET. If we specify that we want to use post
  72. // we should use $_POST. Maybe the better way is to use $_REQUEST, which
  73. // contain both the GET and POST variables. For more information refer to php documentation.
  74. // Get the requested page. By default grid sets this to 1.
  75. $page = 1; //$_GET['page'];
  76.  
  77. // get how many rows we want to have into the grid - rowNum parameter in the grid
  78. $limit = 5; //$_GET['rows'];
  79.  
  80. // get index row - i.e. user click to sort. At first time sortname parameter -
  81. // after that the index from colModel
  82. $sidx = 'invid'; //$_GET['sidx'];
  83.  
  84. // sorting order - at first time sortorder
  85. $sord = $_GET['sord'];
  86.  
  87. // if we not pass at first time index use the first column for the index or what you want
  88. if(!$sidx) $sidx =1;
  89.  
  90. // connect to the MySQL database server
  91. $db = mysql_connect('localhost', 'user', 'pass') or die("Connection Error: " . mysql_error());
  92. $database = 'gridTest';
  93. // select the database
  94. mysql_select_db($database) or die("Error connecting to db.");
  95.  
  96. // calculate the number of rows for the query. We need this for paging the result
  97. $result = mysql_query("SELECT COUNT(*) AS count FROM invheader");
  98. $row = mysql_fetch_array($result,MYSQL_ASSOC);
  99. $count = $row['count'];
  100.  
  101. // calculate the total pages for the query
  102. if( $count > 0 && $limit > 0) {
  103.               $total_pages = ceil($count/$limit);
  104. } else {
  105.               $total_pages = 0;
  106. }
  107.  
  108. // if for some reasons the requested page is greater than the total
  109. // set the requested page to total page
  110. if ($page > $total_pages) $page=$total_pages;
  111.  
  112. // calculate the starting position of the rows
  113. $start = $limit*$page - $limit;
  114.  
  115. // if for some reasons start position is negative set it to 0
  116. // typical case is that the user type 0 for the requested page
  117. if($start <0) $start = 0;
  118.  
  119. // the actual query for the grid data
  120. $SQL = "SELECT invid, invdate, amount, tax,total, note FROM invheader ORDER BY $sidx $sord LIMIT $start , $limit";
  121. $result = mysql_query( $SQL ) or die("Couldn't execute query.".mysql_error());
  122.  
  123. // we should set the appropriate header information. Do not forget this.
  124. header("Content-type: text/xml;charset=utf-8");
  125.  
  126. $s = "<?xml version='1.0' encoding='utf-8'?>";
  127. $s .=  "<rows><br>";
  128. $s .= "<page>".$page."</page>";
  129. $s .= "<total>".$total_pages."</total>";
  130. $s .= "<records>".$count."</records>";
  131.  
  132. // be sure to put text data in CDATA
  133. while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
  134.     $s .= "<row id='". $row['invid']."'>";            
  135.     $s .= "<cell>". $row['invid']."</cell>";
  136.     $s .= "<cell>". $row['invdate']."</cell>";
  137.     $s .= "<cell>". $row['amount']."</cell>";
  138.     $s .= "<cell>". $row['tax']."</cell>";
  139.     $s .= "<cell>". $row['total']."</cell>";
  140.     $s .= "<cell><![CDATA[". $row['note']."]]></cell>";
  141.     $s .= "</row>";
  142. }
  143. $s .= "</rows>";
  144.  
  145. echo $s;
  146. ?>
Advertisement
Add Comment
Please, Sign In to add comment