2012-10-23 43 views
0

首先,我在我的應用程序中使用JQGrid。將我的CSS更改爲Bootstrap CSS後。 我必須使用DataTable。我想重用我的控制器頁面,它使查詢顯示JQGrid中的數據。PHP:重用JQGrid響應顯示數據表中的數據

這是我的控制器頁面。

<?php 

require_once("JsonHeader.php"); 
$at = $_SESSION['abc']; 
$start_date = $_SESSION['start_date']; 
$end_date = $_SESSION['end_date']; 
if ($_SESSION['list'] == '-1') { 
    $user_query = ""; 
} else { 
    $user_list = $_SESSION['list']; 
    $user_query = " AND a.user_id IN ($list)"; 
} 
$start_date = $_SESSION['start_date']; 
$end_date = $_SESSION['end_date']; 

if ($_SESSION['list'] == '-1') { 
    $user_query = ""; 
} else { 
    $user_list = $_SESSION['list']; 
    $user_query = " AND a.user_id IN ($list)"; 
} 

$page = $_GET['page']; // get the requested page 
$limit = $_GET['rows']; // get how many rows we want to have into the grid 
$sidx = $_GET['sidx']; // get index row - i.e. user click to sort 
$sord = $_GET['sord']; // get the direction 
$qtype = ''; // Search column 
$query = ''; // Search string 
if (isset($_GET['searchField'])) { 
    $qtype = mysql_real_escape_string($_GET['searchField']); 
} 
if (isset($_GET['searchString'])) { 
    $query = mysql_real_escape_string($_GET['searchString']); 
} 
if (isset($_GET['searchOper'])) { 
    switch ($_GET['searchOper']) { 
     case 'eq': 
      $oper = '='; 
      $query = mysql_real_escape_string($_GET['searchString']); 
      break; 
     case 'ne': 
      $oper = '!='; 
      $query = mysql_real_escape_string($_GET['searchString']); 
      break; 
     case 'cn': 
      $oper = 'like'; 
      $query = "%".mysql_real_escape_string($_GET['searchString'])."%"; 
      break; 
    } 
} 
$searchSql1 = ($qtype != '' && $query != '') ? "and $qtype $oper '$query'" : ''; 

if (!$sidx) 
    $sidx = 1; // connect to the database 
$result = mysql_query(sprintf("SELECT ae.col1,ae.col2,ae.col3, ae.col4,ae.col5,ae.col6,ae.col7,a.col8,a.col9,r.col10,p.col11 
          FROM $tablename3 ae,$tableName a, $tablename1 p, $tablename2 r 
          WHERE ae.col1=$at $searchSql1 
          AND a.col1 = $at 
          AND a.col5=r.col5 
          AND a.col6=p.col6 
          $user_query")); 
$row = mysql_num_rows($result); 
$count = $row; 
if ($count > 0) { 
    $total_pages = ceil($count/$limit); 
} else { 
    $total_pages = 0; 
} 
if ($page > $total_pages) 
    $page = $total_pages; $start = $limit * $page - $limit; // do not put 
$limit * ($page - 1); 
$SQL = sprintf("SELECT ae.col1,ae.col2,ae.col3, ae.col4,ae.col5,ae.col6,ae.col7,a.col8,a.col9,r.col10,p.col11 
          FROM $tablename3 ae,$tableName a, $tablename1 p, $tablename2 r 
          WHERE ae.col1=$at $searchSql1 
          AND a.col1 = $at 
          AND a.col5=r.col5 
          AND a.col6=p.col6 
          $query"); 

$result = mysql_query($SQL) or die("Couldn t execute query." . mysql_error()); 
$responce = new stdClass(); 
$responce->page = new stdClass(); 
$responce->total = new stdClass(); 
$responce->records = new stdClass(); 

$responce->page = $page; 
$responce->total = $total_pages; 
$responce->records = $count; 
$i = 0; 
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { 


    $responce->rows[$i]['id'] = $row['col1']; 
    $responce->rows[$i]['cell'] = array($row['col1'], $row['col2'], $row['col3'], $row['col4'], $row['col5'], $row['col6'], $time, $row['col7']); 
    $i++; 
} 
echo json_encode($responce); 
?> 

如何更改此數據表? 請給我提供最好的方法...

回答

0

最後我發現如何重新啓動DataTable的控制器頁面。只需更改控制器頁面中的發佈變量即可。 Like

$_GET['page']; By $_REQUEST['sEcho'] 
$_GET['rows']; By $_REQUEST['iDisplayStart'],$_REQUEST['iDisplayLength'] 
$_GET['sord']; $_REQUEST['sSortDir_0'] 
$_GET['searchString']: $_GET['sSearch']: 

And some changes the json response like 
$ans['iTotalRecords'] = $count; 
$ans['iTotalDisplayRecords'] = $count; 
$ans['aaData'] = $aadata; 

echo json_encode($ans);