Advertisement
asimryu

rest api.php with pagination code

Nov 13th, 2018
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.90 KB | None | 0 0
  1. <?php
  2. $method = $_SERVER['REQUEST_METHOD'];
  3. $request = isset($_GET['request']) ? $_GET['request'] : "";
  4. $uri = explode("/", $request);
  5. $api = isset($uri[0]) ? $uri[0] : "";
  6. $id = isset($uri[1]) ? intval($uri[1]) : 0;
  7. $dbh = new PDO("mysql:host=localhost; dbname=restful; charset=utf8","root","");
  8. $listperpage = 10;
  9.  
  10. if( $api == "friend" ) {
  11.    
  12.     if( $method == "GET" ) {
  13.         $page = isset($_GET['page']) ? intval($_GET['page']) : 1;
  14.         if( ! $page ) $page = 1;
  15.         $start = ($page - 1) * $listperpage;
  16.  
  17.         $sql = "SELECT * FROM friends";
  18.         if( $id ) $sql .= " WHERE id={$id}";
  19.         if( ! $id ) $sql .= " LIMIT {$start}, {$listperpage}";
  20.         if( $res = $dbh->query($sql) ) {
  21.             $friends = $res->fetchAll(PDO::FETCH_ASSOC);
  22.             echo json_encode($friends);
  23.         }
  24.  
  25.     } elseif ($method == "POST") {
  26.  
  27.         $result = array("success"=>"false", "count"=>0);
  28.         $name = isset($_POST['name']) ? $_POST['name'] : "";
  29.         $city = isset($_POST['city']) ? $_POST['city'] : "";
  30.         $phone = isset($_POST['phone']) ? $_POST['phone'] : "";
  31.         $email = isset($_POST['email']) ? $_POST['email'] : "";
  32.         if( $name && $city && $phone && $email ) {
  33.             $sql = "INSERT INTO friends(name, city, phone, email) ";
  34.             $sql .= " VALUES('{$name}','{$city}','{$phone}','{$email}')";
  35.             if( $res = $dbh->query($sql) ) {
  36.                 $count = $res->rowCount();
  37.                 if( $count ) {
  38.                     $result['success'] = "true";
  39.                     $result['count'] = $count;
  40.                 }
  41.             }
  42.         }
  43.  
  44.         echo json_encode($result);
  45.  
  46.     } elseif ($method == "PUT") {
  47.        
  48.         $result = array("success"=>"false", "count"=>0);
  49.  
  50.         if( $id ) {
  51.  
  52.             $putData = file_get_contents("php://input");
  53.             $data = array();
  54.             parse_str($putData, $data);
  55.             if( $data['name'] && $data['city'] && $data['phone'] && $data['email'] ) {
  56.                 $sql = "UPDATE friends SET ";
  57.                 $sql .= " name='{$data['name']}' ";
  58.                 $sql .= ", city='{$data['city']}' ";
  59.                 $sql .= ", phone='{$data['phone']}' ";
  60.                 $sql .= ", email='{$data['email']}' ";
  61.                 $sql .= " WHERE id={$id}";
  62.                 if( $res = $dbh->query($sql) ) {
  63.                     $count = $res->rowCount();
  64.                     if( $count ) {
  65.                         $result['success'] = "true";
  66.                         $result['count'] = $count;
  67.                     }
  68.                 }
  69.             }
  70.         }
  71.  
  72.         echo json_encode($result);
  73.  
  74.     } elseif ($method == "DELETE") {
  75.  
  76.         $result = array("success"=>"false", "count"=>0);
  77.         if( $id ) {
  78.             $sql = "DELETE FROM friends WHERE id={$id}";
  79.             if( $res = $dbh->query($sql) ) {
  80.                 $count = $res->rowCount();
  81.                 if( $count ) {
  82.                     $result['success'] = "true";
  83.                     $result['count'] = $count;
  84.                 }
  85.             }
  86.         }
  87.         echo json_encode($result);
  88.  
  89.     }
  90.  
  91. } elseif( $api == "pages" ) {
  92.    
  93.     if( $method == "GET" ) {
  94.         $pages = 0;
  95.         $total = 0;
  96.         $sql = "SELECT count(*) as cnt FROM friends";      
  97.         if( $res = $dbh->query($sql) ) {
  98.             $data = $res->fetch();
  99.             $total = $data['cnt'];
  100.         }
  101.         if( $total) {
  102.             $pages = ceil($total/$listperpage);
  103.         }
  104.         $result = array("total"=>$total, "pages"=>$pages);
  105.         echo json_encode($result);
  106.     }
  107.  
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement