Warning: Cannot modify header information - headers already sent by (output started at /mnt/web7/41/25/5244025/htdocs/4/php-fotorotation.php:1) in /mnt/web7/41/25/5244025/htdocs/include/counter_01.php on line 95
PHP-Skripts - Zufälliger Bilderwechsel meiner Flickr-Fotos
Zum Inhalt springen.
Zum Seitenanfang springen.
Du befindest Dich genau hier: Home > Computer > Coding > PHP-Fotorotation.

PHP-Fotorotation:

Foto

Meine Flickr-Fotos, die hier sowohl links als auch rechts auf dieser Seite zu sehen sind, wechseln bei jedem neu laden der Seite. Welches Bild angezeigt wird, ist durch eine Zufallszahl bestimmt. Die Fotos werden direkt von Flickr geladen, dort gibt es unterschiedliche größen zur Auswahl. Das PHP-Script dafür sieht so aus:

<?php
// Erzeugt eine Zufallszahl und weist den Variablen $link und $foto entsprechend unterschiedliche Werte zu.

srand ((double)microtime()*20000);
$zufall = rand(1,3);
if($zufall == 1)
{
$link = "http://www.flickr.com/...";
$foto = "http://farm2.static.flickr.com/...";
}
elseif($zufall == 2)
{
$link = "http://www.flickr.com/...";
$foto = "http://farm2.static.flickr.com/...";
}
elseif($zufall == 3)
{
$link = "http://www.flickr.com/...";
$foto = "http://farm2.static.flickr.com/...";
}
else
{
$link = "http://www.flickr.com/...";
$foto = "http://farm2.static.flickr.com/...";
}
echo '<a target="_blank" href="',$link,'"title="Fotorotation"><img width="240" height="182" class="bild_im_text_rechts" align="right" src="',$foto,'" alt="Foto von Marvin Nassowitz" />';
?>

Alternativ würde das auch mit Switch gehen, was ein wenig übersichtlicher erscheint. Dies würde dann ungefähr so aussehen:

<?
switch ($auswahl = rand(1,3)) {
case $auswahl = 1:
$foto='...';
$link='...'; break;
case $auswahl = 2:
$foto='...';
$link='...'; break;
case $auswahl = 3:
$foto='...';
$link='...'; break;
default:
$foto='...';
$link='...';
}
?>