for statement problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
speedy33417
Forum Contributor
Posts: 128
Joined: Sun Jul 23, 2006 1:14 pm

for statement problem

Post 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?
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post 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....
User avatar
speedy33417
Forum Contributor
Posts: 128
Joined: Sun Jul 23, 2006 1:14 pm

Post 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>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

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