munkee Posted June 28, 2007 Share Posted June 28, 2007 Alright, I've done some searching and am quite confused. Perhaps someone here can help me. I have an array which consists of some MySQL data. What I'm trying to do is output the arrays 10 at a time... so here is a dirty example of what I'm looking for. for each 10 results, do this: <begin tag> result1, 2, 3, 4, 5, 6, 7, 8, 9, 10 </end tag> <begin tag> results11, 12, 13, 14, 15, 16 </end tag> So for every "10 results", it needs to start a new set of tags. This will be in CSS div's. Thanks in advance - I really apprecaite it. Quote Link to comment https://forums.phpfreaks.com/topic/57615-solved-foreach-while-loops/ Share on other sites More sharing options...
per1os Posted June 28, 2007 Share Posted June 28, 2007 <?php echo "<begin tag>\n result"; for ($i=1;$i<21;$i++) { if ($i % 10 == 0) { echo "<close tag>\n<begin tag>\n result"; } echo $i . " "; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/57615-solved-foreach-while-loops/#findComment-285176 Share on other sites More sharing options...
munkee Posted June 28, 2007 Author Share Posted June 28, 2007 Thanks for pointing me in the right direction. I knew it had to be something like that - I've been playing with basically the same lines of code for the past day or so; this was pretty much spot on except for the amounts of which I changed from "$i<10" to "$i<11" <?php echo '<div>'; for ($i=1;$i<501;$i++) { if ($i % 11 == 0) { echo '</div>'; echo "\n"; echo '<div>'; } echo $i . " "; } echo '</div>'; ?> I appreciate it. Quote Link to comment https://forums.phpfreaks.com/topic/57615-solved-foreach-while-loops/#findComment-285219 Share on other sites More sharing options...
munkee Posted June 30, 2007 Author Share Posted June 30, 2007 Okay, I'm stuck again... I some what got it to work with my data, just stuck on how the looping is working. Thank you Frost for solving my initial issue! You were spot on; I just wasn't very descriptive as to what I was looking for. At first it was, until I started working with it more. Here is what I've come up with: <?php $result = mysql_query("SELECT * FROM $maintable WHERE $thedate = CURDATE() ORDER BY `Title` "); echo '<div class="areaTitle">This is just some random text!'; for ($i=1; $i < 31; $i++) { if ($i % 10 == 1) { $result_arr=mysql_fetch_assoc($result); echo "</div>"; echo '<div class="areaTitle">'; echo $result_arr['Title']; } echo $i . " "; } echo '</div>'; ?> What that spits out is: <div class="areaTitle"> mySQLResult#1 - 1,2,3,4,5,6,7,8,9,10 </div> <div class="areaTitle"> mySQLResult#2 - 11,12,13,14,15,16,17,18,19,20 </div> <div class="areaTitle"> mySQLResult#2 - 21,22,23,24,25,26,27,28,29,30 </div> //and so on... When really I'd like it to be: <div class="areaTitle"> mySQLResult#1 mySQLResult#2 mySQLResult#3 mySQLResult#4 mySQLResult#5 mySQLResult#6 mySQLResult#7 mySQLResult#8 mySQLResult#9 mySQLResult#10 </div> <div class="areaTitle"> mySQLResult#11 mySQLResult#12 mySQLResult#13 mySQLResult#14 mySQLResult#15 mySQLResult#16 mySQLResult#17 mySQLResult#18 mySQLResult#19 mySQLResult#20 </div> //and so on... If that makes sense, could someone please help? I've tried a number of combinations and am stuck. Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/57615-solved-foreach-while-loops/#findComment-286888 Share on other sites More sharing options...
corbin Posted June 30, 2007 Share Posted June 30, 2007 <?php $q = mysql_query("SELECT * FROM $maintable WHERE $thedate = CURDATE() ORDER BY `Title` "); $i = 0; echo '<div class="areaTitle">'; while($r = mysql_fetch_assoc($q)) { echo $r['Title']; if($i % 10 == 0) { echo '</div><div class="areaTitle">'; } } echo '</div>'; ?> Is that what you're looking for? If you need me to explain any of that, just ask ;p. Quote Link to comment https://forums.phpfreaks.com/topic/57615-solved-foreach-while-loops/#findComment-286891 Share on other sites More sharing options...
munkee Posted June 30, 2007 Author Share Posted June 30, 2007 No, I understand what your code says. However, what I'm looking for exactly is for my first 10 results to be in the tag. <div class="areaTitle"> result1 result2 result3 result4 result5 result6 result7 result8 result9 result10 </div> <div class="areaTitle"> result11 result12 result13 result14 result15 result16 result17 result18 result19 result20 </div> So each tag has to include up to 10 results at a time. If there are more then 10 results, start new tag and output up to 10 more results... so on and so forth. I've gotten it to display "1,2,3,4,5,6,7,8,9,10" up until whatever value I need it to stop at, and inside it's proper tags. I just haven't been able to output 10 results, then next ten, then next ten. argh! ??? ??? ??? Quote Link to comment https://forums.phpfreaks.com/topic/57615-solved-foreach-while-loops/#findComment-286896 Share on other sites More sharing options...
corbin Posted June 30, 2007 Share Posted June 30, 2007 Hehe... I forgot something.... Change if($i % 10 == 0) { echo '</div><div class="areaTitle">'; } To have $i++; under it, and it should do what you're wanting to do. ;p Quote Link to comment https://forums.phpfreaks.com/topic/57615-solved-foreach-while-loops/#findComment-286902 Share on other sites More sharing options...
munkee Posted June 30, 2007 Author Share Posted June 30, 2007 you lost me. Quote Link to comment https://forums.phpfreaks.com/topic/57615-solved-foreach-while-loops/#findComment-286904 Share on other sites More sharing options...
corbin Posted June 30, 2007 Share Posted June 30, 2007 <?php $q = mysql_query("SELECT * FROM $maintable WHERE $thedate = CURDATE() ORDER BY `Title` "); //run the query $i = 0; //set i to 0 echo '<div class="areaTitle">'; while($r = mysql_fetch_assoc($q)) { //while there are results and php is able to assign them to $r echo $r['Title']; if($i % 10 == 0) { //if the remainder of $i/10 is 0 echo '</div><div class="areaTitle">'; } $i++; //increment i } echo '</div>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/57615-solved-foreach-while-loops/#findComment-286907 Share on other sites More sharing options...
munkee Posted June 30, 2007 Author Share Posted June 30, 2007 Okay, I figured it was that... I just kept doing "echo $i++;" What I've come to find out, is that $i starting off with a value of "0" causes the output to not be accurate. I changed the value of $i to "1" and everything came out perfect. Thank you SO much for helping me with this. Quote Link to comment https://forums.phpfreaks.com/topic/57615-solved-foreach-while-loops/#findComment-286914 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.