【问题标题】:Double WHERE query on server side to create jqgrid json在服务器端进行双 WHERE 查询以创建 jqgrid json
【发布时间】:2019-02-26 10:41:51
【问题描述】:

我使用服务器端在 jqgrid 上创建 json。

我的问题是当我想在 mySQL 查询中包含 WHERE 时,因为 WHERE 已用于搜索运算符。

  • 服务器端代码是

    <?php 
    
    include("dbconfig.php");
    $page = $_REQUEST['page']; 
    $limit = $_REQUEST['rows']; 
    $sidx = $_REQUEST['sidx']; 
    $sord = $_REQUEST['sord']; 
    
    // if we not pass at first time index use the first column for the index or what you want
    if(!$sidx) $sidx =1; 
    
    //array to translate the search type
    $ops = array(
        'eq'=>'=', //equal
        'ne'=>'<>',//not equal
        'lt'=>'<', //less than
        'le'=>'<=',//less than or equal
        'gt'=>'>', //greater than
        'ge'=>'>=',//greater than or equal
        'nc'=>'NOT LIKE'  //doesn't contain
    );
    function getWhereClause($col, $oper, $val){
        global $ops;
        if($oper == 'bw' || $oper == 'bn') $val .= '%';
        if($oper == 'ew' || $oper == 'en' ) $val = '%'.$val;
        if($oper == 'cn' || $oper == 'nc' || $oper == 'in' || $oper == 'ni') $val = '%'.$val.'%';
        return " WHERE $col {$ops[$oper]} '$val' ";
    }
    $where = ""; //if there is no search request sent by jqgrid, $where should be empty
    $searchField = isset($_GET['searchField']) ? $_GET['searchField'] : false;
    $searchOper = isset($_GET['searchOper']) ? $_GET['searchOper']: false;
    $searchString = isset($_GET['searchString']) ? $_GET['searchString'] : false;
    if ($_GET['_search'] == 'true') {
        $where = getWhereClause($searchField,$searchOper,$searchString);
    //  var_dump($where);
    
    }
    
     $totalrows = isset($_REQUEST['totalrows']) ? $_REQUEST['totalrows']: false;
    if($totalrows) {
        $limit = $totalrows;    
    }
    
    $db = mysql_connect($dbhost, $dbuser, $dbpassword) or die("Connection Error: " . mysql_error()); 
    mysql_select_db($database) or die("Error connecting to db."); 
     if ($limit<0) $limit = 0;
    // calculate the number of rows for the query. We need this for paging the result 
    $result = mysql_query("SELECT * FROM enemy_coords"); 
    $row = mysql_fetch_array($result,MYSQL_ASSOC); 
    $count = $row['count']; 
            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)
            if ($start<0) $start = 0;
    
    // the actual query for the grid data 
    $SQL = "SELECT * FROM enemy_coords ".$where." ORDER BY $sidx $sord LIMIT $start , $limit"; 
    $result = mysql_query( $SQL ) or die("Couldn't execute query.".mysql_error()); 
    
    $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[ID];
        $responce->rows[$i]['cell']=array($row[ID],$row[ID],$row[alliance_name],$row[player_name],$row[player_lvl],$row[player_might],$row[city1_coords]);
        $i++;
    }        
    
    echo json_encode($responce);
    ?>
    

我的问题是我是否将 mysql 查询更改为

SELECT * FROM enemy_coords WHERE category <> 'A',
where should i put the "WHERE category = 'A'"?

【问题讨论】:

    标签: jqgrid jqgrid-php


    【解决方案1】:

    这里有很多选择。一种可能的方法是将初始 $where 设置为此条件,然后根据您的要求在搜索打开时为其余位置添加 AND 或 OR。为了执行此操作,应从 getWhereClause 函数中删除 WHERE 运算符:

    function getWhereClause($col, $oper, $val){
        global $ops;
        if($oper == 'bw' || $oper == 'bn') $val .= '%';
        if($oper == 'ew' || $oper == 'en' ) $val = '%'.$val;
        if($oper == 'cn' || $oper == 'nc' || $oper == 'in' || $oper == 'ni') $val = '%'.$val.'%';
        return " $col {$ops[$oper]} '$val' ";
    }
    $where = "WHERE category = 'A'"; 
    ...
    if ($_GET['_search'] == 'true') {
        $where = $where. " AND ". getWhereClause($searchField,$searchOper,$searchString);
    //  var_dump($where);
    
    }
    ...
    // the actual query for the grid data 
    $SQL = "SELECT * FROM enemy_coords ".$where." ORDER BY $sidx $sord LIMIT $start , $limit"; 
    $result = mysql_query( $SQL ) or die("Couldn't execute query.".mysql_error()); 
    

    当然,为了做到这一点,还需要执行更多的检查条件,但这只是一个方向。

    【讨论】:

      猜你喜欢
      • 2011-06-20
      • 1970-01-01
      • 1970-01-01
      • 2021-06-04
      • 2012-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多