random page

  • tastysite
  • Proficient
  • Proficient
  • User avatar
  • Posts: 349
  • Loc: Brighouse, West Yorkshire, England

Post 3+ Months Ago

Hi I want to make a random page link on my site which will open a random page from a list of ones that can be picked and not the whole site how would one do this?
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

Here you go... an example of usage is put in the script.
PHP Code: [ Select ]
<?php
function get_rands($min, $max, $amount)
{
   $amount = (int) $amount;
   $rands = array();
   $rand = false;
 
   for($i = 0; $i < $amount; ++$i)
   {
      while(!$rand || in_array($rand, $rands))
      {
         $rand = rand($min, $max);
      }
   
      $rands[] = $rand;
   }
 
   return $rands;
}
 
function rand_page($amount = 1)
{
   $pages = array(
      'http://www.google.com' => 'Google',
      'http://www.ozzu.com' => 'Webmaster Resources',
      'http://www.wedevoy.com' => 'Web Development',
      'http://www.yahoo.com' => 'Yahoo',
      'http://www.gmail.com' => 'Google Mail',
      'http://www.ymail.com' => 'Yahoo Mail',
      'http://www.ismywebsite.com' => 'Free cPanel web hosting',
      'http://www.webfaction.com' => 'A good web host',
      'http://www.phpbb.com' => 'phpBB Home Page',
      $_SERVER['PHP_SELF'] => 'ME'
   );
   
   $i = 0;
   foreach($pages as $link => $title)
   {
      $page[$i]['LINK'] = $link;
      $page[$i]['TITLE'] = $title;
      ++$i;
   }
   
   $ids = get_rands(0, $i-1, $amount);
   
   foreach($ids as $id)
   {
      $html .= "<a href=\"{$page[$id]['LINK']}\">{$page[$id]['TITLE']}</a><br />\n";
   }
   
   return $html;
}
   // Test
   echo rand_page(4);
?>
  1. <?php
  2. function get_rands($min, $max, $amount)
  3. {
  4.    $amount = (int) $amount;
  5.    $rands = array();
  6.    $rand = false;
  7.  
  8.    for($i = 0; $i < $amount; ++$i)
  9.    {
  10.       while(!$rand || in_array($rand, $rands))
  11.       {
  12.          $rand = rand($min, $max);
  13.       }
  14.    
  15.       $rands[] = $rand;
  16.    }
  17.  
  18.    return $rands;
  19. }
  20.  
  21. function rand_page($amount = 1)
  22. {
  23.    $pages = array(
  24.       'http://www.google.com' => 'Google',
  25.       'http://www.ozzu.com' => 'Webmaster Resources',
  26.       'http://www.wedevoy.com' => 'Web Development',
  27.       'http://www.yahoo.com' => 'Yahoo',
  28.       'http://www.gmail.com' => 'Google Mail',
  29.       'http://www.ymail.com' => 'Yahoo Mail',
  30.       'http://www.ismywebsite.com' => 'Free cPanel web hosting',
  31.       'http://www.webfaction.com' => 'A good web host',
  32.       'http://www.phpbb.com' => 'phpBB Home Page',
  33.       $_SERVER['PHP_SELF'] => 'ME'
  34.    );
  35.    
  36.    $i = 0;
  37.    foreach($pages as $link => $title)
  38.    {
  39.       $page[$i]['LINK'] = $link;
  40.       $page[$i]['TITLE'] = $title;
  41.       ++$i;
  42.    }
  43.    
  44.    $ids = get_rands(0, $i-1, $amount);
  45.    
  46.    foreach($ids as $id)
  47.    {
  48.       $html .= "<a href=\"{$page[$id]['LINK']}\">{$page[$id]['TITLE']}</a><br />\n";
  49.    }
  50.    
  51.    return $html;
  52. }
  53.    // Test
  54.    echo rand_page(4);
  55. ?>

Basically, what that '4' does in the Test part of the script is tell how many links it would have. To format the HTML, all you have to do is go to the following line:
Code: [ Select ]
     $html .= "<a href=\"{$page[$id]['LINK']}\">{$page[$id]['TITLE']}</a><br />\n";

and manipulate the HTML (Leave the PHP along! :) ) and there you have it.

This is actually a quick re-write of the script I wrote for mista-two-k

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 72 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.