Forum


00:15

08/12/2009

Hello
I've downloaded and installed jqgrid 3.6.1 and succeded in display datas form mysql database. I'm able to sort datas by clicking columns, show 10 20 or 30 records in grid, go to the next records... and open search box. Problem is when I'm click searching datas are not filtered.
Thank you for your help
html page
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>My First Grid</title>
<link rel="stylesheet" type="text/css" media="screen" href="css/ui-lightness/jquery-ui-1.7.2.custom.css">
<link rel="stylesheet" type="text/css" media="screen" href="css/ui.jqgrid.css">
<link rel="stylesheet" type="text/css" media="screen" href="css/jquery.searchFilter.css">
<style>
html, body {
margin: 0;
padding: 0;
font-size: 75%;
}
</style>
<script src="js/jquery-1.3.2.min.js" type="text/javascript"></script>
<script src="js/i18n/grid.locale-fr.js" type="text/javascript"></script>
<script type="text/javascript">
jQuery.jgrid.useJSON = true;
</script>
<script src="js/jquery.jqGrid.min.js" type="text/javascript"></script>
<script type="text/javascript">
jQuery.extend(jQuery.jgrid,{
parse:function(jsstring) {
return JSON.parse(jsstring);
}
});
</script>
<script type="text/javascript">
jQuery().ready(function (){
jQuery("#list").jqGrid({
url:'grid.php?q=1',
datatype: 'xml',
colNames:['numero','organisme', 'maj'],
colModel :[
{name:'NUM', index:'numero', width:55},
{name:'ORG', index:'organisme', width:90},
{name:'MAJ', index:'maj', width:80, align:'right'}
],
pager: '#pager',
rowNum:10,
rowList:[10,20,30],
sortname: 'numero',
sortorder: 'desc',
viewrecords: true,
caption: 'My first grid',
sortname: 'numero',
width:800,
height:400,
}); jQuery("#list").jqGrid('navGrid','#pager',{edit:true,add:false,del:false});
jQuery("#s2").click( function() { jQuery("#list").jqGrid('setGridParam',{sortname:"numero",sortorder:"asc"}).trigger("reloadGrid");
});
jQuery("#s1").click (function() { jQuery("#list").searchGrid( {sopt:['cn','bw','eq','ne','lt','gt','ew']} );
});
});
</script>
</head>
<body>
<table id="list"></table>
<div id="pager"></div>
<a href="javascript:void(0)" id="s2">Set Sort to amount column</a><a href="javascript:void(0)" id="s1">Seaerch</a>
</body>
</html>
php file
$page = $_GET['page'];
// get how many rows we want to have into the grid - rowNum parameter in the grid
$limit = $_GET['rows'];
// get index row - i.e. user click to sort. At first time sortname parameter -
// after that the index from colModel
$sidx = $_GET['sidx'];
// sorting order - at first time sortorder
$sord = $_GET['sord'];
// if we not pass at first time index use the first column for the index or what you want
if(!$sidx) $sidx =1;
mysql_connect("localhost", "xxxxxx", "xxxxxx") or die("Connection Error: " . mysql_error());
mysql_select_db("BDDRIPEINTRANET") or die("Connection Error: " . mysql_error());
// calculate the number of rows for the query. We need this for paging the result
$result = mysql_query("SELECT COUNT(*) AS count FROM organismes");
$row = mysql_fetch_array($result,MYSQL_ASSOC);
$count = $row['count'];
// calculate the total pages for the query
if( $count > 0 && $limit > 0) {
$total_pages = ceil($count/$limit);
} else {
$total_pages = 0;
}
// if for some reasons the requested page is greater than the total
// set the requested page to total page
if ($page > $total_pages) $page=$total_pages;
// calculate the starting position of the rows
$start = $limit*$page - $limit;
// if for some reasons start position is negative set it to 0
// typical case is that the user type 0 for the requested page
if($start <0) $start = 0;
// the actual query for the grid data
$SQL = "SELECT numero, organisme, maj FROM organismes ORDER BY $sidx $sord LIMIT $start , $limit";
$result = mysql_query( $SQL ) or die("Couldn't execute A.".mysql_error());
if ( stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml") ) {
header("Content-type: application/xhtml+xml;charset=utf-8"); } else {
header("Content-type: text/xml;charset=utf-8");
} $et = ">";
echo "<?xml version='1.0' encoding='utf-8'?$et\\n";
$s .= "<rows>";
$s .= "<page>".$page."</page>";
$s .= "<total>".$total_pages."</total>";
$s .= "<records>".$count."</records>";
// be sure to put text data in CDATA
while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
$s .= "<row id='". $row[numero]."'>";
$s .= "<cell>". $row[numero]."</cell>";
$s .= "<cell>". $row[organisme]."</cell>";
$s .= "<cell><![CDATA[".$row[organisme]."]]></cell>";
$s .= "<cell>". $row[maj]."</cell>";
$s .= "<cell><![CDATA[".$row[maj]."]]></cell>";
$s .= "</row>";
}
$s .= "</rows>";
echo $s;
?>
18:22

Moderators
30/10/2007

