Advertisement
Guest User

Untitled

a guest
Mar 6th, 2015
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.56 KB | None | 0 0
  1. <?php
  2.     function getRandomNumbers()
  3.     {
  4.         // We start with an empty array and add numbers until we hit 12.
  5.         $result = array();
  6.    
  7.         // We choose either 3 or 4 as basic number.
  8.         $x = mt_rand(3, 4);
  9.    
  10.         // Now, as long as we don't hit 12, we iterate through a loop,
  11.         // adding either the basic number, or 2 times the basic number:
  12.         while (array_sum($result) < 12) {
  13.    
  14.             // Randomly decide
  15.             if (mt_rand(0, 1) > 0) {
  16.                 $newElement = 2 * $x; // This is either 6 or 8
  17.    
  18.                 // However, always make sure not to exceed 12:
  19.                 if (array_sum($result) + $newElement > 12) {
  20.                     $newElement = $x;
  21.                 }
  22.    
  23.             } else {
  24.                 $newElement = $x; // This is either 3 or 4
  25.             }
  26.    
  27.             // Add the new number to the array:
  28.             $result[] = $newElement;
  29.         }
  30.    
  31.         // Return the resulting array
  32.         return $result;
  33.     }
  34.  
  35.     // BEGIN LOOP
  36.     if(have_posts()) :
  37.        
  38.         // Get the initial array with random numbers
  39.         $randomArray = getRandomNumbers();
  40.  
  41.         // Open the initial row.
  42.         echo '<div>';
  43.        
  44.         // CHECK FOR POSTS
  45.         while (have_posts()) : the_post();
  46.        
  47.             // CREATE ROWS
  48.            
  49.             if (count($randomArray) < 1) {
  50.                 // Close the row and start a new one:
  51.                 echo '</div><div>';
  52.                 // Get a fresh array with random numbers:
  53.                 $randomArray = getRandomNumbers();
  54.             }
  55.            
  56.             $nextRandomNumber = array_pop($randomArray); // This takes the next number.
  57.            
  58.             echo '<div class="col-md-' . $nextRandomNumber . '">';
  59.             the_content();
  60.             echo '</div>';
  61.                
  62.         // END
  63.         endwhile;
  64.    
  65.         // Close the final row.
  66.         echo '</div>';
  67.    
  68.     // CLOSE LOOP
  69.     endif;
  70. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement