Advertisement
ZelenY

api.category.read.php

Jul 19th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.00 KB | None | 0 0
  1. <?php
  2. header("Access-Control-Allow-Origin: *");
  3. header("Content-Type: application/json; charset=UTF-8");
  4. define('JCMS_API', 1);
  5. include_once ("../../includes/defines.php");
  6. include_once ("../../includes/configuration.php");
  7. include_once ("../../class/categories.php");
  8. $page = isset($_GET['page']) ? intval($_GET['page']) : 1;
  9. $pagelimit = isset($_GET['pagelimit']) ? intval($_GET['pagelimit']) :intval($category_limit_value);
  10. ($page < 1)?$page = 1:false;
  11. ($pagelimit < 0)?$pagelimit = intval($category_limit_value):false;
  12. $categories = new Categories($conn);
  13. $totalrows = $categories->getCategory();
  14. $number = $totalrows->rowCount();
  15. $start_category = ($page-1) * $pagelimit;
  16. $GetCategory = $conn->prepare("SELECT * FROM jcms_categories ORDER BY id DESC LIMIT :start_category, :page_limit");
  17. $GetCategory->bindParam(':start_category', $start_category, PDO::PARAM_INT);
  18. $GetCategory->bindParam(':page_limit', $pagelimit, PDO::PARAM_INT);
  19. $GetCategory->execute();
  20. $result = $GetCategory;
  21. $total_pages = ceil($number / $pagelimit);
  22. if($result->rowCount() > 0){
  23.     $category_arr = array();
  24.     $category_arr['data'] = array();
  25.     foreach($result->fetchAll() as $row) {
  26.         $categories = array(
  27.             "id" => $row['id'],
  28.             "title" => $row['title'],
  29.             "uri" => $row['uri'],
  30.             "keyword" => $row['keywords'],
  31.             "description" => $row['description'],
  32.             "main_image" => $row['main_image'],
  33.             "sub-category" => GetChild($row['id'])
  34.         );
  35.         array_push($category_arr['data'], $categories);
  36.     }
  37.     $date = array(
  38.         "success" => array(
  39.             "Data" => $category_arr['data'],
  40.             "Page" => $page,
  41.             "PageLimit" => $pagelimit,
  42.             "Fetched" => count($category_arr['data']),
  43.             "TotalPages" => $total_pages,
  44.             "TotalRecords" => $number
  45.         )
  46.     );
  47.     echo "\n";
  48.     print json_encode($date, JSON_PRETTY_PRINT);
  49.     http_response_code(200);
  50. }else{
  51.     http_response_code(404);
  52.     echo json_encode(array("error" => "We didn't find any category."));
  53. }
  54. /**
  55.  * @param $catid
  56.  * @return array|null
  57.  */
  58.     function GetChild($catid){
  59.         global $conn;
  60.         $subcat = new Categories($conn);
  61.         $GetSubCategories = $subcat->getSubCategoriesByParent($catid);
  62.         if($GetSubCategories->rowCount() > 0) {
  63.             $return = array();
  64.             foreach ($GetSubCategories as $subrow) {
  65.                 $sub_arr = array(
  66.                     "id" => $subrow['id'],
  67.                     "title" => $subrow['title'],
  68.                     "uri" => $subrow['uri'],
  69.                     "keywords" => $subrow['keywords'],
  70.                     "description" => $subrow['description'],
  71.                     "main_image" => $subrow['main_image'],
  72.                     "parent_id" => $subrow['parent_id']
  73.                 );
  74.                 array_push($return, $sub_arr);
  75.             }
  76.         }else{
  77.             $return = null;
  78.         }
  79.         return $return;
  80.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement