2013-06-24 43 views
0

如果回答很明顯,我的php知識是有限的,非常抱歉。PHP .rand函數,

我使用.rand函數在單擊鏈接時從3個頁面之一顯示隨機圖像。這些頁面全部命名相同,並位於不同的文件夾(頁面,媒體,文本)中。我遇到的問題是我每次都需要3個不同的頁面/圖像的組合,但每次都會出現相同的3頁/圖像組合。 我想要這 例如 Page1,Media2,Text1。 next ... Page3,Media2,Text3。 next ... Page1,Media3,Text2。

取而代之的是 例如 Page1,Media1,Text1。 next ... Page3,Media3,Text3。 next ... Page2,Media2,Text2。

該解決方案可能很簡單,但我似乎無法弄清楚。

這裏是我的代碼:

<body> 
<div id="wrapper"> 
    <div id="click"><a href="index.php?p=<?php echo 'include-'.rand(1,3); ?>">CLICK HERE</a>></div> 
    <div id="content"> 
    <?php 
    if (!empty($_GET['p'])) { 
     $pages_dir = 'pages'; 
     $pages = scandir($pages_dir, 0); 
     unset($pages[0], $pages[1]); 

     $p = $_GET['p']; 

     if (in_array($p.'.inc.php',$pages)) { 
      include($pages_dir.'/'.$p.'.inc.php'); 
     } else { 
      echo 'Sorry, page not found.'; 
     } 
    } 
    ?> 
    </div> 
    <div id="content2"> 
    <?php 
    if (!empty($_GET['p'])) { 
     $media_dir = 'media'; 
     $media = scandir($media_dir, 0); 
     unset($media[0], $media[1]); 

     $p = $_GET['p']; 

     if (in_array($p.'.inc.php',$media)) { 
      include($media_dir.'/'.$p.'.inc.php'); 
     } else { 
      echo 'Sorry, page not found.'; 
     } 
    } 
    ?> 
    </div> 
    <div id="content3"> 
    <?php 
    if (!empty($_GET['p'])) { 
     $text_dir = 'text'; 
     $text = scandir($text_dir, 0); 
     unset($text[0], $text[1]); 

     $p = $_GET['p']; 

     if (in_array($p.'.inc.php',$text)) { 
      include($text_dir.'/'.$p.'.inc.php'); 
     } else { 
      echo 'Sorry, page not found.'; 
     } 
    } 
    ?> 
    </div> 
</div> 
</body> 
+0

這可以通過一個循環和一個函數來簡化,但是你的問題是你只產生一次隨機數,你需要爲每個部分執行 – 2013-06-24 20:59:53

回答

0

您所使用的功能「RAND()」只有一次,所以你將有隻有一個隨機數。如果您想要更多的號碼,請多次使用該功能。例如:

$rnd_1 = rand(0,3); 
$rnd_2 = rand(0,3); 
$rnd_3 = rand(0,3); 

他們每個人都可能會產生一個唯一的編號;)

+0

使用'mt_rand()'而不是'rand()' 。它生成更好的隨機數。 –

+0

使用此方法,唯一編號可能相同。 – PHPglue

+0

@ SverriM.Olsen你真的認爲這種用法需要一個「更好的」隨機數嗎? – 2013-06-24 21:06:59

0

您使用蘭特只有一次在這裏:

<a href="index.php?p=<?php echo 'include-'.rand(1,3); ?>"> 

解決辦法有兩個:

  • 不要使用rand作爲GET參數,只需在$ _GET ['p']設置時生成隨機數。

  • 另外創建兩個GET參數,例如, $ _GET ['m']用於媒體,$ _GET ['c']用於內容並用它們替換$ _GET ['p']。

0

嘗試使用shuffle(glob('*.png'));,或者只是shuffle();你的照片的數組。 請參閱:

http://php.net/manual/en/function.glob.php

http://php.net/manual/en/function.shuffle.php

內水珠字符串可以使用任何文件擴展名來代替。

您的代碼可能是這樣的:

$allPics = shuffle(glob('*png')); $pic = array_slice($allPics, 0, 3); 

現在$pic[0]$pic[1]$pic[2]握住你的圖片。

個人而言,我認爲你應該使用JavaScript,如果你只是想返回3張圖片。該代碼會是這個樣子的:

一是外部picloader.js文件:

//+ Jonas Raoni Soares Silva - Google 
//@ http://jsfromhell.com/array/shuffle [v1.0] 
function shuffle(o){ //v1.0 
    for(var j,x,i=o.length; i; j=parseInt(Math.random()*i),x=o[--i],o[i]=o[j],o[j]=x); 
    return o; 
} 
//everything else by Jason Raymond Buckley 
function E(e){ 
    return document.getElementById(e); 
} 
function picLoader(clickId, outIdsArray, imageArray){ 
    E(clickId).onclick = function(){ 
    var oa = outIdsArray, il = oa.length, sh = shuffle(imageArray), pic = sh.slice(0, il); 
    for(var i in oa){ 
     E(oa[i]).innerHTML = "<img src='"+pic[i]+"' />"; 
    } 
    } 
} 

您的網頁上的代碼現在應該是這樣的:

<body> 
    <div id="wrapper"> 
    <input type='button' value='CLICK HERE' id='showPics' /> 
    <div id="content"></div> 
    <div id="content2"></div> 
    <div id="content3"></div> 
    </div> 
    <script type='text/javascript' src='picloader.js'></script> 
    <script type='text/javascript'> 
    var imageArray = ['img1.png', 'img2.png', 'img3.png', 'img4.png', 'img5.png', 'img6.jpg', 'img7.jpg']; 
    picLoader('showPics', ['content', 'content1', 'content2'], imageArray); 
    </script> 
</body> 
</html> 

這應該對任何工作輸出元素的數量,只要imageArray至少與包含輸出內容ID的數組一樣長。