2012-02-20 59 views
0

我想从目录中的文件夹中显示10-15个图像,但我不想重复显示它们。我也不想使用MySQL表显示文件夹中的随机图像,不用重复使用JS或PHP

请帮忙。

目前我使用下面的代码

   $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'/>"; 
        //} 
       } 
      } 
+2

到目前为止你做了什么? – 2012-02-20 08:30:45

回答

3
<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> 
+0

已经使用它,它只显示一个图像:( – 2012-02-20 08:56:51

2

你可以glob的对于图像的文件夹和迭代,直到你到达15,没有人会重复

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

// shuffle($all_images); // uncomment 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}' />"; 
} 
+0

你能写一个详细的代码我不是那个专家:)我的目录名是gallerypage/small /。并且还写入显示图像。 – 2012-02-20 08:36:21

+4

如果你想让某人为你写代码,请雇人,否则学习。 – 2012-02-20 08:37:37

+0

@Dagon:同意,我提供的是绝对足够的。 – 2012-02-20 08:38:27

0

您可以从目录中读取文件并将其存储到一个阵列中,然后洗涤阵列并获得前10项:

$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; 
} 
1
$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].'" />'; 
}