Skip to main content
Known Participant
January 9, 2017
Answered

Dynamic image change when page loads

  • January 9, 2017
  • 1 reply
  • 368 views

Hi All,

I can't figure this forum system out so please bear with me if I am in the wrong discussion area and inform me of the correct area.

I am using CS6 on Win10. I am trying to change background header images every time the user revisits the page. I would prefer html5 or php, not js, as there are user choices and restrictions with js. Is there a tutorial, an extension, or a program that provides this?

Thank you.

Gary

p.s. It is odd that in a DW forum that php and js are noted as being misspelled. It got html5 though.

    This topic has been closed for replies.
    Correct answer osgood_

    One way is to put your images into a php array:

    <?php

    // array background images

    $bg_image = array('bg-01.jpg', 'bg-02.jpg', 'bg-03.jpg', 'bg-04.jpg', 'bg-05.jpg', 'bg-06.jpg');

    // create random number

    $i = rand(0, count($bg_image)-1);

    // Store random image no. in a variable

    $selected_image = "$bg_image[$i]";

    ?>

    Then add some css to your page:

    <style>

    /* SET RANDOM BACKGROUND IMAGE USING PHP */

    .main-mage {

    background-image: url(background_images/<?php echo $selected_image; ?>);

    height: 500px;

    }

    </style>

    The above css link stores the background images in a folder named 'background_images' and in that folder you have the files named - bg-01.jpg, bg-02.jpg, bg-03.jpg etc etc

    So this line in the css:

    background-image: url(background_images/<?php echo $selected_image; ?>);

    when the php is processed will become:

    background-image: url(background_images/bg-02.jpg);  - or whatever the random image name is

    Include the html where your random background image is shown

    <figure class="main-image">

    </figure>

    <!-- end main-image -->

    1 reply

    osgood_Correct answer
    Legend
    January 9, 2017

    One way is to put your images into a php array:

    <?php

    // array background images

    $bg_image = array('bg-01.jpg', 'bg-02.jpg', 'bg-03.jpg', 'bg-04.jpg', 'bg-05.jpg', 'bg-06.jpg');

    // create random number

    $i = rand(0, count($bg_image)-1);

    // Store random image no. in a variable

    $selected_image = "$bg_image[$i]";

    ?>

    Then add some css to your page:

    <style>

    /* SET RANDOM BACKGROUND IMAGE USING PHP */

    .main-mage {

    background-image: url(background_images/<?php echo $selected_image; ?>);

    height: 500px;

    }

    </style>

    The above css link stores the background images in a folder named 'background_images' and in that folder you have the files named - bg-01.jpg, bg-02.jpg, bg-03.jpg etc etc

    So this line in the css:

    background-image: url(background_images/<?php echo $selected_image; ?>);

    when the php is processed will become:

    background-image: url(background_images/bg-02.jpg);  - or whatever the random image name is

    Include the html where your random background image is shown

    <figure class="main-image">

    </figure>

    <!-- end main-image -->

    landt5Author
    Known Participant
    January 9, 2017

    Thank you. I will try this.

    Gary