Forum

November 2nd, 2014
A A A
Avatar

Lost password?
Advanced Search

— Forum Scope —




— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

The forums are currently locked and only available for read only access
sp_Feed Topic RSS sp_Related Related Topics sp_TopicIcon
jqGrid advanced search HELP
23/04/2010
14:24
Avatar
settings
New Member
Members
Forum Posts: 1
Member Since:
23/04/2010
sp_UserOfflineSmall Offline

well i have been around this code for a while and i cant understand why i'm getting the table in blank all the time.

if i change the code to the one of  example it works.

So.. the PHP block

<?php
include("dbconfig.php");
// coment the above lines if php 5
//include("JSON.php");
//$json = new Services_JSON();
// end comment
$examp = $_REQUEST["q"]; //query number

$page = $_REQUEST['page']; // get the requested page
$limit = $_REQUEST['rows']; // get how many rows we want to have into the grid
$sidx = $_REQUEST['sidx']; // get index row - i.e. user click to sort
$sord = $_REQUEST['sord']; // get the direction
if(!$sidx) $sidx =1;

$wh = "";
$searchOn = Strip($_REQUEST['_search']);
if($searchOn=='true') {
    $searchstr = Strip($_REQUEST['filters']);
    $wh= constructWhere($searchstr);
    //echo $wh;
}
function constructWhere($s){
    $qwery = "";
    //['eq','ne','lt','le','gt','ge','bw','bn','in','ni','ew','en','cn','nc']
    $qopers = array(
                  'eq'=>" = ",
                  'ne'=>" <> ",
                  'lt'=>" < ",
                  'le'=>" <= ",
                  'gt'=>" > ",
                  'ge'=>" >= ",
                  'bw'=>" LIKE ",
                  'bn'=>" NOT LIKE ",
                  'in'=>" IN ",
                  'ni'=>" NOT IN ",
                  'ew'=>" LIKE ",
                  'en'=>" NOT LIKE ",
                  'cn'=>" LIKE " ,
                  'nc'=>" NOT LIKE " );
    if ($s) {
        $jsona = json_decode($s,true);
        if(is_array($jsona)){
            $gopr = $jsona['groupOp'];
            $rules = $jsona['rules'];
            $i =0;
            foreach($rules as $key=>$val) {
                $field = $val['field'];
                $op = $val['op'];
                $v = $val['data'];
                if($v && $op) {
                    $i++;
                    // ToSql in this case is absolutley needed
                    $v = ToSql($field,$op,$v);
                    if ($i == 1) $qwery = " AND ";
                    else $qwery .= " " .$gopr." ";
                    switch ($op) {
                        // in need other thing
                        case 'in' :
                        case 'ni' :
                            $qwery .= $field.$qopers[$op]." (".$v.")";
                            break;
                        default:
                            $qwery .= $field.$qopers[$op].$v;
                    }
                }
            }
        }
    }
    return $qwery;
}
function ToSql ($field, $oper, $val) {
    // we need here more advanced checking using the type of the field - i.e. integer, string, float
    switch ($field) {
        case 'id_user':
            return intval($val);
            break;
        case 'username':
        case 'nome':
        case 'email':
        case 'telefone':
            return intval($val);
            break;
        case 'ligacao_telefone':
        case 'departamento':
        case 'edificio':
        case 'sala':
        default :
            //mysql_real_escape_string is better
            if($oper=='bw' || $oper=='bn') return "'" . addslashes($val) . "%'";
            else if ($oper=='ew' || $oper=='en') return "'%" . addcslashes($val) . "'";
            else if ($oper=='cn' || $oper=='nc') return "'%" . addslashes($val) . "%'";
            else return "'" . addslashes($val) . "'";
    }
}

//echo $wh;
// connect to the database
$db = mysql_connect($dbhost, $dbuser, $dbpassword)
or die("Connection Error: " . mysql_error());

mysql_select_db($database) or die("Error conecting to db.");

switch ($examp) {
    case 1:
        $result = mysql_query("SELECT COUNT(*) AS count FROM utilizadores");
        $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;
        $SQL = "SELECT id_user, username, nome, email, telefone, ligacao_telefone, departamento, edificio, sala FROM utilizadores WHERE ".$wh." ORDER BY ".$sidx." ".$sord. " LIMIT ".$start." , ".$limit;
        $result = mysql_query( $SQL ) or die("Could not 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_user];
            $responce->rows[$i]['cell']=array($row[id_user],$row[username],$row[nome],$row[email],$row[telefone],$row[ligacao_telefone],$row[departamento],$row[edificio],$row[sala]);
            $i++;
        }
        //echo $json->encode($responce); // coment if php 5
        echo json_encode($responce);
           
        break;
    case 3:
}
mysql_close($db);

