我的网站上有一个搜索框,可以在其中按姓名、电子邮件、城市、州或国家/地区搜索注册用户。我尝试使用 Jquery 的 Combogrid 插件在该搜索框上实现自动建议功能:http: //combogrid.justmybit.com/。我已经非常努力地让它正常工作,但无论如何它似乎永远不会工作。
我有一个 php 文件“server.php”,它以 JSON 格式发送数据,该插件使用它来呈现包含表格形式数据的自动建议网格。我从 Server.php 文件中复制了 JSON 输出并将其放入文本文件中。当我将资源文件的 URL 设置为此文本文件时,该插件工作正常,但当我将其设置为该 PHP 文件时无法工作。我知道我遗漏了一些东西,需要你的帮助来修补它。
这是我的 Combogrid 的配置
<script>
jQuery(document).ready(function(){
$('#switcher').themeswitcher({
loadTheme:"Smoothness"
});
$( "#project" ).combogrid({
url: 'http://localhost/combogrid-1.6.2/server.php',
sord: "asc",
debug:true,
sidx: "id",
rememberDrag: true,
rows: 10,
alternate: true,
replaceNull: true,
colModel: [{'columnName':'name','width':'25','label':'Name'}, {'columnName':'country','width':'25','label':'Country'},{'columnName':'state','width':'25','label':'State'}, {'columnName':'city','width':'25','label':'City'}],
select: function( event, ui ) {
$( "#project" ).val( ui.item.name );
document.location.href=ui.item.link;
return false;
}
});
});
服务器.PHP
<?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'];
$searchTerm = $_GET['searchTerm'];
// get thedirection
if(!$sidx) $sidx =1;
if ($searchTerm=="") {
$searchTerm="%";
}
else
{
$searchTerm = "%" . $searchTerm . "%";
}
$dbhost = "localhost";
$dbuser = "root";
$dbpassword = "";
$database = "userinfo";
// 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.");
$result = mysql_query("SELECT COUNT(*) AS count FROM userdata WHERE first like '$searchTerm'");
$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($total_pages!=0) $SQL = "SELECT * FROM userdata WHERE first like '$searchTerm' ORDER BY $sidx $sord LIMIT $start , $limit";
else $SQL = "SELECT * FROM userdata WHERE first like '$searchTerm' ORDER BY $sidx $sord";
$result = mysql_query( $SQL ) or die("Couldn t execute query.".mysql_error());
$response->page = $page;
$response->total = $total_pages;
$response->records = $count;
$i=0;
while($row = mysql_fetch_array($result,MYSQL_ASSOC))
{$response->rows[$i]['name']=$row['first'];
$response->rows[$i] ['cell']=array($row['first'],$row['country'],$row['state'],$row['city']);
$response->rows[$i]['name']=$row['first'];
$response->rows[$i]['country']=$row['country'];
$response->rows[$i]['state']=$row['state'];
$response->rows[$i]['city']=$row['city'];
$response->rows[$i]=array("name"=>$row["first"],"country"=>$row['country'],"state"=>$row['state'],"city"=>$row['city']); $i++;
}
echo json_encode($response);
$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
//$stringData = "{$_GET['page']} {$_GET['rows']} {$_GET['sidx'] } {$_GET['sord']} {$_GET['searchTerm']}";
$stringData =$SQL." ".json_encode($response);
fwrite($fh, $stringData);
?>