jQuery PHP API - 设置分页的问题

时间:2017-02-03 08:57:22

标签: php jquery mysql

我有这个小API,它提供了750种啤酒的数据库记录。我希望用20种啤酒来限制它,当我按下它时,它会给我接下来的20条啤酒记录,按ID排序。

我设置MySQL查询以限制第一次调用0,20(当请求页面时)。然后我想到当我按下“下一步”按钮时,我会拉出页面上的最后一个ID并通过AJAX请求将其传递给API。

然后我在PHP中写了一些额外的行来检查最新的id是不是空的,那么它应该是id = id + 20。更改MySQL请求以询问LIMIT 20,20,但某处出现问题,我似乎无法找到我所犯的错误...

我的PHP:

if(isset($_GET["biertjes"])) {

$latestId = $_POST['lid'];
if(!$latestId == 0){
    $latestId = $latestId + 20;
}
else {
    $latestId = 0;
}

$sql = "SELECT * 
    FROM bier
    ORDER BY id
    LIMIT 20,20"; 
$run_query = mysqli_query($connect, $sql);  

if(mysqli_num_rows($run_query) > 0){

$ids = array();
$namen = array();
$brouwerijen = array();
$types = array();
$gistingen = array();
$percs = array();
$inkoop_prijzen = array();
$biertjes = array();

while($row = mysqli_fetch_assoc($run_query)) {
    $id = $row['id'];
    $naam = $row['naam'];
    $brouwerij = $row['bouwerij'];
    $type = $row['type'];
    $gisting = $row['gisting'];
    $perc = $row['perc'];
    $inkoop_prijs = $row['inkoop_prijs'];

    $ids[] = $id;
    $namen[] = $naam;
    $brouwerijen[] = $brouwerij;
    $types[] = $type;
    $gistingen[] = $gisting;
    $percs[] = $perc;
    $inkoop_prijzen[] = $inkoop_prijs;

}   

for($i=0; $i < count($ids); $i++){

    $biertjes[] = array("id"=>$ids[$i], "naam"=>$namen[$i], "brouwerij"=>$brouwerijen[$i], "type"=>$types[$i], "gisting"=>$gistingen[$i], "perc"=>$percs[$i], "inkoop_prijs"=>$inkoop_prijzen[$i]);

}

echo json_encode($biertjes);
}
}   

我的Jquery:

    $("body").delegate( "#volgende", "click", function() {

    var row = $("#biertabel");
    var tr = row.find("tr:last");
    var bier = tr.find("td:nth-child(4)"); 

    for (var i = 0; i < biertjes.length; i++) {
        console.log("Dit is biertje:" +biertjes[i].naam);
        console.log("We zoeken naar :" +bier.html());
        if( bier.html() == biertjes[i].naam ) {
            console.log("Het juiste biertje is gevonden! Gegevens inladen...");
            var id = biertjes[i].id;
            console.log("Klaar met laden van gegevens.");
            break;
        }
    }

    $("#biertabel").remove();
    alert(id);

    $.ajax({
    url :   "action-api.php",
    async: false,
    method: "GET",
    data: {biertjes:1,lid:id},
    dataType: 'json',
    success :   function(data){ 
        console.log(data);
        biertjes = data;

        var html = '<table style="width:100%" id="biertabel"> <tr> <th>Edit</th> <th>Info</th> <th>Delete</th> <th>Naam</th> <th>Percentage</th> </tr>';

        $.each(data, function(i, item) {
            html+='<tr><td><div class="edit"><img src="img/edit.png"/></div></td>';
            html+='<td><div class="info"><img src="img/info.png"/></div></td>';
            html+='<td><div class="delete"><img src="img/delete.png"/></div></td>';
            html+='<td>'+item.naam+'</td>';
            html+='<td>'+item.perc+'</td></tr>';
        });

        html+='</table>'
        container.append(html).css(tableCss());
        return biertjes;
    },  

});     
return biertjes;


});

3 个答案:

答案 0 :(得分:1)

假设您确实要翻阅所有750个“啤酒”,则需要使用parameter for the offset part of the query。我认为这是你$latestId的用途,但我没有看到你实际使用它。

这样的事情:

$beers = array();

$sql = "SELECT * 
    FROM bier
    ORDER BY id
    LIMIT ?,20"; 

if ($stmt = mysqli_prepare($connect, $sql)) {

    mysqli_stmt_bind_param($stmt, "i", $latestId);

    mysqli_stmt_execute($stmt);

    mysqli_stmt_bind_result($stmt, $beers);

    mysqli_stmt_close($stmt);
}

答案 1 :(得分:1)

你的AJAX调用是GET,你正在寻找一个POST变量:

$latestId = $_POST['lid'];
if(!$latestId == 0){
    $latestId = $latestId + 20;
}
else {
    $latestId = 0;
}

试试这个

$latestId = $_GET['lid'];
if(!$latestId == 0){
    $latestId = $latestId + 20;
}
else {
    $latestId = 0;
}

答案 2 :(得分:-1)

JQuery分页 - 您可以使用jquery分页

http://tablesorter.com/docs/example-pager.html

更改为

if(!$latestId == 0) 

来自

if(!$latestId)