speedy33417 Posted November 19, 2006 Share Posted November 19, 2006 I'm having a little trouble with my for statements. My goal would be to display customer names in columns. Assuming I have 18 names to display in 5 columns, I display 4 names in the first 4 columns and the remaining 3 names in column 5.For some reason I'm getting a different result and all of my names are in one column, but my echoed html code is very weird!Here's the php code:[code]<?php$columns = 5;$columnsMinusOne = $columns - 1;$columnMax = ceil($total/$columns);?>blah blah<div align="center"> <table width="740" border="0" cellpadding="0" cellspacing="0"> <tr> <?php for ($j = 0; $j < $columns; $j++) { echo "<td valign=\"top\" align=\"center\"><span class=\"text1\">"; if ($j == $columnMinusOne) $columnMax = $total - ($columnMax * $columnMinusOne); for ($i = 0; $i < $columnMax; $i++) { $counter = ($j * $columnMax) + $i; echo $cust_firstname[$counter] . " " . $cust_lastname[$counter] . "<br>"; } echo "</span></td>"; } ?> </tr> </table></div>[/code]$column is the number of columns$columnMax is the number of names to be displayed in a column$counter is calculated on the fly and should go from 0 to 17.Any thoughts? Link to comment https://forums.phpfreaks.com/topic/27799-for-statement-help/ Share on other sites More sharing options...
glenelkins Posted November 19, 2006 Share Posted November 19, 2006 need to see more code , at the moment im seeing firstly that $total has no value. Link to comment https://forums.phpfreaks.com/topic/27799-for-statement-help/#findComment-127224 Share on other sites More sharing options...
speedy33417 Posted November 19, 2006 Author Share Posted November 19, 2006 $total is the number of names pulled from the database. In our case it's 18. Sorry. Link to comment https://forums.phpfreaks.com/topic/27799-for-statement-help/#findComment-127235 Share on other sites More sharing options...
speedy33417 Posted November 19, 2006 Author Share Posted November 19, 2006 By the way this is the html code that I'm trying to achieve:[code]<div align="center"> <table width="740" border="0" cellpadding="0" cellspacing="0"> <tr> <td valign="top" align="center"><span class="text1"> Name 1<br> Name 2<br> Name 3<br> Name 4<br> Name 5<br> </span></td> <td valign="top" align="center"><span class="text1"> Name 6<br> Name 7<br> Name 8<br> Name 9<br> Name 10<br> </span></td> <td valign="top" align="center"><span class="text1"> Name 11<br> Name 12<br> Name 13<br> Name 14<br> Name 15<br> </span></td> <td valign="top" align="center"><span class="text1"> Name 16<br> Name 17<br> Name 18<br> </span></td> </tr> </table> </div> [/code]And for some reason I'm getting this:[code]<div align="center"> <table width="740" border="0" cellpadding="0" cellspacing="0"> <tr> <td valign="top" align="center"><span class="text1">Name 1<br>Name 2<br>Name 3<br>Name 4<br>Name 5<br>Name 6<br>Name 7<br>Name 8<br>Name 9<br>Name 10<br>Name 11<br>Name 12<br>Name 13<br>Name 14<br>Name 15<br>Name 16<br>Name 17<br>Name 18<br>Name 19<br>Name 20<br></span></td><td valign="top" align="center"><span class="text1"> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br></span></td><td valign="top" align="center"><span class="text1"> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br></span></td><td valign="top" align="center"><span class="text1"> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br></span></td><td valign="top" align="center"><span class="text1"> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br></span></td> </tr> </table> </div>[/code]Sorry. No line breaks... Link to comment https://forums.phpfreaks.com/topic/27799-for-statement-help/#findComment-127241 Share on other sites More sharing options...
glenelkins Posted November 19, 2006 Share Posted November 19, 2006 before we go further why do it this way??:[code]<div align="center"> <table width="740" border="0" cellpadding="0" cellspacing="0"> <tr> <td valign="top" align="center"><span class="text1"> Name 1<br> Name 2<br> Name 3<br> Name 4<br> Name 5<br> </span></td> <td valign="top" align="center"><span class="text1"> Name 6<br> Name 7<br> Name 8<br> Name 9<br> Name 10<br> </span></td> <td valign="top" align="center"><span class="text1"> Name 11<br> Name 12<br> Name 13<br> Name 14<br> Name 15<br> </span></td> <td valign="top" align="center"><span class="text1"> Name 16<br> Name 17<br> Name 18<br> </span></td> </tr> </table> </div>[/code]rather than[code]<div align="center"> <table width="740" border="0" cellpadding="0" cellspacing="0" class='text1'> <tr> <td align='center'>Name 1</td> <td align='center'>Name 6</td> <td align='center'>Name 11</td> <td align='center'>Name 16</td> </tr> <tr> <td align='center'>Name 2</td> <td align='center'>Name 7</td> <td align='center'>Name 12</td> <td align='center'>Name 17</td> </tr> etc.... etc[/code] Link to comment https://forums.phpfreaks.com/topic/27799-for-statement-help/#findComment-127247 Share on other sites More sharing options...
speedy33417 Posted November 19, 2006 Author Share Posted November 19, 2006 I guess it wouldn't make a difference. I just happened to do it another way. Either case, something is off with the for statements... Link to comment https://forums.phpfreaks.com/topic/27799-for-statement-help/#findComment-127249 Share on other sites More sharing options...
Barand Posted November 19, 2006 Share Posted November 19, 2006 Are you sure you know what you want?In your original post you want 5 columns and in your example in reply #3 you show 4 columns ??? Link to comment https://forums.phpfreaks.com/topic/27799-for-statement-help/#findComment-127251 Share on other sites More sharing options...
speedy33417 Posted November 19, 2006 Author Share Posted November 19, 2006 My mistake. It should be five. However the value of $columns determines the number of columns. On this page it will be 5, but other pages could be different. I will use it as a template and adjust the number of columns accordingly.I was just confused why I'm not getting the result I was going for. Link to comment https://forums.phpfreaks.com/topic/27799-for-statement-help/#findComment-127255 Share on other sites More sharing options...
Barand Posted November 19, 2006 Share Posted November 19, 2006 try something like this. Put your names in $a instead of my 1..18.[code]<?php$a = range (1,18);$numcols = 5;$namesPerCell = ceil(count($a)/$numcols);$tmp = array_chunk($a, $namesPerCell);echo '<table border="1">';echo '<tr valign="top">';foreach ($tmp as $names) { echo '<td>'; foreach ($names as $n) { echo "$n<br/>"; } echo '</td>';}echo '</tr>';echo '</table>'; ?>[/code] Link to comment https://forums.phpfreaks.com/topic/27799-for-statement-help/#findComment-127260 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.