Hello,
In your script there is nothing when you perform sort. Could you please see how this is done into the demo in order to have idea what to do.
Best 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.
01:30

Hello
Thank you for you fast reply and sorry for my bad english. I ve developped by myself an application for my job (local authoritiy/international cooperation). Application was at the beginning created on local network with ms access, access data pages and html intranet pages with some js scripts. Now i'm trying to create "real" website with mysql, php... that's new for me. As i told you in my first post, i'm succeded in created page to display datas with jqgrid. I'm able to sort datas by columns and apply my working language plugin (french), the refresh option is working too i think but when search box is loaded and after appllying filter nothing happen. Please see the test page at http://www.nord-international....../grid.html. Please ask me if you need further informations. The code i've posted is the one i used and it's working to sort datas, so i don't understand your question...
Thanks again for your help best regards
01:09

Moderators
30/10/2007

Hello,
Sorry, the link does not work for me.
Best 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.
18:53

sorry http://www.nord-international......qgrid.html
thanks again
18:54

the first one show you where i install jqgrid and : http://www.nord-international....../grid.html is my custom example
17:40

Moderators
30/10/2007

Hello,
For me everthing is working correct. You should check your server side code on how you accept the parameters from
the search dialog and how you perform the serch. Again in the demo package there is such one. Just look at server.php file from
the same package.
Best 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.
13:34

Hi
I ve checked server.php and rewritted it completly and kept only the first example case.
still have the datas but can't perfom search.
When i click search button i've got an error jquery.js object required at line 12
below my server.php content
<?php
include("dbconfig.php");
$page = $_GET['page'];
$limit = $_GET['rows'];
$sidx = $_GET['sidx'];
$sord = $_GET['sord'];
if(!$sidx) $sidx =1;
$searchOn = Strip($_REQUEST['_search']);
if($searchOn=='true') {
$fld = Strip($_REQUEST['searchField']);
if( $fld=='id' || $fld =='numero' || $fld=='organisme') {
$fldata = Strip($_REQUEST['searchString']);
$foper = Strip($_REQUEST['searchOper']);
// costruct where
$wh .= " AND ".$fld;
switch ($foper) {
case "bw":
$fldata .= "%";
$wh .= " LIKE '".$fldata."'";
break;
case "eq":
if(is_numeric($fldata)) {
$wh .= " = ".$fldata;
} else {
$wh .= " = '".$fldata."'";
}
break;
case "ne":
if(is_numeric($fldata)) {
$wh .= " <> ".$fldata;
} else {
$wh .= " <> '".$fldata."'";
}
break;
case "lt":
if(is_numeric($fldata)) {
$wh .= " < ".$fldata;
} else {
$wh .= " < '".$fldata."'";
}
break;
case "le":
if(is_numeric($fldata)) {
$wh .= " <= ".$fldata;
} else {
$wh .= " <= '".$fldata."'";
}
break;
case "gt":
if(is_numeric($fldata)) {
$wh .= " > ".$fldata;
} else {
$wh .= " > '".$fldata."'";
}
break;
case "ge":
if(is_numeric($fldata)) {
$wh .= " >= ".$fldata;
} else {
$wh .= " >= '".$fldata."'";
}
break;
case "ew":
$wh .= " LIKE '%".$fldata."'";
break;
case "ew":
$wh .= " LIKE '%".$fldata."%'";
break;
default :
$wh = "";
}
}
}
$db = mysql_connect($dbhost, $dbuser, $dbpassword)or die("Connection Error: " . mysql_error());
mysql_select_db($database) or die("Error conecting to db.");
$result = mysql_query("SELECT COUNT(*) AS count FROM organismes ".$wh);
$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 numero, organisme, maj FROM organismes".$wh." ORDER BY ".$sidx." ". $sord." LIMIT ".$start." , ".$limit;
$result = mysql_query( $SQL ) or die("Couldn't execute query.".mysql_error());
if ( stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml") ) {
header("Content-type: application/xhtml+xml;charset=utf-8"); } else {
header("Content-type: text/xml;charset=utf-8");
} $et = ">";
echo "<?xml version='1.0' encoding='utf-8'?$et\\n";
$s .= "<rows>";
$s .= "<page>".$page."</page>";
$s .= "<total>".$total_pages."</total>";
$s .= "<records>".$count."</records>";
// be sure to put text data in CDATA
while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
$s .= "<row id='". $row[numero]."'>";
$s .= "<cell>". $row[numero]."</cell>";
$s .= "<cell><![CDATA[". $row[organisme]."]]></cell>";
$s .= "<cell>". $row[maj]."</cell>";
$s .= "</row>";
}
$s .= "</rows>";
echo $s;
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;
}
?>
20:24

Hello, well I think the reason why your filter isn't working is because of your MySQL syntax. After your FROM table_name statement add WHERE 1 = 1
this way when you try to filter it will work correctly because the php code expects a where clause to already exist and in MySQL you can not have two where clauses as it will return a syntax error.
Most Users Ever Online: 715
Currently Online:
66 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.comModerators: tony: 7721, Rumen[Trirand]: 81
Administrators: admin: 66