9three Posted June 24, 2009 Share Posted June 24, 2009 Hey, I'm trying to display 7 columns and then stack the rest under each other so that it doesn't run off the page. echo '<table width="400" border="0" cellspacing="0" cellpadding="0">'; echo '<tr><td>'.CLOSE_OUT_MESSAGE.'</td></tr>'; echo '</table>'; echo '<table width="800" border="0" cellspacing="0" cellpadding="0">'; while($row = db_fetch_array($letterQuery)) { $tempLetter = explode(" ", $row['products_description']); if(!in_array($tempLetter[0], $used)) { $used[] = $tempLetter[0]; echo '<tr>'; for ($i = 0; $i < 7; $i++) echo '<td><a href="'.link_url(PAGENAME_FEATURES, 'featI4yrDauy=4&letter=' . $tempLetter[0]).'" >'.$tempLetter[0].'</a></td>'; echo '</tr>'; } } echo '</table>'; I've tried using a for loop to hit 7 and go down, but it repeats EVERYTHING 7 times. Help please? Quote Link to comment https://forums.phpfreaks.com/topic/163561-solved-displaying-certain-amount-of-columns/ Share on other sites More sharing options...
gizmola Posted June 24, 2009 Share Posted June 24, 2009 echo '</pre> <table width="400" border="0" cellspacing="0" cellpadding="0">'; echo ''.CLOSE_OUT_MESSAGE.''; echo '</table>';<br>echo '<table width="800" border="0" cellspacing="0" cellpadding="0">'; $rowcnt = 0; while($row = db_fetch_array($letterQuery)) { $tempLetter = explode(" ", $row['products_description']); if(!in_array($tempLetter[0], $used)) { $rowcnt++; $used[] = $tempLetter[0]; if ($rowcnt echo ''; echo ''.$tempLetter[0].''; echo ''; } else { // Do your stacking stuff here } } } echo '</table> Quote Link to comment https://forums.phpfreaks.com/topic/163561-solved-displaying-certain-amount-of-columns/#findComment-862961 Share on other sites More sharing options...
9three Posted June 25, 2009 Author Share Posted June 25, 2009 hmm.. That seems to do some-what of what I'm looking for. It displays the first 7, which is fine, but I if, for example, I have 14 in total, the next 7 would need to display under that. I've tried a couple of different solution but I haven't gotten the solution I'm looking for :-\ Quote Link to comment https://forums.phpfreaks.com/topic/163561-solved-displaying-certain-amount-of-columns/#findComment-863052 Share on other sites More sharing options...
.josh Posted June 25, 2009 Share Posted June 25, 2009 echo '<table width="400" border="0" cellspacing="0" cellpadding="0">'; echo '<tr><td>'.CLOSE_OUT_MESSAGE.'</td></tr>'; echo '</table>'; echo '<table width="800" border="0" cellspacing="0" cellpadding="0"><tr>'; $rowcnt = 0; while($row = db_fetch_array($letterQuery)) { $tempLetter = explode(" ", $row['products_description']); if(!in_array($tempLetter[0], $used)) { $rowcnt++; $used[] = $tempLetter[0]; echo '<td><a href="'.link_url(PAGENAME_FEATURES, 'featI4yrDauy=4&letter=' . $tempLetter[0]).'" >'.$tempLetter[0].'</a></td>'; if ($rowcnt%7==0) echo "</tr><tr>"; } } echo '</tr></table>'; Quote Link to comment https://forums.phpfreaks.com/topic/163561-solved-displaying-certain-amount-of-columns/#findComment-863064 Share on other sites More sharing options...
9three Posted June 25, 2009 Author Share Posted June 25, 2009 That works perfect. But I'm confused by this concept, maybe I'm over complicating it (like always). If the remainder is '0' close the table row and open a new table row. Once you open the new table row, theres no value/variable within the new table row.... This is where I get confused. Quote Link to comment https://forums.phpfreaks.com/topic/163561-solved-displaying-certain-amount-of-columns/#findComment-863070 Share on other sites More sharing options...
.josh Posted June 25, 2009 Share Posted June 25, 2009 I don't understand what you're asking... somewhere earlier in your script you query your database for info. Then you have this loop that goes through each row returned from your query. So you want those rows to be displayed in an html table 7 columns wide and however many rows tall that works out to, right? Well the normal structure of an html table is: <table> <tr> <td></td> </tr> </table> So before your loop, you echo out the opening table tag and the first opening row tag. Then for your loop, for each iteration, you just echo the data out with td tags wrapped around it. So you want to make a new row every 7 times, so you have a variable incrementing every iteration. Use a modulus condition to see if we're on a 7th iteration. If we are, close out the row and open a new one. After the loop ends, put the final row's closing tag and the table's closing tag. Was there something in there you don't understand, or am I not understanding your problem/goal? Quote Link to comment https://forums.phpfreaks.com/topic/163561-solved-displaying-certain-amount-of-columns/#findComment-863074 Share on other sites More sharing options...
9three Posted June 25, 2009 Author Share Posted June 25, 2009 You answered it correct, thanks. Quote Link to comment https://forums.phpfreaks.com/topic/163561-solved-displaying-certain-amount-of-columns/#findComment-863542 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.