Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- header("Access-Control-Allow-Origin: *");
- header("Content-Type: application/json; charset=UTF-8");
- define('JCMS_API', 1);
- include_once ("../../includes/defines.php");
- include_once ("../../includes/configuration.php");
- include_once ("../../class/categories.php");
- $page = isset($_GET['page']) ? intval($_GET['page']) : 1;
- $pagelimit = isset($_GET['pagelimit']) ? intval($_GET['pagelimit']) :intval($category_limit_value);
- ($page < 1)?$page = 1:false;
- ($pagelimit < 0)?$pagelimit = intval($category_limit_value):false;
- $categories = new Categories($conn);
- $totalrows = $categories->getCategory();
- $number = $totalrows->rowCount();
- $start_category = ($page-1) * $pagelimit;
- $GetCategory = $conn->prepare("SELECT * FROM jcms_categories ORDER BY id DESC LIMIT :start_category, :page_limit");
- $GetCategory->bindParam(':start_category', $start_category, PDO::PARAM_INT);
- $GetCategory->bindParam(':page_limit', $pagelimit, PDO::PARAM_INT);
- $GetCategory->execute();
- $result = $GetCategory;
- $total_pages = ceil($number / $pagelimit);
- if($result->rowCount() > 0){
- $category_arr = array();
- $category_arr['data'] = array();
- foreach($result->fetchAll() as $row) {
- $categories = array(
- "id" => $row['id'],
- "title" => $row['title'],
- "uri" => $row['uri'],
- "keyword" => $row['keywords'],
- "description" => $row['description'],
- "main_image" => $row['main_image'],
- "sub-category" => GetChild($row['id'])
- );
- array_push($category_arr['data'], $categories);
- }
- $date = array(
- "success" => array(
- "Data" => $category_arr['data'],
- "Page" => $page,
- "PageLimit" => $pagelimit,
- "Fetched" => count($category_arr['data']),
- "TotalPages" => $total_pages,
- "TotalRecords" => $number
- )
- );
- echo "\n";
- print json_encode($date, JSON_PRETTY_PRINT);
- http_response_code(200);
- }else{
- http_response_code(404);
- echo json_encode(array("error" => "We didn't find any category."));
- }
- /**
- * @param $catid
- * @return array|null
- */
- function GetChild($catid){
- global $conn;
- $subcat = new Categories($conn);
- $GetSubCategories = $subcat->getSubCategoriesByParent($catid);
- if($GetSubCategories->rowCount() > 0) {
- $return = array();
- foreach ($GetSubCategories as $subrow) {
- $sub_arr = array(
- "id" => $subrow['id'],
- "title" => $subrow['title'],
- "uri" => $subrow['uri'],
- "keywords" => $subrow['keywords'],
- "description" => $subrow['description'],
- "main_image" => $subrow['main_image'],
- "parent_id" => $subrow['parent_id']
- );
- array_push($return, $sub_arr);
- }
- }else{
- $return = null;
- }
- return $return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement