如果回答很明顯,我的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>
這可以通過一個循環和一個函數來簡化,但是你的問題是你只產生一次隨機數,你需要爲每個部分執行 – 2013-06-24 20:59:53