Ryan alfarisi avatar

Pagination with PHP OOP system #2 Create the previous page, Create the next page, Active class in PHP

alfarisi94

Published: 19 May 2018 › Updated: 19 May 2018Pagination with PHP OOP system #2 Create the previous page, Create the next page, Active class in PHP

Pagination with PHP OOP system #2 Create the previous page, Create the next page, Active class in PHP

Repository

https://github.com/php/php-src

What Will I Learn?

  • Create the previous page function
  • Create the next page function
  • Create active class in PHP

Requirements

  • Basic HTML
  • Basic OOP
  • Localhost (xampp, wampp, or etc)
  • PHP >= 5.6
  • Follow the previous tutorial

Difficulty

  • Intermediate

Tutorial Contents

This tutorial will be easier than the previous tutorial . in this tutorial we will make the user experience better and will set the pagination number more interactive.

Create function prev_page ()

We will create a <a> << </a> tag which when clicked will call the prev_page () function we will create in the pagination class{} in the pagination.php file. in the prev_page () function we will create a ternary operator.
Example:

public function prev_page(){
            return ($this->current_page() > 1) ? $this->current_page() : 1;
        }
  • The idea is we will test whether $this-> current_page () is greater(>) than 1.
  • If result is true then we will return value from $this-> current_page ().
  • If result false then we will return 1. because we do not want to return a value of 0.Because the minimum page is 1. and then we return the result.
  • $this->current_page() is the value we get from the function we have created in the previous tutorial . the following is a function of current_page ()
public function current_page(){
            return isset($_GET['page']) ? (int)$_GET['page'] :1;
        }
  • This function gets its value from query paramater ?page =pageNumber

  • Use function
    Now we can use the function in the <a> tag.

Example:

<a href="?page=prev_page()?>"> << </a>
        <? for($=i; $i<=$pages; $i++): ?>
            <a href="?page="><? echo $i;</a>
        <? endfor; ?>
<a href="?page=next_page()?>"> >> </a>
  • We can use the prev_page () function by accessing$pagination-> prev_page (). Because prev_page() is a function that belongs to the pagination class {}.

  • The Result

prev.gif

Create function next_page ()

We will create a <a> >> </a> tag which when clicked will call the next_page () function we will create in the pagination class{} in the pagination.php file. in the next_page () function we will create a ternary operator.

Example:

public function next_page(){
    return ($this->current_page() < $this->get_pagination_number()) ? $this >current_page()+1 : $this->get_pagination_number();
}
  • The idea is we will compare whether the current_page $this->current_page()is smaller(<) than $this->get_pagination_number() the total number of existing pages.
  • If result is true then we will return value from $this-> current_page () + 1.
  • if result false then we will return the value of $this->get_pagination_number(). Because the maximum value of the page is in the $this->get_pagination_number() function.
  • $this->get_pagination_number() is the value we get from the function we have created in the previous tutorial . the following is a function of get_pagination_number()
    Example:
public function get_pagination_number(){
    return ceil($this->total_records / $this->limit);
}
  • Use function
    Now we can use the function in the <a> tag.

Example:

<a href="?page=prev_page()?>"> << </a>
        <? for($=i; $i<=$pages; $i++): ?>
            <a href="?page="><? echo $i;</a>
        <? endfor; ?>
<a href="?page=next_page()?>"> >> </a>
  • We can use the next_page() function by accessing$pagination-> next_page(). Because next_page() is a function that belongs to the pagination class {}.

  • The Result
    next.gif

Create active class is_active_page()

Before creating the active class function with PHP. We need to pass the parameters to get the page being clicked.
Example:

<a href="?page=prev_page()?>"> << </a>
        <? for($=i; $i<=$pages; $i++): ?>
            <a class="is_active_class($i) ?>" href="?page="><? echo $i;</a>
        <? endfor; ?>
    <a href="?page=next_page()?>"> >> </a>
  • We can get the page number through $i, the loop result from $pages = $pagination->get_pagination_numbers ();.

  • is_active_class()
    We have used the is_active_class () function and passed a parameter as a reference page that is being clicked. Now we can make the function in pagination.php. in the is_active_class() function we will create a ternary operator.

Example:

public function is_active_class($page){
            return ($page == $this->current_page()) ? 'active' : '';
        }
  • The idea is that we will compare whether the current parameter $pageand current page $this->current_page()values ​ have the same(==) value.
  • If result is true then we will return value 'active'. 'active' is the class name that we will create in CSS.
  • If result is false then we will return value ' '. it's mean empty class in CSS.
  • Then we can add the .active class in the CSS section with the following simple style:
    Example:
<style type="text/css">
        .active{
            background: rgb(23, 169, 201);
            color: white;
        }
    </style>
  • The Result
    end.gif

We've managed to create a better user interface than ever before users can also interact with page numbers. in the next section we will create a search system on pagination. thank you for following. hopefully useful for you.

Curriculum

Basic OOP, Fetch Data with PDO database

Proof of Work Done

https://github.com/Ryanalfarisi/paginationOOP

Leave Pagination with PHP OOP system #2 Create the previous page, Create the next page, Active class in PHP to:

Written by

I'm a web developer

Read more #utopian-io posts


Best Posts From Ryan alfarisi

We have not curated any of alfarisi94's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From Ryan alfarisi