簡體   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