繁体   English   中英

Echo 随机选择的 DIV

[英]Echo randomly selected DIV

我不知道如何解决这个问题,也找不到任何资源来做同样的事情,所以任何人都指向正确的方向将不胜感激。

我试图在 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标识它们,然后我怎么知道所做的选择将它保存到一个文本文件中,并且足够随机并且一个数组可以,而不是 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);
}

?>

在此先感谢您,我有一个想法,但不知道如何随机执行或 shuffle 是否可以接受。

由于内容是静态的并且需要手动维护,我们可以分 3 步实现该解决方案:

  1. 创建内容数组

  2. 生成随机数并存储

  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];

工作演示

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM