Double WHERE query on server side to create jqgrid json

孤者浪人 提交于 2021-01-29 07:30:31

问题


I use server side to create json on jqgrid.

My problem is when I want to include WHERE into mySQL query, because WHERE has been used in searching operator.

  • The server side code is

    <?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);
    ?>
    

My question is if i change mysql query to

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

回答1:


There are a lot of options here. One possible is to set the initial $where to this condition and then depending on your requirement to add AND or OR for the rest of where when search is on. In order to perform this the WHERE operator should be removed from the getWhereClause function:

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()); 

Of course more checking conditions should be performed in order to make this fine, but this is just a direction.



来源:https://stackoverflow.com/questions/54883609/double-where-query-on-server-side-to-create-jqgrid-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!