简体   繁体   中英

How to send a image array from one PHP file to another PHP?

I have developed a memory game which gets its image randomly from a directory. once The game is completed, I wanted to display the images which was used in the game.

The code for game logic which gets the images randomly from the directory:

(gameply.php)

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

if(number=='1'){
return '<img src="<?php echo "$images[0]"; ?>">';

}
else if(number == '2'){
return '<img src="<?php echo "$images[1]"; ?>">';

}

else if(number == '3'){
return '<img src="<?php echo "$images[2]"; ?>">';

} 

else if(number == '4'){
return '<img src="<?php echo "$images[3]"; ?>">';

}

else if(number == '5'){
return '<img src="<?php echo "$images[4]"; ?>">';

}
else if(number == '6'){
return '<img src="<?php echo "$images[5]"; ?>">';
}

else if(number == '7'){
return '<img src="<?php echo "$images[6]"; ?>">';
}

else if(number == '8'){
return '<img src="<?php echo "$images[7]"; ?>">';
}
else {
return '<img src="resources/logo.png">';

}

} 
</script>

The above code is for the game, where random images from directory are placed inside the if condition, What i want is to get these exact images from this php and display it as a Gallery in another php

I tried with the following code:

(gallery.php)

<?php
 session_start();

 require 'gameply.php';?>

 <!DOCTYPE html>
 <html>
 <head>
 <title> gallery</title>
 <link href="gallery.css" rel="stylesheet">
 </head>
 <body>

 <div id="gallery"><?php
  foreach ($images as $i) {?>
    <img src="<?php echo "$i"; ?>">   <?php }
?>

 </div>
 </body>
 </html>

But the above code when executed, displays a set of different images rather than the one which is used in the game.

I want to get the same set of images that I use in the game( gameply.php ) to be displayed in Gallery( gallery.php ). I tried with session_start() , but still, I get the wrong output.

Your code is showing what you've told it to. You're asking it to return the $images array which is NOT what you're returning from your JavaScript function. I'm assuming that the items in the function are what you want to return correct?

To use sessions you need to do more than just call session_start(). You have to assign session variables that you can use later. I would read up about sessions if you'd like to go that route.

This should work to return the items from your function. Inside gallery.php:

<div id="gallery"</div>
<script>
  document.getElementById("gallery").innerHTML = getgImage();
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM