Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从PHP到Javascript获取图像数组_Javascript_Php - Fatal编程技术网

如何从PHP到Javascript获取图像数组

如何从PHP到Javascript获取图像数组,javascript,php,Javascript,Php,Noobie如果这个问题很愚蠢,我道歉 我已经开发了一个带有默认图像集的图像匹配游戏,现在我想从目录中访问图像。游戏逻辑是用Javascript完成的。我使用php从目录中随机获得8个图像,代码如下 random.php: <?php $dire="Annotated Dataset/"; $images = glob($dire. '*.{jpg,jpeg}', GLOB_BRACE); shuffle($images); echo 'json_encode(array_sl

Noobie如果这个问题很愚蠢,我道歉

我已经开发了一个带有默认图像集的图像匹配游戏,现在我想从目录中访问图像。游戏逻辑是用Javascript完成的。我使用php从目录中随机获得8个图像,代码如下

random.php:

 <?php $dire="Annotated Dataset/";
 $images = glob($dire. '*.{jpg,jpeg}', GLOB_BRACE);
 shuffle($images);
   echo 'json_encode(array_slice($images,0,8))';
 ?>

我想在带有switch case的Javascript函数中使用上面数组中的图像。我想使用交换机机箱内阵列中的每个图像:

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">';

   }
   }
函数getgImage(数字){
$.ajax({
url:“random.php”,
类型:“post”,
数据类型:“json”,
数据:{},
成功:功能(响应){
//您将从PHP页面获得响应(您回送或打印的内容)
},
错误:函数(jqXHR、textStatus、errorshown){
日志(textStatus,errorshown);
}
});
如果(数字='1'){
返回数组[0];
}
else if(数字='2'){
返回数组[1];
}
else if(数字='3'){
返回数组[2];
} 
else if(数字='4'){
返回数组[3];
}
else if(数字='5'){
返回数组[4];
}
如果为else(数字='6'){
返回数组[5];
}
else if(数字='7'){
返回数组[6];
}
else if(数字='8'){
返回数组[7];
}
否则{
返回“”;
}
}
我在上面的代码中尝试了类似的操作,但是我无法在开关盒中获得图像。有人能帮我解决这个问题吗


我需要php中的8个图像数组位于javascript文件的开关盒内,每个图像对应于每个盒。请帮我解决一些问题。提前感谢您

您可以使用Ajax从PHP获取它们,并在JavaScript中使用它们

提示:
die(json_encode(getgImage(1));

imagefetcher.php game.js

$.ajax({
url:“imagefetcher.php”,
类型:“post”,
数据:{},
成功:功能(响应){
//您将从PHP页面获得响应(您回送或打印的内容)
警报('来自PHP文件的响应:'+响应);
},
错误:函数(jqXHR、textStatus、errorshown){
日志(textStatus,errorshown);
}
});

echo“json编码(数组切片($images,0,8));删除quotation@anikislamShojib我尝试删除引号,但输出仍然相同,我得到的是字符串值,而不是图像(即,我得到的是字母e、p、h等,而不是图像)你能告诉我如何在我的代码中使用这个ajax吗?我以前从未使用过它。我会对我有所帮助。只需将它添加到标记之间,并将url链接到php文件,它将提供所需结果的ajax请求。它将输出一个json响应(成功函数中的响应)使用从PHP发送的数据。他说javascript,这个解决方案是使用Jquery库。他可以使用
var xhr=new-XMLHttpRequest();xhr.open(“POST”)来做同样的事情https://www.mywebsite.com/test.php“”;xhr.send();if(xhr.readyState==4)if(xhr.status==200)var your_array_answer=xhr.responseText;
因此我添加了这段ajax代码,并在函数response{echo'json_encode(array_slice($images,0,8))';}中返回$images[i]…我是对的吗?@aaronnohuan知道我理解你的建议,但我不知道如何实现它,如果我在php中回显图像并在javascript文件中使用ajax调用,如何在If条件中获取图像。我已经更新了代码,你能给我看一段代码如何在If条件中使用它们吗
function getRandomImage() {

  $dire   = "Annotated Dataset/";
  $images = glob($dire. '*.{jpg,jpeg}', GLOB_BRACE);
  $images = shuffle($images);

  return $images[rand(0, (count($images) - 1))];

}

die(json_encode(getRandomImage()));
<script>
 $.ajax({
        url: "imagefetcher.php",
        type: "post",
        data: {},
        success: function (response) {
           // You will get response from your PHP page (what you echo or print)
           alert('Response from PHP file: ' + response);
        },
        error: function(jqXHR, textStatus, errorThrown) {
           console.log(textStatus, errorThrown);
        }
    });
</script>