pagination

137

<nav aria-label="Page navigation example">
  <ul class="pagination">
    <li class="page-item"><a class="page-link" href="#">Previous</a></li>
    <li class="page-item"><a class="page-link" href="#">1</a></li>
    <li class="page-item"><a class="page-link" href="#">2</a></li>
    <li class="page-item"><a class="page-link" href="#">3</a></li>
    <li class="page-item"><a class="page-link" href="#">Next</a></li>
  </ul>
</nav>
<nav aria-label="Page navigation example">
  <ul class="pagination justify-content-center">
    <li class="page-item disabled">
      <a class="page-link" href="#" tabindex="-1" aria-disabled="true">Previous</a>
    </li>
    <li class="page-item"><a class="page-link" href="#">1</a></li>
    <li class="page-item"><a class="page-link" href="#">2</a></li>
    <li class="page-item"><a class="page-link" href="#">3</a></li>
    <li class="page-item">
      <a class="page-link" href="#">Next</a>
    </li>
  </ul>
</nav>
<?php  
$servername='localhost'
$username='root';
$password='';
$dbname = "my_db";
$conn=mysqli_connect($servername,$username,$password,"$dbname");
if(!$conn){
die('Could not Connect My Sql:' .mysql_error());
}
$limit = 10;  
if (isset($_GET["page"])) {
	$page  = $_GET["page"]; 
	} 
	else{ 
	$page=1;
	};  
$start_from = ($page-1) * $limit;  
$result = mysqli_query($conn,"SELECT * FROM user_table ORDER BY userid ASC LIMIT $start_from, $limit");
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
</head>
<body>
<table class="table table-bordered table-striped">  
<thead>  
<tr>  
<th>userid</th>  
<th>First name</th>
<th>Last name</th>
 <th>City name</th>
<th>email</th> 
</tr>  
<thead>  
<tbody>  
<?php  
while ($row = mysqli_fetch_array($result)) {  
?>  
            <tr>  
				<td><?php echo $row["userid"]; ?></td>  
				<td><?php echo $row["first_name"]; ?></td>
				<td><?php echo $row["last_name"]; ?></td>
				<td><?php echo $row["city_name"]; ?></td>
				<td><?php echo $row["email"]; ?></td>			
            </tr>  
<?php  
};  
?>  
</tbody>  
</table>  
<?php  

$result_db = mysqli_query($conn,"SELECT COUNT(id) FROM user_table"); 
$row_db = mysqli_fetch_row($result_db);  
$total_records = $row_db[0];  
$total_pages = ceil($total_records / $limit); 
/* echo  $total_pages; */
$pagLink = "<ul class='pagination'>";  
for ($i=1; $i<=$total_pages; $i++) {
              $pagLink .= "<li class='page-item'><a class='page-link' href='pagination.php?page=".$i."'>".$i."</a></li>";	
}
echo $pagLink . "</ul>";  
?>

</body>
</html>
<?php
namespace Phppot;

use Phppot\DataSource;
use Phppot\Common;
use Phppot\Config;

class Question
{

    private $conn;

    function __construct()
    {
        require_once 'DataSource.php';
        require_once 'Common.php';
        require_once 'Config.php';

        $this->conn = new DataSource();
        $this->common = new Common();
        $this->config = new Config();
    }

    public function getAllProducts()
    {
        $sql = "SELECT * FROM tbl_product";
        $perpage = $this->config::PER_PAGE_LIMIT;
        $currentPage = 1;
        if (isset($_GET['pageNumber'])) {
            $currentPage = $_GET['pageNumber'];
        }
        $startPage = ($currentPage - 1) * $perpage;
        $href = "index.php?";
        if (! empty($_GET['type']) && $_GET['type'] == "prev-next-link") {
            $href = $href . "type=prev-next-link&";
        } else {
            $href = $href . "type=number-link&";
        }
        if ($startPage < 0) {
            $startPage = 0;
        }
        $query = $sql . " limit " . $startPage . "," . $perpage;
        $result = $this->conn->select($query);
        if (! empty($result)) {
            $count = $this->conn->getRecordCount($sql);
            $result["perpage"] = $this->common->showperpage($count, $perpage, $href);
        }
        return $result;
    }
}
?>
<nav aria-label="Page navigation example">
  <ul class="pagination">
    <li class="page-item"><a class="page-link" href="#">Previous</a></>
    <li class="page-item"><a class="page-link" href="#">1</a></li>
    <li class="page-item"><a class="page-link" href="#">2</a></li>
    <li class="page-item"><a class="page-link" href="#">3</a></li>
    <li class="page-item"><a class="page-link" href="#">Next</a></li>
  </ul>
