Link to home
Start Free TrialLog in
Avatar of zero525
zero525

asked on

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

Avatar of theremon
theremon
Flag of Greece image

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

Avatar of zero525
zero525

ASKER

Hi theremon,

the code you gave me displays 4 columns on every row including the first wich is only meant to be 3 columns.
Make the first 3 rows with the 3rd cell as colspan=2.
Avatar of zero525

ASKER

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

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

Avatar of zero525

ASKER

blueghozt that returns 4 columns for every row including the first one.
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

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

ASKER CERTIFIED SOLUTION
Avatar of blueghozt
blueghozt
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial