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?

[Webinar] Streamline your web hosting managementRegister Today

x
 
blueghoztConnect With a Mentor Commented:
@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
 
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Cornelia YoderArtistCommented:
Make the first 3 rows with the 3rd cell as colspan=2.
0
 
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
All Courses

From novice to tech pro — start learning today.