random link gen.

Soldato
Joined
6 Jan 2006
Posts
4,663
Location
Newcastle
Ive got a few website suggestions on my site, in a list format contained in a column on the right.


Q:how do i make it random (from a list) so that only 5 show out of a list of 10 ???
 
Something like (if using MySQL):

Code:
SELECT suggestion_title FROM suggestions
ORDER BY [B]RAND()[/B]
LIMIT 5

Could be select SELCT TOP 5 too, can't remember :)
 
Your random image question from before :)

PHP:
<?php 
 
/* 
 * Name your images 1.jpg, 2.jpg etc. 
 * 
 * Add this line to your page where you want the images to  
 * appear: <?php include "randomimage.php"; ?> 
 */  
 
// Change this to the total number of images in the folder 
$total = "11"; 
 
// Change to the type of files to use eg. .jpg or .gif 
$file_type = ".jpg"; 
 
// Change to the location of the folder containing the images 
$image_folder = "images/random"; 
 
// You do not need to edit below this line 
 
$start = "1"; 
 
$random = mt_rand($start, $total); 
 
$image_name = $random . $file_type; 
 
echo "<img src=\"$image_folder/$image_name\" alt=\"$image_name\" />"; 
 
?>
 
Last edited:
And for your question above :)

PHP:
<?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); 
} 
?>
 
Back
Top Bottom