Hi boys, I'm trying to code some app. with jquery and jqgrid. Most of the app. is finished except search function. I can't get the datas from server when I want to search in my grids. Can anyone help me? Many thanks.... King Regards Ibrahim. Ps: I read the documentation. But i think i've problems with my JSON data. Who can explain me? My js file: Code:
jQuery(document).ready(function(){ function pickdates(id){ jQuery("#"+id+"_tarih","#rowed3").datepicker({dateFormat:"yy-mm- dd"});} var lastsel; jQuery("#rowed3").jqGrid({ url:'get.php', datatype: "json", colNames:['Id','Havuz','Tarih', 'Giren', 'Çıkan','Ortalama','Açıklama'], colModel:[ {name:'id',index:'id', width:40, align:"center"}, {name:'havuz',index:'havuz', width:90, align:"center", editable:true}, {name:'tarih',index:'tarih', width:90, align:"center", editable:true, sorttype:"date"}, {name:'giren',index:'giren', width:90, align:"center", editable:true}, {name:'cikan',index:'cikan', width:90, align:"center", editable:true}, {name:'ortalama',index:'ortalama', width:90, align:"center"}, {name:'aciklama',index:'aciklama', width:250, align:"center", editable:true}, ], rowNum:50, rowList:[50,100,150], imgpath: 'themes/sand/images', pager: jQuery('#prowed3'), sortname: 'id', viewrecords: true, sortorder: "desc", height: 363, width:800, caption: "Balık Hareket Tablosu Düzenleme" }).navGrid('#prowed3', {}, //options {editCaption: "Kayıt Düzenle", bSubmit: "Kaydet", bCancel: "Vazgeç", processData: "Kaydediyor...", url:"duzenle.php", onInitializeForm: function(formid) { $("#tarih",formid).datepicker({dateFormat:"dd-mm-yy"}); }}, // edit options {addCaption: "Kayıt Ekle", bSubmit: "Kaydet", bCancel: "Vazgeç", processData: "Kaydediyor...", url:"ekle.php", onInitializeForm: function(formid) { $("#tarih",formid).datepicker({dateFormat:"dd-mm-yy"}); }}, // add options {delCaption: "Kayıt Sil", bSubmit: "Evet", bCancel: "Hayır", processData: "Siliniyor...", url:"sil.php", msg:"Seçili Kaydın Silinmesini Onaylıyor musunuz?"}, // del options {sopt:['cn','bw','eq','ne','lt','gt','ew'], url:"arama.php?q=1"} // search options ); }); My Json data file: Code: <?php include("ayar.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; // search options // IMPORTANT NOTE!!!!!!!!!!!!!!!!!!!!!!!!!!!! // this type of constructing is not recommendet // it is only for demonstration //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! $wh = ""; $searchOn = Strip($_REQUEST['_search']); if($searchOn=='true') { $fld = Strip($_REQUEST['searchField']); if( $fld=='id' || $fld =='tarih' || $fld=='havuz' || $fld=='giren' || $fld=='cikan' || $fld=='ortalama' || $fld=='aciklama' ) { $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 = ""; } } } //echo $fld." : ".$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, SUM(giren) AS giren, SUM(cikan) AS cikan, SUM(ortalama) AS ortalama FROM balik_hareket ".$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 * FROM balik_hareket".$wh." ORDER BY ".$sidx." ". $sord." LIMIT ".$start." , ".$limit; $result = mysql_query( $SQL ) or die("Couldnt 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[havuz],$row[tarih], $row[giren],$row[cikan],$row[ortalama],$row[aciklama]); $i++; } echo json_encode($responce); break; case 2: $result = mysql_query("SELECT COUNT(*) AS count FROM balik_hareket".$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 * FROM balik_hareket ".$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]; $responce->rows[$i]['cell']=array($row[id],$row[tarih], $row[havuz],$row[giren],$row[cikan],$row[ortalama],$row[aciklama]); $i++; } //echo $json->encode($responce); // coment if php 5 echo json_encode($responce); break; case 3: $result = mysql_query("SELECT COUNT(*) AS count FROM balik_hareket".$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 * FROM balik_hareket".$wh." ORDER BY ".$sidx." ".$sord. " LIMIT ".$start." , ".$limit; $result = mysql_query( $SQL ) or die("Couldnt 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[tarih], $row[havuz],$row[giren],$row[cikan],$row[ortalama],$row[aciklama]); $i++; } echo $json->encode($responce); // coment if php 5 //echo json_encode($responce); break; case 4: $result = mysql_query("SELECT COUNT(*) AS count FROM balik_hareket".$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 * FROM balik_hareket".$wh." ORDER BY ".$sidx." ".$sord. " LIMIT ".$start." , ".$limit; $result = mysql_query( $SQL ) or die("Couldnt 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]=$row; $i++; } //echo $json->encode($responce); // coment if php 5 echo json_encode($responce); break; case 5: $result = mysql_query("SELECT COUNT(*) AS count FROM balik_hareket ".$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 * FROM balik_hareket ".$wh." ORDER BY ".$sidx." ".$sord. " LIMIT ".$start." , ".$limit; $result = mysql_query( $SQL ) or die("Couldt 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]=$responce->rows[$i] ['cell']=array($row[id],$row[tarih],$row[havuz],$row[giren], $row[cikan],$row[ortalama],$row[aciklama]); $i++; } //echo $json->encode($responce); // coment if php 5 echo json_encode($responce); break; } 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; } ?> So here you can see my get.php file (It works!): Code: <?php include "ayar.php"; $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 if(!$sidx) $sidx =1; $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 balik_hareket"); $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) $SQL = "SELECT * FROM balik_hareket ORDER BY $sidx $sord LIMIT $start , $limit"; $result = mysql_query( $SQL ) or die("Couldnt 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[havuz],$row[tarih], $row[giren],$row[cikan],$row[ortalama],$row[aciklama]); $i++; } echo json_encode($responce); ?>