如何使用PHP从Cassandra数据库获取每页数据。

huangapple go评论73阅读模式
英文:

how to get per page data from cassandra db using php

问题

我想为我的数据表添加下一页和上一页的链接。
如何编写查询以获取下一页和上一页的数据?

<div>
   <a style="margin-left:5px;" href="#" class="previous">« 上一页</a>
   <a style="margin-left:5px;" href="#" class="next">下一页 »</a>
</div>
$recordsPerPage = 20;

$totalPages = ceil($totalCount / $recordsPerPage);

$page = isset($_GET['page']) ? $_GET['page'] : 1;

$offset = ($page - 1) * $recordsPerPage;

$query = "SELECT * FROM history" . $whereClause . " LIMIT " .$recordsPerPage. " OFFSET " .$offset.";";

我希望在点击下一页时只获取前20条数据,然后显示下一批20条数据。

英文:

i want to add next and previous links for my data table.
how to write query to get data in next and prevous links data

<div>
   <a style="margin-left:5px;" href="#" class="previous">« Previous</a>
   <a style="margin-left:5px;" href="#" class="next">Next »</a>
</div>
$recordsPerPage = 20;

$totalPages = ceil($totalCount / $recordsPerPage);

$page = isset($_GET['page']) ? $_GET['page'] : 1;

$offset = ($page - 1) * $recordsPerPage;

$query = "SELECT * FROM history" . $whereClause . " LIMIT " .$recordsPerPage. "OFFSET " .$offset.";";

i am expecting to get data only 20 entries first when i click on next i want to show next 20 entries

答案1

得分: 3

你需要调用nextPage:

$cluster   = Cassandra::cluster()
               ->withContactPoints('127.0.0.1')
               ->build();
$session   = $cluster->connect("simplex");
$options   = array('page_size' => 5);
$rows      = $session->execute("SELECT * FROM paging_entries", $options);

while (true) {
    echo "entries in page: " . $rows->count() . "\n";

    foreach ($rows as $row) {
        echo "key: " . $row['key'] . ", value: " . $row['value'] . "\n";
    }

    if ($rows->isLastPage()) {
        break;
    }

    $rows = $rows->nextPage();
}

这里有更多示例:

https://docs.datastax.com/en/developer/php-driver/1.3/features/result_paging/

英文:

You'll need to call nextPage:

<?php
$cluster   = Cassandra::cluster()
               ->withContactPoints('127.0.0.1')
               ->build();
$session   = $cluster->connect("simplex");
$options   = array('page_size' => 5);
$rows      = $session->execute("SELECT * FROM paging_entries", $options);

while (true) {
    echo "entries in page: " . $rows->count() . "\n";

    foreach ($rows as $row) {
        echo "key: " . $row['key'] . ", value: " . $row['value'] . "\n";
    }

    if ($rows->isLastPage()) {
        break;
    }

    $rows = $rows->nextPage();
}

There are more examples here:

https://docs.datastax.com/en/developer/php-driver/1.3/features/result_paging/

huangapple
  • 本文由 发表于 2023年6月8日 14:04:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76429011.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定