</nav>
private int previousTotal = 0;
    private final int visibleThreshold = 5;
    private RecyclerView postRecyclerView;
    private boolean loading = true;
    private int firstVisibleItem, visibleItemCount, totalItemCount;

 @SuppressLint("SourceLockedOrientationActivity")
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);

 StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL);
        postRecyclerView.setLayoutManager(
                staggeredGridLayoutManager
        );


        postRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {

                visibleItemCount = staggeredGridLayoutManager.getChildCount();
                totalItemCount = staggeredGridLayoutManager.getItemCount();
                int[] firstVisibleItems = null;
                firstVisibleItems = staggeredGridLayoutManager.findFirstVisibleItemPositions(firstVisibleItems);
                if (loading) {
                    if (totalItemCount > previousTotal) {
                        loading = false;
                        previousTotal = totalItemCount;
                    }
                }

                if (!loading && (totalItemCount - visibleItemCount)
                        <= (firstVisibleItem + visibleThreshold)) {
                    getData();

                    loading = true;
                }
            }
        });

return view;
    }

private void getData() {
        databaseReference.addValueEventListener(new ValueEventListener() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                if (snapshot.exists()) {
                    shimmerFrameLayout.stopShimmer();
                    shimmerFrameLayout.setVisibility(View.GONE);
                    postRecyclerView.setVisibility(View.VISIBLE);
                    mUploads.clear();
                    for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
                        Upload upload = dataSnapshot.getValue(Upload.class);
                        assert upload != null;
                        upload.setmKey(dataSnapshot.getKey());
                        mUploads.add(upload);


                    }

                }
                postsAdapter.setUploads(mUploads);

                //notify the adapter
                postsAdapter.notifyDataSetChanged();

            }


            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });
    }
<?php
namespace Phppot;

use Phppot\DataSource;
require_once __DIR__ . '/lib/Question.php';
$question = new Question();
$result = $question->getAllProducts();
?>
<html>
<head>
<title>Product</title>
<meta charset="utf-8">
<meta name="viewport"
    content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link
    href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
    rel="stylesheet"
    integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC"
    crossorigin="anonymous">
<script src="assets/js/product.js"></script>
</head>
<body>
    <div class="container">
        <div class="container pt-5">
            <h2 class="text-center heading py-3">Bootstrap Pagination</h2>
            <table class="table table-bordered" id="table">
                <tr>
                    <th>SL.No</th>
                    <th>Product Name</th>
                    <th class="text-end">Price</th>
                    <th>Model</th>
                </tr>
           <?php
        $questions = $result;
        if (is_array($questions)) {
            for ($i = 0; $i < count($questions) - 1; $i ++) {
                ?>
        <tr>
                    <td><?php echo $questions[$i]["id"];?></td>
                    <td><?php echo $questions[$i]["product_name"];?></td>
                    <td class="text-end"><?php echo $questions[$i]["price"];?></td>
                    <td><?php echo $questions[$i]["model"];?></td>
                </tr>
            <?php }}?>
        </table>
        </div>
    </div>
    <div class="container">
        <div class="container py-3">
            <div class="row">
                <div class="col-md-3 text-left">
                    <select class="form-select d-inline-block"
                        name="navyOp" id="select"
                        onchange="change_url(this.value);">
                        <option value="">Bootstrap Pagination Style</option>
                        <option value="prev-next-link"
                            <?php
                            if (! empty($_GET['type']) && $_GET['type'] == "prev-next-link") {
                                echo "selected";
                            }
                            ?>>With previous next</option>
                        <option value="number-link"
                            <?php
                            if (! empty($_GET['type']) && $_GET['type'] == "number-link") {
                                echo "selected";
                            }
                            ?>>With numbers</option>
                    </select>
                </div>
                <div class="col-md-9 text-right">
                    <nav aria-label="Page navigation example">
                        <ul class="pagination float-end "
                            id="previous-next">
    <?php echo $result["perpage"];?>
            </ul>
                    </nav>
                </div>
            </div>
        </div>
    </div>
</body>
</html>
<?php
namespace Phppot;

use Phppot\Config;

class Common
{

    private $conn;

    function __construct()
    {
        require_once 'DataSource.php';
        require_once 'Config.php';
        $this->conn = new DataSource();
        $this->config = new Config();
    }

