Forum Moderators: coopster
<?php
$filename = $_SERVER["DOCUMENT_ROOT"].'/inc/pub/pub_array.inc';
if ($fileContents = file_get_contents($filename)) {
$contentsArray = explode("\n", $fileContents);
$numLines = count($contentsArray)-1;
print($contentsArray[rand(0,$numLines)] . "\n"); // 1
print($contentsArray[rand(0,$numLines)] . "\n"); // 2
print($contentsArray[rand(0,$numLines)] . "\n"); // 3
print($contentsArray[rand(0,$numLines)] . "\n"); // 4
}
else {
die('Could not get contents of: ' . $filename);
}
?>
This provides the randomness, but there are repeating entries - in other words, some lines from the text file are shown twice or more. I've looked at the PHP function
array_rand, but I don't know whether it would do the trick, or how to adapt it... Anyone have any ideas?
Many thanks for your help!
Added: I'm using PHP 4.3.4
<?php
$filename = $_SERVER["DOCUMENT_ROOT"].'/inc/pub/pub_array.inc';
$how_many_to_show = 4;
if ($fileContents = file($filename)) {
shuffle($fileContents);
for ($i = 0; $i < $how_many_to_show; $i++) {
print($fileContents[$i]. '<br />');
}
} else {
die('Could not get contents of: ' . $filename);
}
?>