Page 1 of 1

for statement problem

Posted: Sun Nov 19, 2006 3:24 pm
by speedy33417
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: Select all

<?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>
$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?

Posted: Sun Nov 19, 2006 3:44 pm
by nickvd
There is only one column in your table... could that be why it's only displaying them in one column? ;)

edit... Ignore me, I'm blind....

Posted: Sun Nov 19, 2006 3:54 pm
by speedy33417
Well, there shouldn't be. There's two for statements at work the first ($j) should be adding more columns (td's) and the second is displaying the names and a linebreak.

This is the html code it should create:

Code: Select all

<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>

Posted: Sun Nov 19, 2006 4:09 pm
by volka
try

Code: Select all

<?php
// test data
$customernames = range(1,18);
$columns = 5;
$customernames = array_chunk(array_map('htmlentities', $customernames), $columns);
?>
<table>
	<tr>
<?php
foreach($customernames as $col) {
	echo '		<td>', join('<br />', $col), "</td>\n";
}
?>
	</tr>
</table>