Jump to content

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.

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.