【问题标题】:Echo randomly selected DIV回显随机选择的 DIV
【发布时间】:2021-09-09 13:05:03
【问题描述】:

我不知道如何解决这个问题,也找不到任何资源用于相同的事情,所以任何人都可以指出正确的方向。

我试图在 PHP 中生成一个随机数,然后将其写入文件 here。我现在不确定这是否是最好的方法。现在我已经完成了进一步的研究并更改了范围,因此创建了新问题。

我正在寻找一个随机的 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>
      
            ";
  }


?>

在 cmets 中指向资源的指针或仅回显一个随机 DIV 的答案将非常感谢,因为我不想将所有代码推送到页面并且必须使用额外的 JS 和 CSS,因为它会过于庞大影响加载时间。

然后有这种使用随机播放的方法,但我仍然需要将选定的随机 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);
}

?>

提前谢谢你,我有一个想法,但不知道如何随机执行或随机执行是否可以接受。

【问题讨论】:

  • 我的问题的最后一部分使用了一个数组,然后我的问题是将显示的内容保存到文本文件中。我最初的方法是保存随机数,然后基于它回显 DIV,但保存的随机数与输出 @Remy 的不同
  • 这些 DIV 中的内容是如何生成的?
  • 你有什么问题@Samir?如果这是您所要求的,则每次有新内容可用时,都会手动添加另一个 DIV。我不知道是否要选择一个随机数字,保存数字然后基于该数字输出,或者是否使用数组,但是我不知道如果不使用 echo 和基础,我将如何保存显示的 DIV ID随机选择。
  • 您需要选择一个随机数并全天使用,即用户可以一整天看到一个随机选择的内容。第二天,您需要再次选择一个新的随机内容并显示它。这是你打算做的吗?
  • 在每个页面加载时都会显示新的一天。因此,如果您连续 365 天每天拍摄一张图像,则会有 365 个 DIV,并且每当重新加载页面时,都会显示不同的 DIV。这就是为什么我尝试根据随机数回显。我的问题末尾的数组工作正常,但我不知道如何将 DIV ID 保存到文本文件中,所以我可以参考每个 DIV 被选中的次数。该数组在没有随机的情况下工作,但回显似乎更容易根据已经保存的随机数回显@Samir

标签: php


【解决方案1】:

由于内容是静态的并且是手动维护的,我们可以分三步实施解决方案:

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

Working Demo

【讨论】:

  • 非常感谢您成为 SO 上更有耐心的人之一,非常感谢您的帮助。
猜你喜欢
  • 2020-07-08
  • 2014-08-27
  • 1970-01-01
  • 1970-01-01
  • 2011-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-13
相关资源
最近更新 更多