Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Bilder in Array

Empfohlene Antworten

Veröffentlicht

Hallo an alle!

Ich möchte mit php mein Hintergrundbild je nach Tageszeit ändern. Die bilder speicher ich dabei in einem Array ab und je nach Uhrzeit soll sich das Hintergrund anpassen. Nur leider bekomme ich immer einen weißen Hintergrund... Wo liegt mein Fehler?

Die Bilder liegen im Ordner "images"

Danke!


<?php
$time = date("H:i");
echo $time;
$background = array (
'07:00' => '<img src="images/bg.jpg">',
'11:30' => '<img src="images/bg_sun.jpg">',
'19:00' => '<img src="images/bg_night.jpg">');
?>

<body style="background: <?php echo $background;?> repeat-x;">
[/php]

Hui, da paßt einiges nicht.

Zunächst wird ein Hintergrundbild in CSS so definiert:

background-image: url(images/bg.jpg);
...also ohne das <img>-Tag rundrum. Dann gibst du mit
echo $background;
nicht ein einzelnes Array-Element, sondern einfach das ganze Array aus...schau dir mal das resultierende HTML an:
<body style="background: Array repeat-x;">

Und schlußendlich würde das Ganze auch nur dann funktionieren, wenn es exakt sieben Uhr, halb zwölf oder neunzehn Uhr ist. Da gehört also noch eine entsprechende Logik rein.

Bearbeitet von azett

Danke für deine Antwort! Ich habe jetzt noch eine entsprechende Logik eingebaut und auf url geändert. Und in echo gebe ich jetzt den entsprechenden hintergrund aus

Kannst du mir sagen, warum es trotzdem nicht funkioniert?


<?php
$date = date("h");
$background[0] = 'url(images/bg.jpg)';
$background[1] = 'url(images/bg_sun.jpg)';
$background[2] = 'url(images/bg_night.jpg)';

if($date > 6 AND $date <= 11 ) {
$index=0;
} elseif($date > 11 AND $date <= 19 ) {
$index=1;
} else {
$index=2;
}
?>

<body style="background:<?php echo ".$background[$index]."?> repeat-x;">
[/php]

hat sich erledigt! punkt vor und nach $background mus raus

Danke schön!

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.