    function pagination($count, $perpage, $href)
    {
        $output = '';
        $perpage = $this->config::PER_PAGE_LIMIT;
        $srOnly = "visually-hidden";
        if (! empty($_GET['type']) && $_GET['type'] == "prev-next-link") {
            $srOnly = "";
        }
        if (! isset($_REQUEST["pageNumber"]))
            $_REQUEST["pageNumber"] = 1;

        if ($perpage != 0)
            $pages = ceil($count / $perpage);

        // if pages exists after loop's lower limit
        if ($pages > 1) {
            if ($_REQUEST["pageNumber"] > 1) {
                $previousPage = $_REQUEST["pageNumber"] - 1;
                $output = $output . '<li class="page-item  ' . $srOnly . '"><a href="' . $href . 'pageNumber=' . $previousPage . '"class="page-link text-dark">Previous</a></li>';
            } else {
                $output = $output . '<li class="page-item  ' . $srOnly . '" disabled><a href=""class="page-link text-dark">Previous</a></li>';
            }

            if (($_REQUEST["pageNumber"] - 3) > 0) {
                $output = $output . '<li class="page-item "><a href="' . $href . 'pageNumber=1" class="page-link text-dark">1</a></li>';
            }
            if (($_REQUEST["pageNumber"] - 3) > 1) {
                $output = $output . '<span class="mx-1">...</span>';
            }

            // Loop for provides links for 2 pages before and after current page
            for ($i = ($_REQUEST["pageNumber"] - 2); $i <= ($_REQUEST["pageNumber"] + 2); $i ++) {
                if ($i < 1)
                    continue;
                if ($i > $pages)
                    break;
                if ($_REQUEST["pageNumber"] == $i)
                    $output = $output . '<li class="page-item active"><a class="page-link" id=' . $i . '>' . $i . '</a></li>';
                else
                    $output = $output . '<li class="page-item"><a href="' . $href . "pageNumber=" . $i . '" class="page-link text-dark">' . $i . '</a></li>';
            }

            // if pages exists after loop's upper limit
            if (($pages - ($_REQUEST["pageNumber"] + 2)) > 1) {
                $output = $output . '<span class="mx-1">...</span>';
            }
            if (($pages - ($_REQUEST["pageNumber"] + 2)) > 0) {
                if ($_REQUEST["pageNumber"] == $pages)
                    $output = $output . '<li class="page-item"><a id=' . ($pages) . ' class="page-link text-dark">' . ($pages) . '</a></li>';
                else
                    $output = $output . '<li class="page-item"><a href="' . $href . "pageNumber=" . ($pages) . '" class="page-link text-dark">' . ($pages) . '</a></li>';
            }

            if ($_REQUEST["pageNumber"] < $pages) {
                $nextPage = $_REQUEST["pageNumber"] + 1;
                $output = $output . '<li class="page-item   ' . $srOnly . '"><a href="' . $href . 'pageNumber=' . $nextPage . '"class="page-link text-dark">Next</a></li>';
            } else {
                $output = $output . '<li class="page-item   ' . $srOnly . '" disabled><a href=""class="page-link text-dark">Next</a></li>';
            }
        }
        return $output;
    }

    // function calculate total records count and trigger pagination function
    function showperpage($count, $per_page = "3", $href)
    {
        $perpage = $this->pagination($count, $per_page, $href);
        return $perpage;
    }
}
?>
--
-- Database: `bootstrap_pagination`
--

-- --------------------------------------------------------

--
-- Table structure for table `tbl_product`
--

CREATE TABLE `tbl_product` (
  `id` int(11) NOT NULL,
  `product_name` varchar(255) NOT NULL,
  `price` varchar(255) NOT NULL,
  `model` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

--
-- Dumping data for table `tbl_product`
--

INSERT INTO `tbl_product` (`id`, `product_name`, `price`, `model`) VALUES
(1, 'GIZMORE Multimedia Speaker with Remote Control, Black', '€15.72', '2020'),
(2, 'Black Google Nest Mini', '€41.11', '2021'),
(3, 'Black Digital Hand Band, Packaging Type: Box', '€21.77', '2019'),
(4, 'Lenovo IdeaPad 3 Intel Celeron N4020 14\'\' HD ', '€356.59', '2021'),
(5, 'JBL Airpods', '€27.81', '2020'),
(6, 'Black Google Nest Mini', '€41.11', '2021'),
(7, 'Black Digital Hand Band, Packaging Type: Box', '€21.77', '2019'),
(8, 'Lenovo IdeaPad 3 Intel Celeron N4020 14\'\' HD ', '€356.59', '2021'),
(9, 'Dell New Inspiron 3515 Laptop', '€537.48', '2021');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `tbl_product`
--
ALTER TABLE `tbl_product`
  ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `tbl_product`
--
ALTER TABLE `tbl_product`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=11;

Comments

Submit
0 Comments