我不知道如何处理这件事,也找不到任何资源来处理同一类事情,所以任何人都能指出正确的方向,我们将不胜感激。
我试图用PHP生成一个随机数,然后把它写在这里的一个文件中。我现在不确定这是否是最好的方法。新的问题产生了,现在我做了进一步的研究,并改变了范围。
我想展示一个随机DIV,我需要能够添加最大数字。我不知道是在数字还是ID之间循环,但总体感觉是这样的。
<div id="day-1">
<p>Show if Day 1 is randomly chosen</p>
</div>
<div id="day-2">
<p>Show if Day 2 is randomly chosen</p>
</div>
<div id="day-3">
<p>Show if Day 3 is randomly chosen</p>
</div>
<div id="day-4">
<p>Show if Day 4 is randomly chosen</p>
</div>
<div id="day-5">
<p>Show if Day 5 is randomly chosen</p>
</div>
<div id="day-6">
<p>Show if Day 6 is randomly chosen</p>
</div>
<div id="day-7">
<p>Show if Day 7 is randomly chosen</p>
</div>
<div id="day-8">
<p>Show if Day 8 is randomly chosen</p>
</div>
<div id="day-9">
<p>Show if Day 9 is randomly chosen</p>
</div>
<div id="day-10">
<p>Show if Day 10 is randomly chosen</p>
</div>
我不想加载每个DIV,所以我只想回显随机选择的DIV,而不想其他。
我用这个来选择一个随机数,然后写
<?php
$min=1;
$max=100;
echo rand($min,$max);
$file = fopen("./randomnumber.txt","a+ n");
fwrite($file,$min,$max);
fclose($file);
?>
但它会向一个输出写入不同的数字。我需要将每个随机数存储到一个文件中。我知道这不起作用,但我目前的想法是根据随机数显示每个对应的DIV。我不知道如何只回显一个随机DIV,所以不是所有东西都必须加载。
<?php
$min=1;
$max=10; // I'd update this to 11 and then add an 11th DIV when another day is added.
echo rand($min,$max);
$file = fopen("./randomnumber.txt","a+ n");
fwrite($file,$min,$max);
fclose($file);
if (rand === '1') {
echo "
<div id="day-1">
<p>Show if Day 1 is randomly chosen</p>
</div>
";
}
else if (rand === '2') {
echo "
<div id="day-2">
<p>Show if Day 2 is randomly chosen</p>
</div>
";
}
else if (rand === '3') {
echo "
<div id="day-3">
<p>Show if Day 3 is randomly chosen</p>
</div>
";
}
else if (rand === '4') {
echo "
<div id="day-4">
<p>Show if Day 4 is randomly chosen</p>
</div>
";
}
else if (rand === '5') {
echo "
<div id="day-5">
<p>Show if Day 5 is randomly chosen</p>
</div>
";
}
else if (rand === '6') {
echo "
<div id="day-6">
<p>Show if Day 6 is randomly chosen</p>
</div>
";
}
else if (rand === '7') {
echo "
<div id="day-7">
<p>Show if Day 7 is randomly chosen</p>
</div>
";
}
else if (rand === '8') {
echo "
<div id="day-8">
<p>Show if Day 8 is randomly chosen</p>
</div>
";
}
else if (rand === '9') {
echo "
<div id="day-9">
<p>Show if Day 9 is randomly chosen</p>
</div>
";
}
else if (rand === '10') {
echo "
<div id="day-10">
<p>Show if Day 10 is randomly chosen</p>
</div>
";
}
?>
在评论中指向资源的指针或只回复一个随机DIV的答案将不胜感激,因为我不想把所有代码都推到页面上,也不想使用额外的JS和CSS,因为这会对加载时间产生太大的影响。
然后有一种使用shuffle的方法,但我仍然需要将所选的随机DIV写入一个文件,这样我就知道输出了什么,所以我要用数字或day-1
来识别它们吗?然后我如何知道将其保存到文本文件的选择,以及shuffle是否足够随机,数组是否可以,而不是echo和else语句。
<?php
$day_array = array(
'<div id="day-1">
<p>Show if Day 1 is randomly chosen</p>
</div>',
'<div id="day-2">
<p>Show if Day 2 is randomly chosen</p>
</div>',
'<div id="day-3">
<p>Show if Day 3 is randomly chosen</p>
</div>',
'<div id="day-4">
<p>Show if Day 4 is randomly chosen</p>
</div>',
'<div id="day-5">
<p>Show if Day 5 is randomly chosen</p>
</div>',
'<div id="day-6">
<p>Show if Day 6 is randomly chosen</p>
</div>',
'<div id="day-7">
<p>Show if Day 7 is randomly chosen</p>
</div>',
'<div id="day-8">
<p>Show if Day 8 is randomly chosen</p>
</div>',
'<div id="day-9">
<p>Show if Day 9 is randomly chosen</p>
</div>',
'<div id="day-10">
<p>Show if Day 10 is randomly chosen</p>
</div>',
);
shuffle($day_array);
for ($i = 1;$i < 2;$i++)
{
echo array_shift($day_array);
}
?>
提前谢谢你,我有一个主意,但不知道如何随机执行,也不知道洗牌是否可以接受。
由于内容是静态的,并且是手动维护的,我们可以通过3个步骤来实现解决方案:
-
创建内容数组
-
生成随机数并将其存储在中
-
使用随机数显示特定内容。
/* 1. Create a static content array */ $day_array = array( '<div id="day-1"> <p>Show if Day 1 is randomly chosen</p> </div>', '<div id="day-2"> <p>Show if Day 2 is randomly chosen</p> </div>', '<div id="day-3"> <p>Show if Day 3 is randomly chosen</p> </div>', '<div id="day-4"> <p>Show if Day 4 is randomly chosen</p> </div>', '<div id="day-5"> <p>Show if Day 5 is randomly chosen</p> </div>', '<div id="day-6"> <p>Show if Day 6 is randomly chosen</p> </div>', '<div id="day-7"> <p>Show if Day 7 is randomly chosen</p> </div>', '<div id="day-8"> <p>Show if Day 8 is randomly chosen</p> </div>', '<div id="day-9"> <p>Show if Day 9 is randomly chosen</p> </div>', '<div id="day-10"> <p>Show if Day 10 is randomly chosen</p> </div>' ); /* Generate Random number */ $min = 1; $max = count($day_array); $random = mt_rand($min,$max); echo "Random Number is=".$random; /* And Save random number to a file */ $file = fopen("./randomnumber.txt","a+"); fwrite($file,$random."n"); fclose($file); /* 3. Show randomly selected content */ // $random-1 because the array index starts from 0 // and we generated random starting from 1 echo $day_array[$random-1];
工作演示