I'am very new to PHP and i have tried to browse the forums for my answer but i could not find what i was looking for. So here we go.
So i just want a basic image grabber that gets a unique image from my folder and echo/printing it out on my website, in additional of this i would like to have a next image button, a previous button and a random image button.
This is how far i have come.
Here i have an Example of what i mean. (Yes its very basic but i'am trying to learn)
<?php
$dir = "images";
$images = scandir($dir);
$z = rand(2, sizeof($images)-1);
?>
This code above "generates" the random image but are not unique every time, for example if refresh my page i have a possibility to get the same pic several times. <- Help here would be nice to get a unique image every time you enter the page, or not entering more like when you pressing the random button, but we are getting to that further down.
<div class="container"> <!-- Start Rng Picture -->
<center><div class="col-md-8 col-md-offset-2">
<img src="images/<?php echo $images[$z]; ?>" alt="Sorry Your Image Are On Vacation" class="thumbnail img-responsive">
</div></center> <!-- Close Rng Picture -->
The code above is my front end code for displaying my random image.
My biggest problem i have is to understand how i can get the next,previous random array in my buttons.
<div class="navbar navbar-inverse"> <!-- Start Navbar -->
<div class="navbar-inner">
<center><div class="btn-group">
<button type="button" class="btn btn-lg btn-danger>"Previous</button>
<button type="button" class="btn btn-lg btn-info">Random</button>
<button type="button" class="btn btn-lg btn-success">Next Pic</button>
</div></center>
</div>
</div> <!-- Close Navbar -->
In order to fix the duplicate image problem, add a cookie (session will do), storing the last few(5) images the user has seen. Then before choosing an image to show, remove the five images stored in the cookie.
As far as the buttons go, try looking into Ajax.
use this to avoid duplicate random number to avoid duplicate image. you can set the range.in here is between 1 to 20.
<?php
for ($i=0; $i<=5; $i++)
{
$num[$i] = rand(1,20);
for ($j=0; $j<$i; $j++)
{
while ($num[$j] == $num[$i])
{
$num[$i] = rand(1,20);
$j = 0;
}
}
}
sort($num);
?>
in your view or wherever you want to print random number just do this:
<?php echo $num[1]; ?>
and also u can easily put images in a folder and just chose a unique name for all of them and just append a number to end of each images and display them like this:
<img src="images/imagename<?php echo $num[1]; ?>" alt="Sorry Your Image Are On Vacation" class="thumbnail img-responsive">