Php and javascript problem

Thank you.

Start menu

This link shows the images on the front page cycling.
If you go to Galleries, there is a gallery that calls the same code that should cycle. The only difference being that I have asked the gallery to display a description for each image.

This won’t be the only usage. I want to know how to do this so I can use the technique elsewhere.

Steve

It’s long-past when I should have beded down for the night, but the following should help.
Have you considered using arrays to store data, instead of hard-coded variable names.

For example:


var image_gallery = [];
image_gallery[0] = new Image();
image_gallery[0].src = "galleries/front_top/G1.JPG";
image_gallery[0].width = "262.5";
image_gallery[0].height = "350";
image_gallery[0].desc = "G1";
image_gallery[1] = new Image();
image_gallery[1].src = "galleries/front_top/G2.JPG";
image_gallery[1].width = "262.5";
image_gallery[1].height = "350";
image_gallery[1].desc = "G2";
image_gallery[2] = new Image();
image_gallery[2].src = "galleries/front_top/G3.JPG";
image_gallery[2].width = "262.5";
image_gallery[2].height = "350";
image_gallery[2].desc = "G3";
image_gallery[3] = new Image();
image_gallery[3].src = "galleries/front_top/G4.JPG";
image_gallery[3].width = "262.5";
image_gallery[3].height = "350";
image_gallery[3].desc = "G4";
image_gallery[4] = new Image();
image_gallery[4].src = "galleries/front_top/G5.JPG";
image_gallery[4].width = "262.5";
image_gallery[4].height = "350";
image_gallery[4].desc = "G5";
image_gallery[5] = new Image();
image_gallery[5].src = "galleries/front_top/G6.JPG";
image_gallery[5].width = "262.5";
image_gallery[5].height = "350";
image_gallery[5].desc = "G6";

followed with something like:


var image_step = 0;
var show_image_desc_gallery = "Y";

function slide_gallery() {
    if (!document.images) {
        return;
    }

    document.images.image_gallery.src = image_gallery[image_step].src;
    document.images.image_gallery.width = image_gallery[image_step].width;
    document.images.image_gallery.height = image_gallery[image_step].height;

    if (show_image_desc_gallery == "Y") {
        document.getElementById("p_id_gallery").innerHTML = image_gallery[image_step].desc;
    }

    image_step = (image_step + 1) % image_gallery.length;
    
    setTimeout(slide_gallery, 3000);
}

slide_gallery();

After that, you may want to consider replacing the hard-coded array values with a JSON array, so that you can just create the array in PHP and echo it straight out as a JavaScript array.

Yes I like the idea of this. I will try and implement it. It may take me a while.

Thanks,

Steve