fife Posted April 12, 2012 Share Posted April 12, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/260806-php-to-loop-some-javascript/ Share on other sites More sharing options...
kicken Posted April 12, 2012 Share Posted April 12, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/260806-php-to-loop-some-javascript/#findComment-1336773 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.