Simple PHP randomizer


/ Published in: PHP
Save to your folder(s)

Visit the link for instructions on how to use it.


Copy this code and paste it in your HTML
  1. <?php
  2. /*
  3. Simple randomizer.
  4. Roger Johansson, http://www.456bereastreet.com/
  5. */
  6. echo '<ul>' . "\n";
  7. $items = array(
  8. 1 => array(
  9. 'href' => 'url-1',
  10. 'title' => 'title-text-1',
  11. 'src' => 'image-1.jpg',
  12. 'alt' => 'alt-text-1'
  13. ),
  14. 2 => array(
  15. 'href' => 'url-2',
  16. 'title' => 'title-text-2',
  17. 'src' => 'image-2.jpg',
  18. 'alt' => 'alt-text-2'
  19. ),
  20. 3 => array(
  21. 'href' => 'url-3',
  22. 'title' => 'title-text-3',
  23. 'src' => 'image-3.jpg',
  24. 'alt' => 'alt-text-3'
  25. ),
  26. 4 => array(
  27. 'href' => 'url-4',
  28. 'title' => 'title-text-4',
  29. 'src' => 'image-4.jpg',
  30. 'alt' => 'alt-text-4'
  31. ),
  32. 5 => array(
  33. 'href' => 'url-5',
  34. 'title' => 'title-text-5',
  35. 'src' => 'image-5.jpg',
  36. 'alt' => 'alt-text-5'
  37. )
  38. );
  39. $numberOfItems = 3; // Change to the number of items you want
  40. $randItems = array_rand($arrItems, $numberOfItems);
  41. for ($i = 0; $i < $numberOfItems; $i++) {
  42. $item = $arrItems[$randItems[$i]];
  43. echo "\t" . '<li class="r' . ($i + 1) . '"><a href="' . $item['href'] . '" title="' . $item['title'] . '"><img src="' . $item['src'] . '" alt="' . $item['alt'] . '"></a></li>' . "\n";
  44. }
  45. echo '</ul>' . "\n";
  46. ?>

URL: http://www.456bereastreet.com/archive/200701/simple_php_randomizer/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.