Thursday, 15 May 2014

javascript - Using php variables for image location, use onclick to run through image gallery -



javascript - Using php variables for image location, use onclick to run through image gallery -

i'm making simple database query returns several images. these images stored in php variable, javascript gallery showing 1 image @ time , using onclick function run though images. not working, please help!

my database query returns images, each set variable. variables set array (because thought should easier run through array)

$img1 = $row['img1']; $img2 = $row['img2']; $img3 = $row['img3']; $images = array($img1,$img2,$img3);

on page gallery section shows first image fine

<div id="showimage"> <img src= " <?php echo "$images[0]" ?>" id="gallery"> <div id="rightholder"><img onclick="gallery(1)" src="images/arrow-right.png"></div> <div id="leftholder"><img onclick="gallery(-1)" src="images/arrow-left.png"></div> </div>

but javascript wont date array key $images onclick, sec image not loaded.

var imagecount = 1; var totalimage = 3; function gallery(x) { var image = document.getelementbyid('gallery'); imagecount = imagecount + x; if(imagecount > totalimage){imagecount = 1;} if(imagecount < 1){imagecount = totalimage;} image.src = "<?php echo $images["+ imagecount +"]?>"; }

you cant this: image.src = "<?php echo $images["+ imagecount +"]?>";

js runs in browser. js not have php data.

you seek in js file:

var images = "<?php echo implode(",", $images); ?>"; images = images.split(",");

with piece of js can phone call image doing:

image.src = images[imagecount];

what takes php array, makes string seperated commas. string gets split on comma in js array.

javascript php image

No comments:

Post a Comment