Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $servername = "localhost";
- $username = "root";
- $password = "";
- $dbname = "sakila";
- // Create connection
- $conn = new mysqli($servername, $username, $password, $dbname);
- // Check connection
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- $sql = "SELECT count(*) as total FROM actor";
- $query = $sql;
- $result = mysqli_query($conn, $query);
- $row = mysqli_fetch_array($result); //Total de registros falta adicionar la condicion where
- $result->close();
- // getting parameters required for pagination
- $jsondata = array(); //data json
- $total = $row[0];
- if(isset($_GET['current_page'])){
- $current_page = $_GET['current_page'];
- } else { $current_page = 1; }
- if(isset($_GET['per_page'])){
- $per_page= $_GET['per_page'];
- } else { $per_page = 10; }
- $startPage = ($current_page - 1) * $per_page; // paginacion con LIMIT
- if($startPage < 0) $startPage = 0; //
- //adding limits to select query
- $sql = "SELECT * FROM actor";
- $query = $sql . " limit " . $startPage . "," . $per_page;
- $result = mysqli_query($conn, $query);
- $last_page = ceil($total / $per_page);
- $jsondata["pagination"] = (["total" => $total,
- "per_page" => $per_page,
- "current_page" => $current_page,
- "next_page_url" => '',
- "last_page" => $last_page
- ]);
- if($result->num_rows > 0) {
- while($row = mysqli_fetch_assoc($result)) {
- $jsondata["data"][] = $row;
- }
- }
- $result->close();
- header('Content-type: application/json; charset=utf-8');
- echo json_encode($jsondata);
- $conn->close();
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement