RandomPic
Das gleiche Prinzip wie von RandomQuote läßt sich auch zur Anzeige von Bildern verwenden.Der Code von RandomPic
<?php
$pic[] = "scripting/pic-1.jpg";
$pic[] = "scripting/pic-2.jpg";
$pic[] = "scripting/pic-3.jpg";
srand ((double) microtime() * 1000000);
$random = rand(0,count($pic)-1);
echo "<p>" . "<img src='" . $pic[$random] . "' width='120' height='180'>" . "</p>";
?>
Und das Ergebnis:

RandomPic-Pro
Eleganter ist es gerade bei Bildern, wenn man nicht jedes Bild einzeln angibt, sondern als Quelle ein extra Verzeichnis benutzt wird.Der Vorteil dabei ist, daß man so leichert mal ein Bild austauschen oder hinzufügen kann, ohne daß man dafür den Code ändern müßte.
Der Code von RandomPic-Pro
<?
$quelle = "./scripting/pic-quelle";
$d = dir("./".$quelle);
while (false !== ($entry = $d->read())) {
if ($entry == ".") {continue;}
if ($entry == "..") {continue;}
if ( strpos($entry, ".jpg") > 0 ) { $returnarray[] = $entry;}
}
$d->close();
srand ((double) microtime() * 1000000);
$random = rand(0,count($returnarray)-1);
echo "<p>" . "<img src='" . $quelle . "/" . $returnarray[$random] . "' width='120' height='180'>" . "</p>";
?>
Und das Ergebnis:
