query($sql); if ($result!== false && $result->num_rows > 0) { $listArray = []; while($row = $result->fetch_assoc()) { ////////////////////////////////////////////////////////////////////////////////////////////// $swapsql = "SELECT *, swap.CreatedAt AS SwapCreatedAt, swap.UpdatedAt AS SwapUpdatedAt FROM swap swap"; $swapresult = $conn->query($swapsql); $swaplistArray= []; if ($swapresult!== false && $swapresult->num_rows > 0) { while($swaprow = $swapresult->fetch_assoc()) { $swapvalue = array( "SwapID"=>(int)$swaprow["SwapID"], "RiderID"=>$swaprow["RiderID"], "SwapReferID"=>$swaprow["SwapReferID"], "SwapRechargeID"=>$swaprow["SwapRechargeID"], "ReturnBatterID"=>$swaprow["ReturnBatterID"], "SwappedBatterID"=>$swaprow["SwappedBatterID"], "IsFreeSwap"=>$swaprow["IsFreeSwap"], "SwapCreatedAt"=>$swaprow["SwapCreatedAt"], "SwapUpdatedAt"=>$swaprow["SwapUpdatedAt"], ); $swaplistArray[]= $swapvalue; } } $PaidSwapCount =array_count_values(array_column($swaplistArray, 'IsFreeSwap'))["0"]??0; $FreeSwapCount =array_count_values(array_column($swaplistArray, 'IsFreeSwap'))["1"]??0; $swapCount = count($swaplistArray); /////////////////////////////////////////////////////////////////////////////////////////////// $rechargesql = "SELECT *, recharge.CreatedAt AS RechargeCreatedAt, recharge.UpdatedAt AS RechargeUpdatedAt FROM rechargehistory recharge"; $rechargeresult = $conn->query($rechargesql); $rechargelistArray= []; if ($rechargeresult!== false && $rechargeresult->num_rows > 0) { while($rechargerow = $rechargeresult->fetch_assoc()) { $rechargevalue = array( "RechargeID"=>(int)$rechargerow["RechargeHistoryID"], "RiderID"=>$rechargerow["RiderID"], "TransactionID"=>$rechargerow["TransactionID"], "PaymentTypeID"=>$rechargerow["PaymentTypeID"], "RechargeCreatedAt"=>$rechargerow["RechargeCreatedAt"], "RechargeUpdatedAt"=>$rechargerow["RechargeUpdatedAt"], ); $rechargelistArray[]= $rechargevalue; } } $rechargeCount = count($rechargelistArray); ///////////////////////////////////////////////////////////////////////////////////////////////// $servicehistorysql = "SELECT *, servicehistory.CreatedAt AS ServiceHistoryCreatedAt, servicehistory.UpdatedAt AS ServiceHistoryUpdatedAt FROM servicehistory servicehistory"; $servicehistoryresult = $conn->query($servicehistorysql); $servicehistorylistArray= []; if ($servicehistoryresult!== false && $servicehistoryresult->num_rows > 0) { while($servicehistoryrow = $servicehistoryresult->fetch_assoc()) { $servicehistoryvalue = array( "ServiceHistoryID"=>(int)$servicehistoryrow["ServiceHistoryID"], "AssetID"=>$servicehistoryrow["AssetID"], "isServiced"=>(int)$servicehistoryrow["isServiced"], "InWard"=>$servicehistoryrow["InWard"], "outWard"=>$servicehistoryrow["outWard"], "ServiceHistoryCreatedAt"=>$servicehistoryrow["ServiceHistoryCreatedAt"], "ServiceHistoryUpdatedAt"=>$servicehistoryrow["ServiceHistoryUpdatedAt"], ); $servicehistorylistArray[]= $servicehistoryvalue; } } $InCompletedServiceCount =array_count_values(array_column($servicehistorylistArray, 'isServiced'))["0"]??0; $servicehistoryCount = count($servicehistorylistArray); ////////////////////////////////////////////////////////////////////////////////////////////////// $assetsql = "SELECT *, asset.CreatedAt AS AssetCreatedAt, asset.UpdatedAt AS AssetUpdatedAt, assetcategory.CreatedAt AS AssetCategoryCreatedAt, assetcategory.UpdatedAt AS AssetCategoryUpdatedAt FROM asset asset JOIN assetcategory assetcategory ON assetcategory.AssetCategoryID =asset.AssetCategoryID "; $assetresult = $conn->query($assetsql); $assetlistArray= []; if ($assetresult!== false && $assetresult->num_rows > 0) { while($sassetrow = $assetresult->fetch_assoc()) { $assetvalue = array( "AssetID"=>$sassetrow["AssetID"], "AssetName"=>$sassetrow["AssetName"], "AssetCategoryID"=>(int)$sassetrow["AssetCategoryID"], "AssetCategory"=>array( "AssetCategoryID"=>(int)$sassetrow["AssetCategoryID"], "AssetCategoryTitle"=>$sassetrow["AssetCategoryTitle"], "AssetCategoryCreatedAt"=>$sassetrow["AssetCategoryCreatedAt"], "AssetCategoryUpdatedAt"=>$sassetrow["AssetCategoryUpdatedAt"], ), "IsInServiceCenter"=>(int)$sassetrow["IsInServiceCenter"], "AssetCreatedAt"=>$sassetrow["AssetCreatedAt"], "AssetUpdatedAt"=>$sassetrow["AssetUpdatedAt"], ); $assetlistArray[]= $assetvalue; } } $assetCount = count($assetlistArray); ///////////////////////////////////////////////////////////////////////////////////////////////////// $value = array( "UserData"=>array( "UserID"=>(int)$row["UserID"], "UserName"=>$row["UserName"], "PhoneNumber"=>$row["PhoneNumber"], "IsAdmin"=>(int)$row["IsAdmin"], "UserCreatedAt"=>$row["UserCreatedAt"], "UserUpdatedAt"=>$row["UserUpdatedAt"], ), "SwapData"=>array( "HistoryData"=>array( "TotalCount"=>$swapCount, "FreeSwapCount"=>$FreeSwapCount, "PaidSwapCount"=>$PaidSwapCount, "History"=>$swaplistArray ), ), "RechargeData"=>array( "HistoryData"=>array( "TotalCount"=>$rechargeCount, "History"=>$rechargelistArray ), ), "ServiceData"=>array( "HistoryData"=>array( "TotalCount"=>$servicehistoryCount, "InCompletedServiceCount"=>$InCompletedServiceCount, "History"=>$servicehistorylistArray ), ), "AssetData"=>array( "HistoryData"=>array( "TotalCount"=>$assetCount, "History"=>$assetlistArray ), ), "Message"=>"Date successfully get" ); $listArray[]= $value; } $count = count($listArray); if($count==1){ // Use json_encode() function $json = json_encode($listArray[0]); }else { // Use json_encode() function $json = json_encode($listArray); } }else { http_response_code(404); $errorMesage= $conn->error; if ($errorMesage=="") { $errorMesage = "UserID not available"; if (empty($_GET["UserID"])) { $errorMesage = "UserID is empty"; } } // Declare an array with error message $value = array( "Message"=>"Failed to get data", "Error"=>$errorMesage ); // Use json_encode() function $json = json_encode($value); } // Display the output echo($json); $conn->close();