| Server IP : 127.0.0.1 / Your IP : 192.168.99.1 Web Server : Apache/2.4.35 (Win64) OpenSSL/1.1.1b mod_fcgid/2.3.9 System : Windows NT WIN-JEJMB9EN0L0 10.0 build 14393 (Windows Server 2016) AMD64 User : Administrator ( 0) PHP Version : 7.4.33 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /laragon3/www/bpkd/api/ |
Upload File : |
<?php
// SET HEADER
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: access");
header("Access-Control-Allow-Methods: GET");
header("Access-Control-Allow-Credentials: true");
header("Content-Type: application/json; charset=UTF-8");
// INCLUDING DATABASE AND MAKING OBJECT
require 'database.php';
$db_connection = new Database();
$conn = $db_connection->dbConnection();
// CHECK GET ID PARAMETER OR NOT
if(isset($_GET['id']))
{
//IF HAS ID PARAMETER
$post_id = filter_var($_GET['id'], FILTER_VALIDATE_INT,[
'options' => [
'default' => 'all_posts',
'min_range' => 1
]
]);
}
else{
$post_id = 'all_posts';
}
// MAKE SQL QUERY
// IF GET POSTS ID, THEN SHOW POSTS BY ID OTHERWISE SHOW ALL POSTS
$sql = is_numeric($post_id) ? "SELECT * FROM download2 WHERE id_download='$post_id'" : "SELECT * FROM download2 ";
$stmt = $conn->prepare($sql);
$stmt->execute();
//CHECK WHETHER THERE IS ANY POST IN OUR DATABASE
if($stmt->rowCount() > 0){
// CREATE POSTS ARRAY
$posts_array = [];
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
$post_data = [
'id' => $row['id_download'],
'judul' => $row['judul'],
'nama_file' => $row['nama_file'],
'jenislaporan' => $row['grup']
];
// PUSH POST DATA IN OUR $posts_array ARRAY
array_push($posts_array, $post_data);
}
//SHOW POST/POSTS IN JSON FORMAT
echo json_encode($posts_array);
}
else{
//IF THER IS NO POST IN OUR DATABASE
echo json_encode(['message'=>'No post found']);
}
?>