Note that there are some explanatory texts on larger screens.

plurals
  1. PORandomize photos from a directory via PHP
    primarykey
    data
    text
    <p>I have the following code that is functional that will randomize the photos I have in my 'photos' folder each time the refresh button is clicked. I know this may not be the most efficient way for this to be coded but for my matter it works. I am looking for help regarding my PHP code that will make the photos more random. I currently have 200+ pictures in the folder and often get repeated pictures more than I'd like. What changes to it can I make? (PS. ignore the AJAX/JavaScript I was playing around with)</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;title&gt;Pictures!&lt;/title&gt; &lt;style type="text/css"&gt; body{ background-color:D3DFDE; } &lt;/style&gt; &lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"&gt;&lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;div id='main'&gt; &lt;?php function randomimages(){ $dirname = isset($_REQUEST['dir'])? $_REQUEST['dir'] : './photos/'; $numimages = isset($_REQUEST['num'])? $_REQUEST['num'] : 1; $pattern = '#\.(jpg|jpeg|png|gif|bmp)$#i'; $files = array(); if($handle = opendir($dirname)){ while(($file = readdir($handle)) !== false){ if(preg_match($pattern, $file)){ array_push($files, "&lt;center&gt;&lt;img src='" . $dirname . $file . "' alt='' /&gt;&lt;/br&gt;&lt;br/&gt;&lt;hr/&gt;&lt;/center&gt;"); } } closedir($handle); shuffle($files); } return implode("&lt;center&gt;&lt;br/&gt;", array_slice($files, 0, $numimages)) . "&lt;br/&gt; &lt;/center&gt;"; } ?&gt; &lt;!-- &lt;center&gt;&lt;a id="myButton" href="#"&gt;MAS PICTURES!&lt;/a&gt;&lt;/center&gt; --&gt; &lt;center&gt;&lt;input type='button' onClick='window.location.reload(true)' value='MAS PICTURES!!!' style="height:200px; width:150px" /&gt;&lt;/center&gt; &lt;hr/&gt; &lt;script type="text/javascript"&gt; $(function() { $("#myButton").click(function() { $("#main").load("index.php"); }); }); &lt;/script&gt; &lt;?php echo randomimages(); ?&gt; &lt;center&gt;Created by: Matt &amp; Joe&lt;/center&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload