title: jqgrid and server side pagination
body: <p>I realize there are some related questions but I couldn't find what I was looking for.  I used jqgrid many times in the past but forgot how to achieve server side pagination.</p>
  
  <p>here is my javascript</p>
  
  <pre><code>$("#list").jqGrid({
      url: "index.php?loadData=test",
      datatype: "json",
      mtype: "GET",
      colNames: ["id", "eNodeB_unique", "enodeB_type", "radio_freq_mod", "macroEnbId_dec representation", "num_cells"],
      colModel: [
          { name: "id", width: 55 },
          { name: "enodeB_unique", width: 90 },
          { name: "enodeB_type", width: 80, align: "right" },
          { name: "radio_freq_mod", width: 80, align: "right" },
          { name: "macroEnbId_dec_rep", width: 80, align: "right" },
          { name: "num_cells", width: 150, sortable: false }
      ],
      pager: "#pager",
      rowNum: 10,
      rowList: [10, 20, 30],
      sortname: "id",
      sortorder: "desc",
      viewrecords: true,
      gridview: true,
      autoencode: true,
      caption: "My first grid",
      loadonce:false
  }); 
  </code></pre>
  
  <p>and my server side code</p>
  
  <pre><code>public function getData($page, $limit, $sidx, $sord){
  
      $query_str = "SELECT COUNT(*) AS count FROM tbl";
  
      $prepState = $this-&gt;DBi-&gt;prepare($query_str);             
      $result = $this-&gt;DBi-&gt;query($prepState);
  
      $count = $result[0]['count'];
  
  
      if( $count &gt; 0 &amp;&amp; $limit &gt; 0) { 
          $total_pages = ceil($count/$limit); 
      } else { 
          $total_pages = 0; 
      } 
  
      if ($page &gt; $total_pages){
          $page = $total_pages;
      }
  
      $start = $limit * $page - $limit;
  
      if($start &lt; 0){
          $start = 0;
      }
  
  $query_str = "SELECT * FROM tbl ORDER BY {$sidx} {$sord} LIMIT {$start}, {$limit}";
  
      $prepState = $this-&gt;DBi-&gt;prepare($query_str);             
      $result = $this-&gt;DBi-&gt;query($prepState);        
  
      return $result;     
  
  
  }
  </code></pre>
  
  <p>if I keep $start and $limit in the query then i just get the inital ten results.  If I take those out... then my grid shows all my results.. but there is only one page available.  I have on option to click on the next page.  </p>
  
  <p>EDIT:</p>
  
  <p>okay I realize now that I have to return this information..  I'm puzzled by the way I have to return the rows.  Was JQgrid always this way?</p>
  
  <pre><code>$query_str = "SELECT * FROM enodeB ORDER BY {$sidx} {$sord} LIMIT {$start}, {$limit}";
  
      $prepState = $this-&gt;DBi-&gt;prepare($query_str);             
      $result = $this-&gt;DBi-&gt;query($prepState);        
  
      $finalRows = array();
  
      foreach($result as $row){
          $finalRows[] = array('cell'=&gt; $row);
  
      }
      return array('page' =&gt; $page, 'total' =&gt; $total_pages, 'records' =&gt; $count, 'rows' =&gt; $finalRows);
  </code></pre>