amwd07 Posted July 27, 2008 Share Posted July 27, 2008 The below works but only displays 1 image so there must be something wrong with using for each around the JS code ??? <?php /*** create DB sql query to prepare for the array ***/ $sql = db::getInstance()->ExecuteS('SELECT id_image, id_product FROM ps_image GROUP BY id_product'); /*** Start looping rows in mysql database. ***/ foreach ($sql as $row) { $imageID = $row['id_image']; $productID = $row['id_product']; $img = "img/p/".$productID."-".$imageID."-home.jpg"; print "product ID = $productID image ID = $imageID </br>"; $request_url = $_SERVER['REQUEST_URI']; ?> <SCRIPT type="text/javascript"> var img = "<?php echo $img ?>"; var url = "<?php echo $request_url ?>product.php?id_product=<?php echo $productID ?>"; Car_Image_Sources = new Array(img, url); </script> <?php } /*** end foreach loop ***/ ?> Link to comment https://forums.phpfreaks.com/topic/116894-foreach-loop-around-js/ Share on other sites More sharing options...
br0ken Posted July 27, 2008 Share Posted July 27, 2008 Are you sure the SQL query returns more than image? I would check that because I can't see any other obvious errors. Link to comment https://forums.phpfreaks.com/topic/116894-foreach-loop-around-js/#findComment-601153 Share on other sites More sharing options...
amwd07 Posted July 27, 2008 Author Share Posted July 27, 2008 yes the query returns 7 rows http://www.amwdesign.co.uk/demo7/ notice only one shows ??? Link to comment https://forums.phpfreaks.com/topic/116894-foreach-loop-around-js/#findComment-601175 Share on other sites More sharing options...
DarkWater Posted July 27, 2008 Share Posted July 27, 2008 Umm, are you using the Javascript to display the images? Because I think you're overwriting the Javascript variables on each iteration of the foreach. Link to comment https://forums.phpfreaks.com/topic/116894-foreach-loop-around-js/#findComment-601179 Share on other sites More sharing options...
amwd07 Posted July 27, 2008 Author Share Posted July 27, 2008 I have to use JS because the carousel script needs to use this array ? http://www.dynamicdrive.com/dynamicindex14/carousel.htm is there a way I can print the JS array? Link to comment https://forums.phpfreaks.com/topic/116894-foreach-loop-around-js/#findComment-601184 Share on other sites More sharing options...
amwd07 Posted July 28, 2008 Author Share Posted July 28, 2008 What I have noticed in the dynamic drive example there is no comma to seperate the values if i put a comma in the loop this will appear for the last item which will kill the script ??? Car_Image_Sources=new Array( "photo1.jpg","http://www.dynamicdrive.com", "photo2.jpg","http://www.javascriptkit.com", "photo3.jpg","", //this slide isn't linked "photo4.jpg","http://www.codingforums.com" // NOTE No comma after last line ); Link to comment https://forums.phpfreaks.com/topic/116894-foreach-loop-around-js/#findComment-601187 Share on other sites More sharing options...
amwd07 Posted July 28, 2008 Author Share Posted July 28, 2008 I have now changed my JS to the below <SCRIPT type="text/javascript"> var img = "<?php echo $img ?>"; var url = " <?php echo $request_url ?>product.php?id_product=<?php echo $productID ?> ,"; Car_Image_Sources = new Array(img, url); document.write(Car_Image_Sources); </script> which prints the below all I think I need to do is replace the last comma with a semi colon can anyone help wityh that ??? img/p/1-1-home.jpg, /demo7/product.php?id_product=1 , img/p/2-5-home.jpg, /demo7/product.php?id_product=2 , img/p/5-15-home.jpg, /demo7/product.php?id_product=5 , img/p/6-18-home.jpg, /demo7/product.php?id_product=6 , img/p/7-24-home.jpg, /demo7/product.php?id_product=7 , img/p/8-33-home.jpg, /demo7/product.php?id_product=8 , img/p/9-36-home.jpg, /demo7/product.php?id_product=9 , Link to comment https://forums.phpfreaks.com/topic/116894-foreach-loop-around-js/#findComment-601192 Share on other sites More sharing options...
amwd07 Posted July 28, 2008 Author Share Posted July 28, 2008 Right final change just 1 comma to take off the last row of the array ??? <SCRIPT type="text/javascript"> var img = "<?php echo $img ?>"; var url = "<?php echo $request_url ?>product.php?id_product=<?php echo $productID ?>"; Car_Image_Sources = new Array(img, url,""); document.write(Car_Image_Sources); </script> img/p/1-1-home.jpg,/demo7/product.php?id_product=1, img/p/2-5-home.jpg,/demo7/product.php?id_product=2, img/p/5-15-home.jpg,/demo7/product.php?id_product=5, img/p/6-18-home.jpg,/demo7/product.php?id_product=6, img/p/7-24-home.jpg,/demo7/product.php?id_product=7, img/p/8-33-home.jpg,/demo7/product.php?id_product=8, img/p/9-36-home.jpg,/demo7/product.php?id_product=9, the last comma needs removing for this script to work Link to comment https://forums.phpfreaks.com/topic/116894-foreach-loop-around-js/#findComment-601203 Share on other sites More sharing options...
amwd07 Posted July 28, 2008 Author Share Posted July 28, 2008 <SCRIPT type="text/javascript"> var img = "<?php echo $img ?>"; var url = "<?php echo $request_url ?>product.php?id_product=<?php echo $productID ?>"; var car=new Array(img, url); car=split(','); document.write(car); </script> I can't get split to work ??? Link to comment https://forums.phpfreaks.com/topic/116894-foreach-loop-around-js/#findComment-601219 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.