PDA

View Full Version : ALT values


ramandeep
01-16-2006, 01:57 PM
Hi,
i am using this script to randomly display the images


$dir = 'title_images/images/';
$dh = opendir($dir);
/* The following loop scans the directory specified ignoring folders and Thumbs.db */
while (false !== ($filename = readdir($dh))) {
if($filename == "Thumbs.db" || is_dir($filename)){
}else{
$files[] = $filename;
}
}

/* Generate a random number */
$nooffildi = count($files);
$nooffiles = ($nooffildi-1);
srand((double)microtime()*1000000);
$randnum = rand(0,$nooffiles);

/* print the result */
echo "<IMG SRC='$dir/$files[$randnum]' ALT='$files[$randnum]' BORDER='0'>";

?>


It might be a silly question as i dont know much about PHP but I wonder how can we assign different alt values to different images.

davidj
01-16-2006, 02:45 PM
/* Generate a random number */
$nooffildi = count($files);
$nooffiles = ($nooffildi-1);
srand((double)microtime()*1000000);
$randnum = rand(0,$nooffiles);

add the below. I am doing a switch on $randnum then checking the string and assigning a string value to $alt var. You need to add a case as many times as you have images to specify an alt for a number

switch($randnum){
case '1' : $alt = 'this is an alt 1'; break;
case '2' : $alt = 'this is an alt 2; break;
case '3' : $alt = 'this is an alt 3; break;
case '4' : $alt = 'this is an alt 4; break;
case '5' : $alt = 'this is an alt 5; break;

}

$alt_tag = "<IMG SRC='$dir/$files[$randnum]' ALT='$alt' BORDER='0'>";

echo $alt_tag;

ramandeep
01-16-2006, 04:06 PM
Thanx DJ

imported_ove
01-16-2006, 08:04 PM
remember:
alt= alternative. Should be used for apps that dont display graphics
title = the little tooltip that show up. food for searchengines.

For example
<img src="logo.gif" alt="RDSNetworks Logo" title="RDSNetworks - Web Hosting and Web Design Services UK">