Jump to content

foreach loop around JS


amwd07

Recommended Posts

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

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

);

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 ,

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.