Jump to content

php to loop some javascript


fife

Recommended Posts

Hi.  I have some javascript that I need to loop a set number of times.  I have used a php query limited to 6.  I then need to loop the javascript I have the same amount of times.  However it is not looping.  It loops through once and stops.  Here is the code.

 

//the query
$Fetchactivityq = mysql_query("SELECT * FROM Activites WHERE clubid ='".$club['club']."' ORDER BY RAND() LIMIT 6 ") or die('activity error');

// then the images I want to loop further down the page

<?php 
$i = 1;
while($row = mysql_fetch_assoc($Fetchactivityq)) { ?>

<li id="capslide_img_cont<?php echo $i;?>" class="ic_container">
       	      <?php if ($row['Image']=='activityImage.jpg') { ?>
      <img src="/images/icons/activityImage.jpg" id="activityImage" /><?php } ?>
</li>

<?php $i++ } ?>

// then at the bottom of the page the problem bit

<script type="text/javascript">
$(function() {
<?php $i = 1; do { ?>
                $("#capslide_img_cont<?php echo $i; ?>").capslide({
                    caption_color	: 'white',
                    caption_bgcolor	: 'black',
                    overlay_bgcolor : 'black',
                    border			: '',
                    showcaption	    : false
                });

<?php $i++; } while($row1 = mysql_fetch_array($Fetchactivityq)); ?>
});
</script>

 

Thanks for your help

 

Danny

Link to comment
https://forums.phpfreaks.com/topic/260806-php-to-loop-some-javascript/
Share on other sites

You can only loop through a result-set once.  Your first while loop runs it to the end then your second at the bottom has nothing to do.  Using a do...while loop with a result set is not correct either as it will execute the loop body before fetching the row which is wrong.

 

After you do your query, load all the results into an array, then replace your two while loops you have now with a foreach() loop over that array.

 

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.