How do i display row of 3 columns then rows of 4 columns ?

Hi,

the code i have attached shows a function i have written in PHP to display a table of 3 columns and then move on to another row of 3 columns after the first 3 coumns are displayed, it all works fine but my question is how do i alter the function so that after the first 3 columns are displayed it then displays rows of 4 columns from then on ?
function NewestFans()
{

$sql="SELECT * FROM fans ORDER BY joined DESC LIMIT 0, 10";
$result = mysql_query($sql);

	$directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);
	

echo '<table><tr>';
?> 
<td><img src="images/newUsers.png" alt="new users" width="116" height="116"></td>
<?
$rows = 0;
while ($fans_newest_row = mysql_fetch_assoc($result)) {
  echo '<td width="80" height="80" valign="top"><a href="fan.php?fid=' . print_r($fans_newest_row['username'], true) . '">'. print_r($fans_newest_row['username'], true) .'</a><br />';
  $uploadsDirectory = $directory_self . 'include/users/' . $fans_newest_row['username'] . '/' . $fans_newest_row['p_pic'];
	?>
	<a href="fan.php?fid=<? echo $fans_newest_row['username']; ?>"><img src="<? echo $uploadsDirectory; ?>" border ="0" width="80" height="80"></a>
	<?
  echo '</td>';
  ++$rows;
  if ($rows %3 == 0) {
   echo '</tr><tr>';  
  }
}
echo '</tr></table>';

}

Open in new window

zero525Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

theremonCommented:
Hi there

try the following code - it should help:
function NewestFans()
{

$sql="SELECT * FROM fans ORDER BY joined DESC LIMIT 0, 10";
$result = mysql_query($sql);

	$directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);
	

echo '<table><tr>';
?> 
<td><img src="images/newUsers.png" alt="new users" width="116" height="116"></td>
<?
$rows = 0;
$sep=3;
while ($fans_newest_row = mysql_fetch_assoc($result)) {
  echo '<td width="80" height="80" valign="top"><a href="fan.php?fid=' . print_r($fans_newest_row['username'], true) . '">'. print_r($fans_newest_row['username'], true) .'</a><br />';
  $uploadsDirectory = $directory_self . 'include/users/' . $fans_newest_row['username'] . '/' . $fans_newest_row['p_pic'];
	?>
	<a href="fan.php?fid=<? echo $fans_newest_row['username']; ?>"><img src="<? echo $uploadsDirectory; ?>" border ="0" width="80" height="80"></a>
	<?
  echo '</td>';
  ++$rows;
  if ($rows % $sep == 0) {
   echo '</tr><tr>';
  if ($rows==3) { $sep=4; }
  }
}
echo '</tr></table>';

}

Open in new window

0
zero525Author Commented:
Hi theremon,

the code you gave me displays 4 columns on every row including the first wich is only meant to be 3 columns.
0
Cornelia YoderArtistCommented:
Make the first 3 rows with the 3rd cell as colspan=2.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

zero525Author Commented:
Sorry my mistake theremon the code you gave me dosnt display the first row as 4 columns it displays as 3 columns but on the second row it displays 1 column and then creates the rest of the rows with 4 columns
0
blueghoztCommented:
try this code which should allow your image in the first cell of the first row and then 4 cols per row thereafter
function NewestFans()
{

$sql="SELECT * FROM fans ORDER BY joined DESC LIMIT 0, 10";
$result = mysql_query($sql);

	$directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);
	

echo '<table><tr>';
?> 
<td><img src="images/newUsers.png" alt="new users" width="116" height="116"></td>
<?
$cols = 0;
while ($fans_newest_row = mysql_fetch_assoc($result)) {
  echo '<td width="80" height="80" valign="top"><a href="fan.php?fid=' . print_r($fans_newest_row['username'], true) . '">'. print_r($fans_newest_row['username'], true) .'</a><br />';
  $uploadsDirectory = $directory_self . 'include/users/' . $fans_newest_row['username'] . '/' . $fans_newest_row['p_pic'];
	?>
	<a href="fan.php?fid=<? echo $fans_newest_row['username']; ?>"><img src="<? echo $uploadsDirectory; ?>" border ="0" width="80" height="80"></a>
	<?
  echo '</td>';
  $cols++;
  if ($cols %4 == 0 || $cols == 3) {
   echo '</tr><tr>';  
  }
}
echo '</tr></table>';

}