function Strip($value)
{
    if(get_magic_quotes_gpc() != 0)
      {
        if(is_array($value))  
            if ( array_is_associative($value) )
            {
                foreach( $value as $k=>$v)
                    $tmp_val[$k] = stripslashes($v);
                $value = $tmp_val;
            }                
            else  
                for($j = 0; $j < sizeof($value); $j++)
                    $value[$j] = stripslashes($value[$j]);
        else
            $value = stripslashes($value);
    }
    return $value;
}
function array_is_associative ($array)
{
    if ( is_array($array) && ! empty($array) )
    {
        for ( $iterator = count($array) - 1; $iterator; $iterator-- )
        {
            if ( ! array_key_exists($iterator, $array) ) { return true; }
        }
        return ! array_key_exists(0, $array);
    }
    return false;
}
?>

JS+HTML

<head>
<style type="text/css">

    table { font: 8pt Arial; }

</style>
<link rel="stylesheet" type="text/css" href="themes/redmond/jquery-ui-1.7.1.custom.css" />
<link rel="stylesheet" type="text/css" media="screen" href="js/src/css/ui.jqgrid.css" />
<link rel="stylesheet" type="text/css" media="screen" href="js/src/css/jquery.searchFilter.css" />
<link rel="stylesheet" type="text/css" media="screen" href="js/src/css/ui.multiselect.css" />

<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.7.1.custom.min.js"></script>
<script src="js/src/grid.loader.js" type="text/javascript"></script>

<script type="text/javascript" src="js/jquery.jqGrid.min.js"></script>

<script type="text/javascript">
$(document).ready(function() {

    jQuery("#s4list").jqGrid({
        url:'search_adv.php?q=1',
        datatype: "json",
        width: 900,
        colNames:['id user','username', 'nome', 'email','telefone','ligacao telefone','departamento', 'edificio','sala'],
        colModel:[
            {name:'id_user',index:'id_user', width:65, searchoptions:{sopt:['eq','ne','lt','le','gt','ge']}},
            {name:'username',index:'username', width:120, search:true},
            {name:'nome',index:'nome', width:100, search:true},
            {name:'email',index:'email', width:80, align:"right", search:true},
            {name:'telefone',index:'telefone', width:80, align:"right", searchoptions:{sopt:['eq','ne','lt','le','gt','ge']}},
            {name:'ligacao_telefone',index:'ligacao_telefone', width:130,align:"right",search:true},
            {name:'departamento',index:'departamento', width:150, search:true}    ,
            {name:'edificio',index:'edificio', width:150, search:true}    ,
            {name:'sala',index:'sala', width:150, search:true}
        ],
        rowNum:10,
        mtype: "POST",
        rowList:[10,20,30],
        pager: '#s4pager',
        sortname: 'id_user',
        viewrecords: true,
        rownumbers: true,
        gridview : true,
        sortorder: "desc",
        caption:"Advanced Search Example"
    });
    jQuery("#s4list").jqGrid('navGrid','#s3pager',
    {
        edit:false,add:false,del:false,search:true,refresh:true
    },
    {}, // edit options
    {}, // add options
    {}, //del options
    {multipleSearch:true} // search options
    );
});
</script>

</head>

<body>

    <table id="s4list"></table>
    <div id="s4pager"></div>

</body>

26/04/2010
14:31
Avatar
tony
Sofia, Bulgaria
Moderator
Members

Moderators
Forum Posts: 7721
Member Since:
30/10/2007
sp_UserOfflineSmall Offline

Hello,

Sorry it is not a jqGrid problem

Regards

Tony

For professional UI suites for Java Script and PHP visit us at our commercial products site - guriddo.net - by the very same guys that created jqGrid.

Forum Timezone: Europe/Sofia

Most Users Ever Online: 715

Currently Online:
32 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

OlegK: 1255

markw65: 179

kobruleht: 144

phicarre: 132

YamilBracho: 124

Renso: 118

Member Stats:

Guest Posters: 447

Members: 11373

Moderators: 2

Admins: 1

Forum Stats:

Groups: 1

Forums: 8

Topics: 10592

Posts: 31289

Newest Members:

, razia, Prankie, psky, praveen neelam, greg.valainis@pa-tech.com

Moderators: tony: 7721, Rumen[Trirand]: 81

Administrators: admin: 66

Comments are closed.
Privacy Policy   Terms and Conditions   Contact Information