Noobie here, Apologies if this question is silly,
I have developed a image match game with set of default images, now i want to access images from directory. the game logic is done in Javascript. I used php to get random 8 images from the directory and the code is as follows.
random.php:
<?php $dire="Annotated Dataset/";
$images = glob($dire. '*.{jpg,jpeg}', GLOB_BRACE);
shuffle($images);
echo 'json_encode(array_slice($images,0,8))';
?>
I want to use the images in the above array in the Javascript function which has switch case. I wanted to use each image from array inside the switch case:
Game.js:
function getgImage(number) {
$.ajax({
url: "random.php",
type: "post",
datatype: "json",
data: {},
success: function (response) {
// You will get response from your PHP page (what you echo or print)
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
}
});
if(number=='1'){
return ranarray[0];
}
else if(number == '2'){
return ranarray[1];
}
else if(number == '3'){
return ranarray[2];
}
else if(number == '4'){
return ranarray[3];
}
else if(number == '5'){
return ranarray[4];
}
else if(number == '6'){
return ranarray[5];
}
else if(number == '7'){
return ranarray[6];
}
else if(number == '8'){
return ranarray[7];
}
else {
return '<img src="resources/logo.png">';
}
}
I tried something like in the above code, but I am not able to get the images in the switch case. Can someone help me with this problem.
I need the array of 8 images from php to be inside switch case of the javascript file, each image for each case. Kindly help me with some solution. Thank you in advance