Advertisement
Donaldini

jqGrid Example.php

Jun 7th, 2011
681
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.16 KB | None | 0 0
  1. <?php
  2.     $dbhost = 'localhost';
  3.     $dbuser = 'root';
  4.     $dbpassword = 'root';
  5.     $database = 'stage';
  6.    
  7.     // to the url parameter are added 4 parameters as described in colModel
  8.     // we should get these parameters to construct the needed query
  9.     // Since we specify in the options of the grid that we will use a GET method
  10.     // we should use the appropriate command to obtain the parameters.
  11.     // In our case this is $_GET. If we specify that we want to use post
  12.     // we should use $_POST. Maybe the better way is to use $_REQUEST, which
  13.     // contain both the GET and POST variables. For more information refer to php documentation.
  14.    
  15.    
  16.        
  17.         $page = $_GET['page'];                                                              // Get the requested page. By default grid sets this to 1.
  18.         $limit = $_GET['rows'];                                                             // get how many rows we want to have into the grid - rowNum parameter in the grid            
  19.         $sidx = $_GET['sidx'];                                                              // get index row: user click to sort. At first time sortname parameter,
  20.                                                                                             // after that the index from colModel
  21.         $sord = $_GET['sord'];                                                              // sorting order - at first time sortorder
  22.         if(!$sidx) $sidx =1;                                                                // if we not pass at first time index use the first column for the index or what you want
  23.          
  24.         $db = mysql_connect($dbhost, $dbuser, $dbpassword) or die("Connection Error: " . mysql_error());        // connect to the MySQL database server
  25.         mysql_select_db($database) or die("Error connecting to db.");                                           // select the database
  26.          
  27.         // calculate the number of rows for the query. We need this for paging the result
  28.         $result = mysql_query("SELECT COUNT(*) AS count FROM sanatorium");
  29.         $row = mysql_fetch_array($result,MYSQL_ASSOC);
  30.         $count = $row['count'];
  31.          
  32.         // calculate the total pages for the query
  33.         if( $count > 0 && $limit > 0) {
  34.             $total_pages = ceil($count/$limit);
  35.         } else {
  36.             $total_pages = 0;
  37.         }
  38.          
  39.         // if for some reasons the requested page is greater than the total
  40.         // set the requested page to total page
  41.         if ($page > $total_pages) $page=$total_pages;
  42.        
  43.         $start = $limit*$page - $limit;                                                                     // calculate the starting position of the rows
  44.    
  45.         if($start <0) $start = 0;                                                                           // if for some reasons start position is negative set it to 0
  46.                                                                                                             // typical case is that the user type 0 for the requested page
  47.        
  48.         $SQL = "SELECT name, description FROM sanatorium ORDER BY $sidx $sord LIMIT $start , $limit";
  49.         $result = mysql_query( $SQL ) or die("Couldn't execute query.".mysql_error());
  50.          
  51.         header("Content-type: text/xml;charset=utf-8");                                                     // we should set the appropriate header information. Do not forget this.
  52.         $s = "<?xml version='1.0' encoding='utf-8'?>";
  53.         $s .=  "<rows>";
  54.         $s .= "<page>".$page."</page>";
  55.         $s .= "<total>".$total_pages."</total>";
  56.         $s .= "<records>".$count."</records>";
  57.          
  58.         while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {                                              // be sure to put text data in CDATA
  59.             $s .= "<row >";            
  60.             $s .= "<cell>". $row['name']."</cell>";
  61.             $s .= "<cell>". $row['description']."</cell>";
  62.             $s .= "</row>";
  63.         }
  64.         $s .= "</rows>";
  65.          
  66.         echo $s;
  67.  
  68. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement