2016-03-02 26 views
0

因此,我需要在網站上顯示2個或3個不同的圖像,作爲內容的簡單視差分隔符。每頁多個隨機圖像,無需重複

我有一張4張圖片的數組,所以我可以在同一頁面中隨機化,而不會在同一頁面重複它們,但是隨機不會那麼明顯。

我已經潛伏了一會兒,發現這些

Random Image Display, Without Repeat, with Javascript

<script language="javascript"> 
    var imagesArray = [ 
     'images/img-1.jpg', 
     'images/img-2.jpg', 
     'images/img-3.jpg', 
     'images/img-4.jpg', 
     'images/img-5.jpg', 
    ]; 

    var usedImages = {}; 
    var usedImagesCount = 0; 

    function displayImage() { 

     var num = Math.floor(Math.random() * (imagesArray.length)); 
     if (!usedImages[num]) { 
      document.canvas.src = imagesArray[num]; 
      usedImages[num] = true; 
      usedImagesCount++; 
      if (usedImagesCount === imagesArray.length) { 
       usedImagesCount = 0; 
       usedImages = {}; 
      } 
     } else { 
      displayImage(); 
     } 
    } 
</script> 

(這是創建一個按鈕後點擊顯示出的圖像,因爲它遵循

<input onclick="displayImage();" type=button value="Click Here"> 

我試着去適應我的需要,但我的網頁上的呼叫不會產生任何結果)

https://www.daniweb.com/programming/web-development/threads/266181/random-imageslinks-without-repeating

(這一個,我不明白爲什麼,但我不能夠應用的解決方案。不知道這是否是代碼或調用錯圖像的方式......)

http://www.utopiamechanicus.com/article/not-so-random-image-rotation-in-php-for-html-the-sequel/

<?php 
    // rotate images randomly but w/o dups on same page - format: 
    // <img src='rotate.php?i=0'> - rotate image #0 - use 'i=1' 
    // for second, etc 
    // (c) 2004 David Pankhurst - use freely, but please leave in my credit 
    $images = array(// list of files to rotate - add as needed 
     "img1.gif", 
     "img2.gif", 
     "img3.gif", 
     "img4.gif", 
     "img5.gif"); 
    $total = count($images); 
    $secondsFixed = 10; // seconds to keep list the same 
    $seedValue = (int) (time()/$secondsFixed); 
    srand($seedValue); 
    for ($i = 0; $i < $total; ++$i) { // shuffle list 'randomly' 
     $r = rand(0, $total - 1); 
     $temp = $images[$i]; 
     $images[$i] = $images[$r]; 
     $images[$r] = $temp; 
    } 
    $index = (int) ($_GET['i']); // image index passed in 
    $i = $index % $total; // make sure index always in bounds 
    $file = $images[$i]; 
    header("Location: $file"); // and pass file reference back 
?> 

(這個應該工作呼叫通過:

<img src='mysite.com/rotate.php?i=0'> 

但圖像仍然有時重複)

他們沒有爲我工作,所以我決定開始一個新的話題,因爲我真的是新手(其實,我不知道令狀什麼都沒有)在JavaScript,並不能找出什麼是最好的方法。我明白這將是像隨機化項目,分配每個位置(數字,字符,[我]等),然後在我的PHP頁面中調用它。你們能幫我嗎?

+0

'我不知道什麼都不寫*這不是教程網站。 –

+0

所有這些例子有什麼問題,您還需要什麼? – mitkosoft

+0

對不起。我現在已經清理了一切,關於我的需求。我不是要求某人從a,b,c教學(雖然這是我需要的,但是,這是一個不是教程的網站,一切都有它自己的時間和地點),只是爲了幫助我適應這些解決我的需求。 –

回答

1

不知道的是,你想,但這樣會給你隨機圖像(不重複)頁面,每次不同的人上:

<?php 
    $img = array('img1.png', 'img2.png', 'img3.png', 'img4.png', 'img5.png', 'img6.png'); 
    shuffle($img); 
    for($i=0; $i<3; $i++){ 
     print '<img src="'.$img[$i].'"><br>'; 
    } 
?>   

如果你想接收每次呼叫一個形象,一個可能的解決方案是使用會話:

image.php:

<?php 
    session_start(); 
    function getImage() { 
     $img = array('img1.png', 'img2.png', 'img3.png', 'img4.png', 'img5.png'); 
     shuffle($img); 
     foreach($img as $key => $val) { 
      if (!is_array($_SESSION['img'])) { 
       return $val; 
      } elseif (count($_SESSION['img']) >= count($img)) { 
       $_SESSION['img'] = array(); 
      } 
      if (is_array($_SESSION['img']) && !in_array($val, $_SESSION['img'])) { 
       return $val; 
      } 
     } 
    } 
?> 

page.php文件:

<?php 
    include 'image.php'; 
    for ($i = 0; $i < 3; $i++) { 
     $currImg = getImage(); 
     $_SESSION['img'][] = $currImg; 
     echo $currImg . '<br>'; 
    } 
?> 

所有顯示的圖像被存儲到SESSION變量。當顯示所有圖像時,該SESSION var將被重置並開始一個新的循環。

+0

非常感謝您的支持!但事情是,你的腳本在我的頁面上生成了3張圖片,我放在這裏。我需要的是每個呼叫產生一個圖像,不會在同一頁面上重複以前的任何圖像。 PS:非常感謝您對我付出的耐心,將幫助 –

+0

不知道我是否正確應用的代碼,不過,我真的很抱歉,如果我沒有,但我想直接申請,並試圖糾正它,但不能這樣做。 –

+0

我已經把第二個選項通過使用'SESSION'變量,檢查它。這種方法也適用於多頁面,只需在每個頁面的頂部包含'image.php'即可。 – mitkosoft