Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. <?php
  2. $servername = "localhost";
  3. $username = "root";
  4. $password = "";
  5. $dbname = "sakila";
  6.  
  7. // Create connection
  8. $conn = new mysqli($servername, $username, $password, $dbname);
  9. // Check connection
  10. if ($conn->connect_error) {
  11. die("Connection failed: " . $conn->connect_error);
  12. }
  13.  
  14. $sql = "SELECT count(*) as total FROM actor";
  15. $query = $sql;
  16. $result = mysqli_query($conn, $query);
  17. $row = mysqli_fetch_array($result); //Total de registros falta adicionar la condicion where
  18. $result->close();
  19.  
  20. // getting parameters required for pagination
  21. $jsondata = array(); //data json
  22.  
  23. $total = $row[0];
  24.  
  25. if(isset($_GET['current_page'])){
  26. $current_page = $_GET['current_page'];
  27. } else { $current_page = 1; }
  28.  
  29.  
  30. if(isset($_GET['per_page'])){
  31. $per_page= $_GET['per_page'];
  32. } else { $per_page = 10; }
  33.  
  34. $startPage = ($current_page - 1) * $per_page; // paginacion con LIMIT
  35. if($startPage < 0) $startPage = 0; //
  36.  
  37. //adding limits to select query
  38. $sql = "SELECT * FROM actor";
  39. $query = $sql . " limit " . $startPage . "," . $per_page;
  40. $result = mysqli_query($conn, $query);
  41.  
  42. $last_page = ceil($total / $per_page);
  43.  
  44. $jsondata["pagination"] = (["total" => $total,
  45. "per_page" => $per_page,
  46. "current_page" => $current_page,
  47. "next_page_url" => '',
  48. "last_page" => $last_page
  49. ]);
  50.  
  51. if($result->num_rows > 0) {
  52. while($row = mysqli_fetch_assoc($result)) {
  53. $jsondata["data"][] = $row;
  54. }
  55. }
  56. $result->close();
  57.  
  58. header('Content-type: application/json; charset=utf-8');
  59. echo json_encode($jsondata);
  60.  
  61. $conn->close();
  62.  
  63. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement