croakingtoad Posted March 12, 2007 Share Posted March 12, 2007 Hello, I have written a basic loop- $row = mysql_num_rows($query); for ($i = 0; $i< $row; $i++) { $record = mysql_fetch_row($query); if ($record[0] !== "12" && $record[0] !== "20" && $record[0] !== "10") { echo "$record[1]" . " " . "$record[2]" . "<br />"; } } What I think I need is a loop within a loop because what I want to end up with is something like-- <div class="container"> <div class="associate"> <img src="**" width="147" height="220" /><br />div 1</div> <div class="associate"> <img src="**" width="147" height="220" /><br />div 2</div> <div class="associate"> <img src="**" width="147" height="220" /><br />div 3</div> <div class="associate"> <img src="**" width="147" height="220" /><br />div 4</div> </div> <div class="container"> <div class="associate"> <img src="**" width="147" height="220" /><br />div 1</div> <div class="associate"> <img src="**" width="147" height="220" /><br />div 2</div> <div class="associate"> <img src="**" width="147" height="220" /><br />div 3</div> <div class="associate"> <img src="**" width="147" height="220" /><br />div 4</div> </div> The details being pulled in from the database are marked with an asterisk above. I need the container div to only be echoed once every 4 iterations. Does that make sense? How would I take what I have and get that? Thanks!! Quote Link to comment Share on other sites More sharing options...
monk.e.boy Posted March 12, 2007 Share Posted March 12, 2007 you want to do something like: $row = mysql_num_rows($query); echo '<div class="container">'; for ($i = 0; $i< $row; $i++) { $record = mysql_fetch_row($query); if ($record[0] !== "12" && $record[0] !== "20" && $record[0] !== "10") { if( $new_div ) echo '</div><div class="container">'; echo "$record[1]" . " " . "$record[2]" . "<br />"; } } } echo '</div>'; But you will need to figure out the $new_div bit (call a function or something) monk.e.boy Quote Link to comment Share on other sites More sharing options...
croakingtoad Posted March 12, 2007 Author Share Posted March 12, 2007 That was kind of the part I was wanting help with Quote Link to comment Share on other sites More sharing options...
donynam Posted March 12, 2007 Share Posted March 12, 2007 <?php loop 20 times{ ?> <div class="associate"> <?php do add asterisk jobs(); ?> </div> <?php } ?> I think this is what you want. Quote Link to comment Share on other sites More sharing options...
croakingtoad Posted March 12, 2007 Author Share Posted March 12, 2007 Here is what I have come up with that is very close to working, I'm just having one problem- $row = mysql_num_rows($query); for ($i = 0; $i< $row; $i++) { $record = mysql_fetch_row($query); if ($record[0] !== "12" && $record[0] !== "20" && $record[0] !== "10") { if (is_int($i / 4)) { echo "<div class=\"container\" id=\"$i\">"; } echo "<div class=\"associate\"> <img src=\"\" width=\"147\" height=\"220\" /></div> "; //echo "$row $record[1]" . " " . "$record[2]" . "<br />"; } if (is_int($i / 4)) { echo "</div>"; } } In short what this should be doing is determining if the value of $i divided by 4 is an integer. If it is, then it should echo out my container div with it's closing tag. This seems to be working okay except that when it calculates iteration '4' it is returning false. The answer is 1 with no decimals so I'm not sure why it's failing that count. Here is the subsequent output I am getting- <div class="container" id="0"> <div class="associate"> <img src="" width="147" height="220" /></div> </div> (WHY IS THIS DIV HERE?) <div class="associate"> <img src="" width="147" height="220" /></div> <div class="associate"> <img src="" width="147" height="220" /></div> <div class="associate"> <img src="" width="147" height="220" /></div> </div> <div class="associate"> <img src="" width="147" height="220" /></div> <div class="associate"> <img src="" width="147" height="220" /></div> <div class="container" id="8"> <div class="associate"> <img src="" width="147" height="220" /></div> </div> (WHY IS THIS HERE?) <div class="associate"> <img src="" width="147" height="220" /></div> <div class="associate"> <img src="" width="147" height="220" /></div> <div class="associate"> <img src="" width="147" height="220" /></div> <div class="container" id="12"> <div class="associate"> <img src="" width="147" height="220" /></div> </div> (WHY IS THIS HERE?) <div class="associate"> ETC ETC... I have put in the container divs an id that is the value of $i. Thanks! Quote Link to comment Share on other sites More sharing options...
monk.e.boy Posted March 13, 2007 Share Posted March 13, 2007 try changing: if (is_int($i / 4)) { // to: if( ($i % 4) == 0 ) { } % is the mod command (it splits a number on the '.' : so 9.876 = 876, and 4%4 = 0 and 4%8 = 0) monk.e.boy Quote Link to comment 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.