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 ?
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>';
}
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.
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.
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>';
}
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>';
}
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>';
}
interesting confusion with both blueghozt and theremon.
I have a solution too
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> </td>'; // just to keep symmetry
}
if ($rows % 4 == 0) {
echo '</tr><tr>';
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
try the following code - it should help:
Open in new window