Skip to content

Commit

Permalink
improve paginate method
Browse files Browse the repository at this point in the history
  • Loading branch information
parsgit committed May 17, 2023
1 parent 2e75158 commit fe731bb
Showing 1 changed file with 37 additions and 27 deletions.
64 changes: 37 additions & 27 deletions src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -954,45 +954,43 @@ public function skip(int $skip)



public function page(int $value,int $take){
$offset = $value*$take;
public function page(int $page_number, int $take)
{
$offset = $page_number * $take;
return $this->take($take)->offset($offset)->get();
}


public function paginate(int $take = 15){
$page_id = $_GET['page']??1;
$url = \Webrium\Url::current();

$list = $this->page($page_id-1, $take);
$count = DB::table($this->TABLE)->count();
public function paginate(int $take = 15,int $page_number = null)
{
if ($page_number <= 0) {
$page_number = 1;
}

$params = new stdClass;

$params->current_page = $page_id;
$params->total = $count;
$params->count = count($list);
$params->per_page = $take;
$list = $this->page($page_number - 1, $take);
$count = $this->clone()->count();

$params->last_page = intval($count/$take);
$params = new stdClass;
$params->last_page = round($count / $take);


$params->first_page_url = $url.'?page=1';
$params->last_page_url = $url.'?page='.$params->last_page;
$nextpage = (($page_number) < $params->last_page) ? ($page_number + 1) : false;

$nextpage = (($page_id)<$params->last_page)?($page_id+1):false;
$params->next_page_url = ($nextpage)?("$url?page=$nextpage"):false;

$prevpage = null;
if($params->current_page<=$params->last_page && $params->current_page > 1){
$prevpage = $params->current_page-1;
$prevpage = false;
if ($page_number <= $params->last_page && $page_number > 1) {
$prevpage = $page_number - 1;
}

$params->prev_page_url = ($prevpage)?("$url?page=$prevpage"):false;

$params->data = $list;
$params->path = $url;


$params->total = $count;
$params->count = count($list);
$params->per_page = $take;
$params->prev_page = $prevpage;
$params->next_page = $nextpage;
$params->current_page = $page_number;
$params->data = $list;

return $params;
}

Expand Down Expand Up @@ -1412,4 +1410,16 @@ protected function clearSource($struct_name){
$s_index = $this->sql_stractur($struct_name);
$this->SOURCE_VALUE[$s_index] = [];
}

private function clone()
{
$db = DB::table($this->TABLE);
$db->PARAMS = $this->PARAMS;
$db->SOURCE_VALUE = $this->SOURCE_VALUE;
$db->clearSource('SELECT');
$db->clearSource('LIMIT');
$db->clearSource('OFFSET');
$db->clearSource('FROM');
return $db;
}
}

0 comments on commit fe731bb

Please sign in to comment.