How can I process them so that I can use the data of both requests? UI:
async function getSpecialties(){
let res = await fetch ('http://server-npk-web-core/specialties');
let specialties = await res.json();
})
}
async function getSubjectsSpecial(){
let res = await fetch ('http://server-npk-web-core/specialties');
let subjectsSpecial = await res.json();
})
}
BLL: index.php
if($method === 'GET'){
if($type === 'subjects'){
getSubjects($pdo);
} elseif($type === 'specialties'){
getSpecialties($pdo);
getSubjectsSpecial($pdo);
}
specialties.php
function getSpecialties($pdo){
$specialties = 'SELECT * FROM `specialties`';
$stmt = $pdo -> query($specialties);
while ($special = $stmt->fetch()){
$specialtiesList[] = $special;
}
echo json_encode($specialtiesList);
}
function getSubjectsSpecial($pdo){
$subjectsSpecial = 'SELECT `subjects`.`title` FROM `subjects` WHERE `id_specialties` = 2';
$stmt = $pdo -> query($subjectsSpecial);
while ($subjectSpecial = $stmt->fetch()){
$subjectsSpecialList[] = $subjectSpecial;
}
echo json_encode($subjectsSpecialList);
}
P.S. Don't beat me with sticks, I'm learning on my own -_-
To use the same endpointto process two or more different requests you need to provide the means by which the server can choose the appropriate actions to take. As your fetch requests are done using GET you can provide a querystring parameter which is then used server-side to fork program logic. For example:
<?php
//-------------------------------
//server-npk-web-core/specialties
//-------------------------------
function getSpecialties($pdo){
$specialties = 'SELECT * FROM `specialties`';
$stmt = $pdo -> query($specialties);
while ($special = $stmt->fetch()){
$specialtiesList[] = $special;
}
echo json_encode($specialtiesList);
}
function getSubjectsSpecial($pdo){
$subjectsSpecial = 'SELECT `subjects`.`title` FROM `subjects` WHERE `id_specialties` = 2';
$stmt = $pdo -> query($subjectsSpecial);
while ($subjectSpecial = $stmt->fetch()){
$subjectsSpecialList[] = $subjectSpecial;
}
echo json_encode($subjectsSpecialList);
}
if( !empty( $_GET['action'] ) ){
switch( $_GET['action'] ){
case 'specialties':
getSpecialties($pdo)
break;
case 'subjectsspecial':
getSubjectsSpecial( $pdo );
break;
default:
echo 'Error';
break;
}
}
?>
And the modified fetch requests with new querystring parameter
<script>
async function getSpecialties(){
let res = await fetch ('http://server-npk-web-core/specialties?action=specialties');
let specialties = await res.json();
})
};
async function getSubjectsSpecial(){
let res = await fetch ('http://server-npk-web-core/specialties?action=subjectsspecial');
let subjectsSpecial = await res.json();
})
};
</script>