Skip to content

Instantly share code, notes, and snippets.

@thobbs
Created December 14, 2012 17:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thobbs/4287021 to your computer and use it in GitHub Desktop.
Save thobbs/4287021 to your computer and use it in GitHub Desktop.
Pagination of a row with phpcassa
<?php
$cf->return_format = ColumnFamily::ARRAY_FORMAT;
function get_page($cf, $key, $page, $page_size) {
$start_col = "";
$current_page = 0;
$page_data = null;
while ($current_page < $page) {
// fetch one extra column at the end
$slice = new ColumnSlice($start_col, "", $page_size + 1);
$page_data = $cf->get($key, $slice);
if ($count($page_data < $page_size + 1)) {
// we're at the end of the row
break;
}
// use that extra column as the next start
$start_col = $page_data[count($page_data) - 1][0];
$page++;
}
// make sure not to return the extra column
return array_slice($page_data, 0, $page_size);
}
@hodgsonsam
Copy link

Thanks for this Tyler, didn't know about the return format functionality either :)

Iv modified it a bit for forum type of use, im using timestamps as column keys so want to pull from the end of the row showing the newest posts first:

return_format = ColumnFamily::ARRAY_FORMAT; function get_page($cf, $key, $page, $page_size) { $start_col = ""; $current_page = 0; $page_data = null; ``` // check page number is not higher than last page $total_postcount = $cf->get_count($key); $total_pagecount = ceil($total_postcount / $page_size); if($page > $total_pagecount) { echo "404 Page not found\n"; //header('Location: /404.html'); die; } while ($current_page < $page) { // fetch one extra column at the end $slice = new ColumnSlice($start_col , null , $page_size + 1 , true); $page_data = $cf->get($key, $slice); if(count($page_data) < ($page_size + 1)) { // we're at the end of the row break; } // use that extra column as the next start $start_col = $page_data[count($page_data) - 1][0]; $current_page++; } // make sure not to return the extra column return array_slice($page_data, 0, $page_size); ``` }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment