|
|
|
|
@ -3,33 +3,75 @@ |
|
|
|
|
declare(strict_types=1); |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* FROM site: |
|
|
|
|
* originally FROM site: |
|
|
|
|
* @link https://code.tutsplus.com/tutorials/how-to-paginate-data-with-php--net-2928 |
|
|
|
|
* @author Jason |
|
|
|
|
* @copyright 12/30/2020 |
|
|
|
|
* |
|
|
|
|
* Emailed on July 30th, 2023 for permission to use/license type |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
namespace tts\database; |
|
|
|
|
|
|
|
|
|
class paginate { |
|
|
|
|
|
|
|
|
|
private $_conn; |
|
|
|
|
private $_limit; |
|
|
|
|
private $_page; |
|
|
|
|
private int $_limit; |
|
|
|
|
private int $_page; |
|
|
|
|
private $_query; |
|
|
|
|
private $_total; |
|
|
|
|
|
|
|
|
|
public function __construct($conn, string $query) { |
|
|
|
|
private $_url; |
|
|
|
|
private $_url_limit; |
|
|
|
|
private $_url_page; |
|
|
|
|
public $max_limit = 500; |
|
|
|
|
|
|
|
|
|
/* |
|
|
|
|
* Query needs to be string for MySQL and array for MonogDB |
|
|
|
|
*/ |
|
|
|
|
public function __construct($conn, string|array $query, string $db_type = "mysql") { |
|
|
|
|
$this->_conn = $conn; |
|
|
|
|
$this->_query = $query; |
|
|
|
|
|
|
|
|
|
$rs = $this->_conn->query($this->_query); |
|
|
|
|
$this->_total = $rs->rowCount(); |
|
|
|
|
if ($db_type === "mysql") { |
|
|
|
|
$rs = $this->_conn->query($this->_query); |
|
|
|
|
$this->_total = $rs->rowCount(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private function set_limit(int $limit = 10): int { |
|
|
|
|
if ($limit < 1) { |
|
|
|
|
return 10; |
|
|
|
|
} |
|
|
|
|
return ($limit > $this->max_limit) ? $this->max_limit : $limit; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public function mongo_get_data(int $limit = 10, int $page = 1, array $options) { |
|
|
|
|
$this->_limit = $this->set_limit($limit); // Number of items per page |
|
|
|
|
$this->_page = $page; // The current page number |
|
|
|
|
|
|
|
|
|
$skip = (( $this->_page - 1 ) * $this->_limit ); |
|
|
|
|
|
|
|
|
|
if ($this->_limit === 0) { |
|
|
|
|
$db_options = $options; |
|
|
|
|
} else { |
|
|
|
|
$limits = ['limit' => $this->_limit, 'skip' => $skip]; |
|
|
|
|
$db_options = array_merge($limits, $options); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// NOTE _query is the WHERE condition as an array |
|
|
|
|
$collection = $this->_conn; |
|
|
|
|
$documents = $collection->find($this->_query, $db_options); |
|
|
|
|
|
|
|
|
|
// Calculate the total number of documents in the collection |
|
|
|
|
$this->_total = $collection->countDocuments($this->_query); |
|
|
|
|
|
|
|
|
|
$result = new \stdClass(); |
|
|
|
|
$result->page = $this->_page; |
|
|
|
|
$result->limit = $this->_limit; |
|
|
|
|
$result->total = $this->_total; |
|
|
|
|
$result->data = $documents; |
|
|
|
|
|
|
|
|
|
return $result; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public function get_data(int $limit = 10, int $page = 1) { |
|
|
|
|
$this->_limit = $limit; |
|
|
|
|
$this->_limit = $this->set_limit($limit); |
|
|
|
|
$this->_page = $page; |
|
|
|
|
|
|
|
|
|
if ($this->_limit === 0) { |
|
|
|
|
@ -52,14 +94,30 @@ class paginate { |
|
|
|
|
|
|
|
|
|
return $result; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public function use_hash_routes(string $url): void { |
|
|
|
|
$this->_url = $url; |
|
|
|
|
$this->_url_limit = "/"; |
|
|
|
|
$this->_url_page = "/"; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public function use_get_routes(string $url = ""): void { |
|
|
|
|
$this->_url = $url; |
|
|
|
|
$this->_url_limit = "?limit="; |
|
|
|
|
$this->_url_page = "&page="; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private function do_href(int $limit, int $page): string { |
|
|
|
|
return 'href="' . $this->_url . $this->_url_limit . $limit . $this->_url_page . $page . '"'; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public function create_links(int $links = 7, string $list_class = "ui pagination menu", string $item = "item") { |
|
|
|
|
if ($this->_limit === 0) { |
|
|
|
|
$last = ceil($this->_total / $this->_limit); |
|
|
|
|
|
|
|
|
|
if ($this->_limit === 0 || $last < 2) { |
|
|
|
|
return ''; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$last = ceil($this->_total / $this->_limit); |
|
|
|
|
|
|
|
|
|
$start = ( ( $this->_page - $links ) > 0 ) ? $this->_page - $links : 1; |
|
|
|
|
$end = ( ( $this->_page + $links ) < $last ) ? $this->_page + $links : $last; |
|
|
|
|
|
|
|
|
|
@ -68,25 +126,25 @@ class paginate { |
|
|
|
|
$class = ( $this->_page == 1 ) ? "disabled " : ""; |
|
|
|
|
$item = " " . $item; |
|
|
|
|
|
|
|
|
|
$html .= '<a class="' . $class . $item . '" href="?limit=' . $this->_limit . '&page=' . ( $this->_page - 1 ) . '">«</a>'; |
|
|
|
|
$html .= '<a class="' . $class . $item . '" ' . $this->do_href($this->_limit, $this->_page - 1) . '>«</a>'; |
|
|
|
|
|
|
|
|
|
if ($start > 1) { |
|
|
|
|
$html .= '<a class="' . $item . '" href="?limit=' . $this->_limit . '&page=1">1</a>'; |
|
|
|
|
$html .= '<a class="' . $item . '" ' . $this->do_href($this->_limit, 1) .'>1</a>'; |
|
|
|
|
$html .= '<div class="disabled' . $item . '"><span>...</span></div>'; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for ($i = $start; $i <= $end; $i++) { |
|
|
|
|
$class = ( $this->_page == $i ) ? "active" : ""; |
|
|
|
|
$html .= '<a class="' . $class . $item . '" href="?limit=' . $this->_limit . '&page=' . $i . '">' . $i . '</a>'; |
|
|
|
|
$html .= '<a class="' . $class . $item . '" ' . $this->do_href($this->_limit, $i) . '>' . $i . '</a>'; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if ($end < $last) { |
|
|
|
|
$html .= '<div class="disabled item"><span>...</span></div>'; |
|
|
|
|
$html .= '<a class="' . $class . $item . '" href="?limit=' . $this->_limit . '&page=' . $last . '">' . $last . '</a>'; |
|
|
|
|
$html .= '<div class="disabled' . $item . '"><span>...</span></div>'; |
|
|
|
|
$html .= '<a class="' . $class . $item . '" ' . $this->do_href($this->_limit, $last) . '>' . $last . '</a>'; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$class = ( $this->_page == $last ) ? "disabled" : ""; |
|
|
|
|
$html .= '<a class="' . $class . $item . '" href="?limit=' . $this->_limit . '&page=' . ( $this->_page + 1 ) . '">»</a>'; |
|
|
|
|
$html .= '<a class="' . $class . $item . '" ' . $this->do_href($this->_limit, $this->_page + 1) . '>»</a>'; |
|
|
|
|
|
|
|
|
|
$html .= '</div>'; |
|
|
|
|
|
|
|
|
|
@ -105,14 +163,14 @@ class paginate { |
|
|
|
|
|
|
|
|
|
// Prev. Page |
|
|
|
|
$class = ( $this->_page == 1 ) ? "disabled" : ""; |
|
|
|
|
$href = ( $this->_page == 1 ) ? "" : 'href="?limit=' . $this->_limit . '&page=' . ( $this->_page - 1 ) . '"' ; |
|
|
|
|
$href = ( $this->_page == 1 ) ? "" : $this->do_href($this->_limit, $this->_page - 1 ); |
|
|
|
|
$html .= '<a class="' . $class . ' item" ' . $href . '>«</a>'; |
|
|
|
|
|
|
|
|
|
$html .= $this->create_jump_menu(); |
|
|
|
|
|
|
|
|
|
// Next Page |
|
|
|
|
$class = ( $this->_page == $last ) ? "disabled" : ""; |
|
|
|
|
$href = ( $this->_page == $last ) ? "" : 'href="?limit=' . $this->_limit . '&page=' . ( $this->_page + 1 ) . '"'; |
|
|
|
|
$href = ( $this->_page == $last ) ? "" : $this->do_href($this->_limit, $this->_page + 1 ); |
|
|
|
|
$html .= '<a class="' . $class . ' item" ' . $href . '>»</a>'; |
|
|
|
|
|
|
|
|
|
return $html; |
|
|
|
|
@ -125,7 +183,7 @@ class paginate { |
|
|
|
|
for($i=1; $i <= $last; $i++) { |
|
|
|
|
$option .= ($i == $this->_page) ? "<option value=\"{$i}\" selected>Page {$i}</option>\n":"<option value=\"{$i}\">Page {$i}</option>\n"; |
|
|
|
|
} |
|
|
|
|
return "<label>{$label}<select class=\"{$item}\" onchange=\"window.location='?limit={$this->_limit}&page='+this[this.selectedIndex].value;return false;\">" |
|
|
|
|
return "<label>{$label}<select class=\"{$item}\" onchange=\"window.location='". $this->_url . $this->_url_limit . $this->_limit . $this->_url_page . "'+this[this.selectedIndex].value;return false;\">" |
|
|
|
|
. "{$option}</select>" |
|
|
|
|
. "{$end_label}</label>\n"; |
|
|
|
|
} |
|
|
|
|
@ -145,8 +203,53 @@ class paginate { |
|
|
|
|
$items = "<option selected value=\"{$this->_limit}\">{$this->_limit}</option>\n" . $items; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return "<label>{$label}<select class=\"{$item}\" onchange=\"window.location='?page=1&limit='+this[this.selectedIndex].value;return false;\">{$items}</select>" |
|
|
|
|
$my_page = str_replace("&", "?", $this->_url_page); |
|
|
|
|
$my_limit = str_replace("?", "&", $this->_url_limit); |
|
|
|
|
|
|
|
|
|
return "<label>{$label}<select class=\"{$item}\" onchange=\"window.location='". $my_page . "1" . $my_limit . "'+this[this.selectedIndex].value;return false;\">{$items}</select>" |
|
|
|
|
. "{$end_label}</label>\n"; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* |
|
|
|
|
* mongo db |
|
|
|
|
$limit = $_GET['limit'] ?? 18; |
|
|
|
|
$page = $_GET['page'] ?? 1; |
|
|
|
|
$url = "#pages/bill"; |
|
|
|
|
$query = [ 'user_id' => (string) $user_id ]; |
|
|
|
|
$mongo_options = [ 'projection' => [ 'billing' => 1 ]]; |
|
|
|
|
|
|
|
|
|
$pag = new paginate($collection, $query, "mongodb"); |
|
|
|
|
$r = $pag->mongo_get_data($limit, $page, $mongo_options); |
|
|
|
|
$pag->use_hash_routes($url); |
|
|
|
|
|
|
|
|
|
foreach ($r->data as $bill) { |
|
|
|
|
echo $bill; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
echo $pag->create_links(); |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* |
|
|
|
|
* css |
|
|
|
|
* |
|
|
|
|
.pagination { |
|
|
|
|
display: inline-block; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
.pagination a { |
|
|
|
|
color: black; |
|
|
|
|
float: left; |
|
|
|
|
padding: 8px 16px; |
|
|
|
|
text-decoration: none; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
.pagination a.active { |
|
|
|
|
background-color: #4CAF50; |
|
|
|
|
color: white; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
.pagination a:hover:not(.active) {background-color: #ddd;} |
|
|
|
|
*/ |