最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Show Random images from a folder without repeating using JS or PHP - Stack Overflow

programmeradmin3浏览0评论

I want to show 10- 15 images from a folder in directory but I don't want to show them repeatedly. I also don't want to use MySQL Table

Please help.

currently i am using below code

             $imglist='';
             $img_folder = "gallerypage/small/";

              mt_srand((double)microtime()*1000);

             $imgs = dir($img_folder);

             while ($file = $imgs->read()) {
                 $imglist .= "$file"."|";

             } closedir($imgs->handle);
             $imglist = explode("|", $imglist); 
             //print_r($imglist);
             $no = sizeof($imglist)-2;
             //echo $no;

             for ($i=0; $i<=$no; $i++)
             {
             $random = $i; // mt_rand($i, $no/$i);
             //echo $random;
             $fileb = 
             $image = $imglist[$i];
             $fileb = $image; 
                 if($image != '.' && $image != '..' && $image != 'Thumbs.db' )
                 {

                    //echo '<img src="'.$img_folder.$image.'" border=0>';
                    //if($image != ""){
                     //echo "k".$image."k";
                    echo "<a href='".$img_folderb.$fileb."' rel='lightbox-journey'><img src='".$img_folder.$image."' title='".$image."' alt='".$image."'   height='100'/>";
                    //}
                 }
             }

I want to show 10- 15 images from a folder in directory but I don't want to show them repeatedly. I also don't want to use MySQL Table

Please help.

currently i am using below code

             $imglist='';
             $img_folder = "gallerypage/small/";

              mt_srand((double)microtime()*1000);

             $imgs = dir($img_folder);

             while ($file = $imgs->read()) {
                 $imglist .= "$file"."|";

             } closedir($imgs->handle);
             $imglist = explode("|", $imglist); 
             //print_r($imglist);
             $no = sizeof($imglist)-2;
             //echo $no;

             for ($i=0; $i<=$no; $i++)
             {
             $random = $i; // mt_rand($i, $no/$i);
             //echo $random;
             $fileb = 
             $image = $imglist[$i];
             $fileb = $image; 
                 if($image != '.' && $image != '..' && $image != 'Thumbs.db' )
                 {

                    //echo '<img src="'.$img_folder.$image.'" border=0>';
                    //if($image != ""){
                     //echo "k".$image."k";
                    echo "<a href='".$img_folderb.$fileb."' rel='lightbox-journey'><img src='".$img_folder.$image."' title='".$image."' alt='".$image."'   height='100'/>";
                    //}
                 }
             }
Share Improve this question edited Feb 20, 2012 at 8:38 Code Spy asked Feb 20, 2012 at 8:28 Code SpyCode Spy 9,9644 gold badges71 silver badges46 bronze badges 0
Add a ment  | 

4 Answers 4

Reset to default 5
<SCRIPT LANGUAGE="JavaScript">

<!-- Begin
// Set up the image files to be used.
var theImages = new Array() // do not change this
// To add more image files, continue with the
// pattern below, adding to the array.

theImages[0] = '1.gif'
theImages[1] = '2.gif'
theImages[2] = '3.gif'
theImages[3] = '4.gif'

// do not edit anything below this line

var j = 0
var p = theImages.length;
var preBuffer = new Array()
for (i = 0; i < p; i++){
   preBuffer[i] = new Image()
   preBuffer[i].src = theImages[i]
}
var whichImage = Math.round(Math.random()*(p-1));
function showImage(){
document.write('<img src="'+theImages[whichImage]+'">');
}

//  End -->
</script>

</HEAD>

<!-- STEP TWO: Copy this code into the BODY of your HTML document  -->

<BODY>

<SCRIPT LANGUAGE="JavaScript">



<!-- Begin
showImage();
//  End -->
</script>

<p><center>
<font face="arial, helvetica" size"-2">Free JavaScripts provided<br>
Master</font>
</center><p>

You could glob the folder for images and iterate until you reach 15, none will be repeated

$all_images = glob("/your/directory/{*.jpg, *.JPG, *.JPEG, *.png, *.PNG}", GLOB_BRACE);

// shuffle($all_images); // unment this line to randomize the images

$images = array();

foreach ($all_images as $index => $image) {
     if ($index == 15) break;  // Only print 15 images
     $image_name = basename($image);
     echo "<img src='/public/directory/{$image_name}' />";
}
$images = glob('/path/to/image/dir/{*.jpg,*.png,*.gif}', GLOB_BRACE);

foreach(array_rand($images,10) as $key) //display 10 image
{
    echo '<img src="'.$images[$key].'" />';
}

You can read the files from the directory and store it into an array then shuffle the array and get the first 10 items:

$files = array();
$handle=opendir(".");
while (($file = readdir($handle))!==false) {
    if(is_file($file))
       $files[] = $file;
}

shuffle($files);
$ctr = 0;
foreach($files as $f)
{
    //process file here
    if($ctr++>=10)
     break;
}
发布评论

评论列表(0)

  1. 暂无评论