Open in new window

0
blueghoztCommented:
sorry just saw the problem with that, modulo 4 won't work if we return on the first 3 - so this will work:
function NewestFans()
{

$sql="SELECT * FROM fans ORDER BY joined DESC LIMIT 0, 10";
$result = mysql_query($sql);

	$directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);
	

echo '<table><tr>';
?> 
<td><img src="images/newUsers.png" alt="new users" width="116" height="116"></td>
<?
$cols = 0;
while ($fans_newest_row = mysql_fetch_assoc($result)) {
  echo '<td width="80" height="80" valign="top"><a href="fan.php?fid=' . print_r($fans_newest_row['username'], true) . '">'. print_r($fans_newest_row['username'], true) .'</a><br />';
  $uploadsDirectory = $directory_self . 'include/users/' . $fans_newest_row['username'] . '/' . $fans_newest_row['p_pic'];
	?>
	<a href="fan.php?fid=<? echo $fans_newest_row['username']; ?>"><img src="<? echo $uploadsDirectory; ?>" border ="0" width="80" height="80"></a>
	<?
  echo '</td>';
  if($cols !=3) $cols++; else $cols = 4;
  if ($cols %4 == 0) {
   echo '</tr><tr>';  
  }
}
echo '</tr></table>';

}

Open in new window

0
zero525Author Commented:
blueghozt that returns 4 columns for every row including the first one.
0
blueghoztCommented:
looking at your mark-up I can see that there is a hard coded table cell (column) inserted before the loop starts - do you want your first row to have this cell PLUS three more or this cell PLUS two more to make 3 in total? below code will return 3 cells for top row (which when added to the hard coded cell in your mark up will make 4)
<?
function NewestFans()
{

$sql="SELECT * FROM fans ORDER BY joined DESC LIMIT 0, 10";
$result = mysql_query($sql);

	$directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);
	

echo '<table><tr>';
?> 
<td><img src="images/newUsers.png" alt="new users" width="116" height="116"></td>
<?
$cols = 1;
while ($fans_newest_row = mysql_fetch_assoc($result)) {
  echo '<td width="80" height="80" valign="top"><a href="fan.php?fid=' . print_r($fans_newest_row['username'], true) . '">'. print_r($fans_newest_row['username'], true) .'</a><br />';
  $uploadsDirectory = $directory_self . 'include/users/' . $fans_newest_row['username'] . '/' . $fans_newest_row['p_pic'];
	?>
	<a href="fan.php?fid=<? echo $fans_newest_row['username']; ?>"><img src="<? echo $uploadsDirectory; ?>" border ="0" width="80" height="80"></a>
	<?
  echo '</td>';
  if ($cols %4 == 0) {
   echo '</tr><tr>';  
  }
}
echo '</tr></table>';

}

Open in new window

0
nasirbestCommented:
interesting confusion with both blueghozt and theremon.

I have a solution too
++$rows;
  if ($rows %3 == 0) {
   echo '</tr><tr>';  
  }

Replace above code with

  ++$rows;
  if ($rows == 3) {
     $rows = 4;              // simulate 4th column
     echo '<td>&nbsp;</td>'; // just to keep symmetry 
  }
  if ($rows % 4 == 0) {
   echo '</tr><tr>';  
  }

Open in new window

0
blueghoztCommented:
@nasirbest your inclusion of a 4th column is actually a 5th column - look at the mark up and you will notice a hard-coded <td></td> before the while loop begins - hence my question above about the inclusion of this cell. Also I think simply starting the $rows count on 1 instead of 0 solves the problem more neatly.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.