我正在使用ajax从我的Mysql数据库中获取数据。数据大约是10k行,所以我需要在页面中进行分页。我在网上得到一个分页代码,它没有ajax工作正常,但我无法使用我的ajax代码使其正常运行。
这是我的分页文件" function.php"我对此没有任何问题
<?php
function displayPaginationBelow($per_page,$page){
$page_url="?";
$query = "SELECT COUNT(*) as totalCount from pdbp inner join bp on(pdbp.bp_id=bp.id) where bp.status= 1";
$rec = mysql_fetch_array(mysql_query($query));
$total = $rec['totalCount'];
$adjacents = "2";
$page = ($page == 0 ? 1 : $page);
$start = ($page - 1) * $per_page;
$prev = $page - 1;
$next = $page + 1;
$setLastpage = ceil($total/$per_page);
$lpm1 = $setLastpage - 1;
$setPaginate = "";
if($setLastpage > 1)
{
$setPaginate .= "<ul class='setPaginate'>";
$setPaginate .= "<li class='setPage'>Page $page of $setLastpage</li>";
if ($setLastpage < 7 + ($adjacents * 2))
{
for ($counter = 1; $counter <= $setLastpage; $counter++)
{
if ($counter == $page)
$setPaginate.= "<li><a class='current_page'>$counter</a></li>";
else
$setPaginate.= "<li><a href='{$page_url}page=$counter'>$counter</a></li>";
}
}
elseif($setLastpage > 5 + ($adjacents * 2))
{
if($page < 1 + ($adjacents * 2))
{
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($counter == $page)
$setPaginate.= "<li><a class='current_page'>$counter</a></li>";
else
$setPaginate.= "<li><a href='{$page_url}page=$counter'>$counter</a></li>";
}
$setPaginate.= "<li class='dot'>...</li>";
$setPaginate.= "<li><a href='{$page_url}page=$lpm1'>$lpm1</a></li>";
$setPaginate.= "<li><a href='{$page_url}page=$setLastpage'>$setLastpage</a></li>";
}
elseif($setLastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
{
$setPaginate.= "<li><a href='{$page_url}page=1'>1</a></li>";
$setPaginate.= "<li><a href='{$page_url}page=2'>2</a></li>";
$setPaginate.= "<li class='dot'>...</li>";
for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
{
if ($counter == $page)
$setPaginate.= "<li><a class='current_page'>$counter</a></li>";
else
$setPaginate.= "<li><a href='{$page_url}page=$counter'>$counter</a></li>";
}
$setPaginate.= "<li class='dot'>..</li>";
$setPaginate.= "<li><a href='{$page_url}page=$lpm1'>$lpm1</a></li>";
$setPaginate.= "<li><a href='{$page_url}page=$setLastpage'>$setLastpage</a></li>";
}
else
{
$setPaginate.= "<li><a href='{$page_url}page=1'>1</a></li>";
$setPaginate.= "<li><a href='{$page_url}page=2'>2</a></li>";
$setPaginate.= "<li class='dot'>..</li>";
for ($counter = $setLastpage - (2 + ($adjacents * 2)); $counter <= $setLastpage; $counter++)
{
if ($counter == $page)
$setPaginate.= "<li><a class='current_page'>$counter</a></li>";
else
$setPaginate.= "<li><a href='{$page_url}page=$counter'>$counter</a></li>";
}
}
}
if ($page < $counter - 1){
$setPaginate.= "<li><a href='{$page_url}page=$next'>Next</a></li>";
$setPaginate.= "<li><a href='{$page_url}page=$setLastpage'>Last</a></li>";
}else{
$setPaginate.= "<li><a class='current_page'>Next</a></li>";
$setPaginate.= "<li><a class='current_page'>Last</a></li>";
}
$setPaginate.= "</ul>\n";
}
return $setPaginate;
}
?>
这是我的ajax文件,我在其中通过数据库获取数据并将其发送到我的视图文件。
<?php
include_once('config/connection.php');
// here is the Pagination code starts
include_once "function.php";
if(isset($_GET["page"]))
$page = (int)$_GET["page"];
else
$page = 1;
$setLimit = 10;
$pageLimit = ($page * $setLimit) - $setLimit;
// Pagination code ends
if(isset($_GET['name'])){
$name = $_GET['name'];
$retval = "";
$pagination = "";
$counter = 0;
$query = mysql_query("select id,card_name,phone1,address,region,country,cert_num,category_cod,material_cod,trade_type,web from pdbp inner join bp on(pdbp.bp_id=bp.id) where bp.status= 1 and card_name like '%$name%' limit $pageLimit , $setLimit");
while($data = mysql_fetch_assoc($query)){
$id = $data['id'];
$name = $data['card_name'];
$phone1 = $data['phone1'];
$region = $data['region'];
$trade_type = $data['trade_type'];
$address = $data['address'];
$web = $data['web'];
$queryr= mysql_query("select Region from regions where RegionId = '$region'");
$region_1 = mysql_fetch_array($queryr);
$region_name = $region_1['Region'];
$country = $data['country'];
$queryc= mysql_query("select Country from countries where CountryId = '$country'");
$country_1 = mysql_fetch_array($queryc);
$country_name = $country_1['Country'];
$cert_num = $data['cert_num'];
$cn=explode(",",$cert_num);
$cns = "";
foreach($cn as $cc){
$cert_fetch = mysql_query("select description from cert where id='$cc'");
$cert_fetch1 = mysql_fetch_array($cert_fetch);
$cnum = $cert_fetch1['description'];
$cns=$cns."<div style='margin:5px;display:block;clear:both'> ".$cnum." </div>";
}
$cat = $data['category_cod'];
$category = explode(",",$cat);
$cc = "";
foreach($category as $categories){
$categories_fetch = mysql_query("select subc_name, category_name, line from sub_categories as s inner join product_categories p ON (p.category_cod = s.pc_Cod) inner join product_line l ON (p.line_cod = l.line_cod) where subc_cod='$categories'");
$aaa = mysql_fetch_array($categories_fetch);
$s = $aaa['subc_name'];
$cname = $aaa['category_name'];
$l = $aaa['line'];
$cc=$cc."<div style='margin:5px;display:block;clear:both'> ".$s.'-'.$cname.'-'.$l." </div>";
}
$mat = $data['material_cod'];
$material = explode(",",$mat);
$mm = "";
foreach($material as $materials){
$material_fetch = mysql_query("select material from product_material where material_cod='$materials'");
$mat_fetch1 = mysql_fetch_array($material_fetch);
$mcod = $mat_fetch1['material'];
$mm=$mm."<div style='margin:5px;display:block;clear:both'> ".$mcod." </div>";
}
$retval=$retval."<tr><td>".++$counter."</td><td> ".$name." </td><td> ".$phone1." </td><td> ".$address." </td><td> ".$region_name." </td><td> ".$country_name." </td><td> ".$cns." </td><td> ".$cc." </td><td> ".$mm."</td><td> ".$trade_type."</td><td> ".$web."</td><td></tr>";
}
echo $retval;
}
?>
我正在成功获取数据,但现在我需要回显displayPaginationBelow($ setLimit,$ page);在数据下面显示分页,这是通过function.php来的,我不知道如何做到这一点。
这是我的jquery代码:
$("#cmp_name").keyup(function(){
$.ajax({
'url':"filter.php",
'data':{name:$("#cmp_name").val()},
'method':'GET',
'success':function(name){
$("#data").html(name);
}
})
});
任何人都知道如何实现这一目标。请帮助我,或者如果有人提供更有效的方法来实现我的目标,我将非常感激。其次我知道我使用的是不推荐使用的mysql_ *,我很快就会修复它
答案 0 :(得分:0)
使用ajax
时使用type替代方法$("#cmp_name").keyup(function(){
name = $("#cmp_name").val();
$.ajax({
'url':"filter.php",
'data':{"name":name},
'type':'POST',
'success':function(data){
$("#data").html(data);
}
})
});
答案 1 :(得分:0)
使用数组作为多个返回值。
在filter.php中
$retval['data'] = YOUR_PREVIOUS_RETVAL;
$retval['pagination'] = displayPaginationBelow($setLimit,$page);
echo json_encode($retval);
在javascript中
$("#cmp_name").keyup(function(){
$.ajax({
'url':"filter.php",
'data':{name:$("#cmp_name").val()},
'type':'GET',
'dataType':'json',
'success':function(result){
$("#data").html(result.data);
$("#PAGINATION").html(result.pagination);
}
})
});
您必须将dataType指定为'json'。