简体   繁体   中英

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 . 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. I don't know if to cycle between numbers or ID's but the general feel would be like this.

 <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.

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. I'm not sure how else to echo just one random DIV so not everything has to load.

<?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.

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.

<?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.

Since the content is static and maintained manually, we can implement the solution in 3 steps:

  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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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