title: pagination using Rest API in php
body: <p>I am trying to fetch data from mysql and display them category wise with pagination. I am getting the category wise result but when i change my page data of every category changed too i need to change the data of only category selected . i have setup multi category news option for user so they can add one news to multiple category .help me out here is my code what am trying. </p>
  
  <pre><code>&lt;?php
  
  include'../config.php';
  
  // number of rows to show per page
  $rowsperpage = 2;
  
  
  $main = array();
  $cat = array();
  
  
  $qry = "SELECT * FROM app_category order by id DESC ";
  $res = mysqli_query($con, $qry);
  $records = mysqli_fetch_array($res, MYSQLI_ASSOC);
  $rowcount = mysqli_num_rows($res);
  if ($rowcount &gt; 1) {
      foreach ($res as $records) {
  
          $cat = $records;
  // lets find out how many rows are in the MySQL table
          $sql = "SELECT COUNT(*) FROM app_news WHERE cat_id='" . $cat['id'] . "'";
          $result = mysqli_query($con, $sql) or die(mysql_error());
          $r = mysqli_fetch_row($result);
          $numrows = $r[0];
  // find out total pages
          $totalpages = ceil($numrows / $rowsperpage);
  
          if (isset($_GET['currentpage']) &amp;&amp; is_numeric($_GET['currentpage'])) {
              $currentpage = (int) $_GET['currentpage'];
          } else {
              $currentpage = 1;  // default page number
          }
  
          // if current page is greater than total pages
          if ($currentpage &gt; $totalpages) {
  // set current page to last page
              $currentpage = $totalpages;
          }
  // if current page is less than first page
          if ($currentpage &lt; 1) {
  // set current page to first page
              $currentpage = 1;
          }
  // the offset of the list, based on current page
          $offset = ($currentpage - 1) * $rowsperpage;
  
  
          $qry = "SELECT * FROM app_news where cat_id='" . $cat['id'] . "' LIMIT $offset, $rowsperpage";
          $res1 = mysqli_query($con, $qry);
  
          $json1 = array();
  
          while ($records1 = mysqli_fetch_array($res1, MYSQLI_ASSOC)) {
              $news_id = $records1['news_id'];
              $news_title = $records1['title'];
              $news_des = $records1['description'];
              $news_des = htmlspecialchars_decode(str_replace("&amp;quot;", "\"", $news_des));
              $news_image = $records1['image'];
  
              $json1[] = array("news_id" =&gt; $news_id, "title" =&gt; $news_title, "image" =&gt; $imgapi . $news_image, "description" =&gt; $news_des);
          }
          $cat['All-news'] = array("total no of pages" =&gt; $totalpages, "data" =&gt; $json1);
          $main[] = $cat;
          unset($json1);
          unset($json);
          unset($cat);
      }
      unset($totalpages);
  //header("Content-Type: application/json; charset=utf-8");
      echo json_encode($main, JSON_UNESCAPED_SLASHES);
      unset($main);
      mysqli_close($con);
  } else {
      $minfo = array("success" =&gt; 'false', "message" =&gt; 'No result to display. Please add First category.');
      $jsondata = json_encode($minfo);
      print_r($jsondata);
      mysqli_close($conn);
      exit();
  }
  </code></pre>