简体   繁体   English

Echo 随机选择的 DIV

[英]Echo randomly selected DIV

I have no idea how to approach this and cannot find any resources for the same sort of thing so anyone to point in the correct direction would be appreciated.我不知道如何解决这个问题,也找不到任何资源来做同样的事情,所以任何人都指向正确的方向将不胜感激。

I was trying to generate a random number in PHP and then write this to a file here .我试图在 PHP 中生成一个随机数,然后将其写入此处的文件。 I'm not sure now if this is the best approach.我现在不确定这是否是最好的方法。 New question created now that I've done furthee research and changed the scope.我已经做了进一步的研究并改变了范围,现在创建了新问题。

I'm looking to show a random DIV that I need to be able to add a maximum number to.我希望显示一个随机 DIV,我需要能够向其中添加最大数字。 I don't know if to cycle between numbers or ID's but the general feel would be like this.我不知道是在数字还是 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>

I don't want every DIV loaded so I only want to echo the randomly chosen DIV and nothing else.我不想加载每个 DIV,所以我只想回显随机选择的 DIV 而没有别的。

I was using this to choose a random number and then write it我用它来选择一个随机数然后写出来

<?php
  $min=1;
  $max=100;
  echo rand($min,$max);
  $file = fopen("./randomnumber.txt","a+ \n");
   fwrite($file,$min,$max);
   fclose($file);
?>

But it writes a different number to the one output.但它会向一个输出写入一个不同的数字。 I need to store to a file each random number that was put out.我需要将输出的每个随机数存储到一个文件中。 I know this doesn't work but my current idea is to show each corresponding DIV based on the random number.我知道这不起作用,但我目前的想法是根据随机数显示每个相应的 DIV。 I'm not sure how else to echo just one random DIV so not everything has to load.我不知道如何只回显一个随机 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>
      
            ";
  }


?>

Pointers to resources in comments or an answer that would echo just one random DIV would be incredibly appreciated because I don't want to push all of the code to the page and have to use extra JS and CSS because it would too massively impact load times.指向评论中资源的指针或仅响应一个随机 DIV 的答案将非常受欢迎,因为我不想将所有代码推送到页面并且不得不使用额外的 JS 和 CSS,因为它会极大地影响加载时间.

There's then this approach using shuffle but I'd then still need to write the selected random DIV to a file too so I know what's been output so do I ID them all with numbers or day-1 , how would I then know the choice made to save this to a text file and is shuffle random enough and an array okay rather than echo and else statements.然后是使用 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);
}

?>

Thank you in advance, I have an idea but don't know how to execute randomly or if shuffle would be acceptable.在此先感谢您,我有一个想法,但不知道如何随机执行或 shuffle 是否可以接受。

Since the content is static and maintained manually, we can implement the solution in 3 steps:由于内容是静态的并且需要手动维护,我们可以分 3 步实现该解决方案:

  1. Create a content Array创建内容数组

  2. Generate Random Number and store it生成随机数并存储

  3. Use the random number to show specific content.使用随机数显示特定内容。

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

Working Demo工作演示

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

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