...

View Full Version : Display 3 Random Links



fro0ty
11-09-2006, 01:21 PM
Hi everyone!

I have about 30 quotes and I want to display 3 of them at random. Can anyone help me with a PHP script that would be able to do this?

Thanks

mlseim
11-09-2006, 01:39 PM
You could use PHP.

The extension of your webpage would need to be .php (instead of .html).

Say you have a list of quotes like this (text file is called "bumper_sticker.dat")
http://www.catpin.com/bumper_sticker.dat

Then, within your webpage (with the .php extension), you would have
a script like this:



<?php
#Read Bumper Sticker File
$filename="bumper_sticker.dat";
$lines = array();
$fp = fopen($filename, "r");
$array1 = explode("\n", fread($fp, filesize($filename)));
fclose ($fp);

#Generate Random Numbers
srand(time());
$arr = array();
while ( count($arr) < count($array1) ) {
$x = mt_rand(1,count($array1));
if ( !in_array($x-1,$arr) ) { $arr[] = $x-1; }
}

#Pick out the 3 Random Quotes
$quote1 = $array1[$arr[0]];
$quote2 = $array1[$arr[1]];
$quote3 = $array1[$arr[2]];

echo "<span style='font-family: helvetica; font-size: 9pt; color: #000000;'>".$quote1."</span><br />";
echo "<span style='font-family: helvetica; font-size: 9pt; color: #000000;'>".$quote2."</span><br />";
echo "<span style='font-family: helvetica; font-size: 9pt; color: #000000;'>".$quote3."</span><br />";
?>



See my working test example:

http://www.catpin.com/bumper_sticker3.php


NOTE: you can make the script more compact, but I left it alone
so you can more easily see how it works.




.

fro0ty
11-09-2006, 03:17 PM
Wow thanks a lot!! that's exactly what I was looking for! :)

ps. added rep :p

Nicklas
11-10-2006, 12:55 AM
Or you just do it like this:

<?php

$quote = file("yourfile.txt");
shuffle($quote);

echo "$quote[0]<br />\n$quote[1]<br />\n$quote[2]";

?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum