Jump to content

three column layout


scvinodkumar

Recommended Posts

I have to display 100 records, but needs to align in three column

 

Assuming you dont want a CSS style setup, but plain html, then it is relatively easy. Here is the crux of a bit of my code that displays a variable across (in order) on a 4 column format. Is it elegant? Heck no, but it works and works really well. I'd love to have others pick it apart as my 2 week knowledge of php & mySQL is growing, but only to solve problems I encounter like this lol. I have added a few comments to the code to explain it.

 

  echo '<div align="center"><table border="1">';	
  $tcount=1;
  while ($row2 = mysql_fetch_assoc($result2)) {
   if ($tcount==1) echo('<tr><td>');  // start of line
   else echo('<td>'); // if it's not 1 then it's the next 
   echo('<a href="index.php?pid=2&Shortname='.$row2["Shortname"].'">'.$row2["Shortname"].'</a></td>');  // DOnt be worried - just  the required bit of info on the screen
   $tcount++;
   if ($tcount==5) { //i.e. if tcount is 1 greater than the number of columns you want in the table, reset it and put the end of row
    $tcount=1;
    echo ('</tr>');
   } 
  }
//this bit fills in and blank table boxes with non blanking spaces at the bottom of the table just to make it nice & neat 

  if ($tcount>1) {
   while ($tcount<5) {
    echo('<td> </td>');
    $tcount++;
   }
  }
  echo ('</tr></table></div><br>'); // close the table

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/147224-three-column-layout/#findComment-773003
Share on other sites

this kinda depends how u want the data displayed

1-33 in col1

34-66 in col 2

66-99 in col 3

 

or in an across type table

1,4,7,10,..... in col1

2,5,8,11,.... in col2

3,6,9,12,.... in col3

 

 

in first style its all about the math.

$cols=3;
$max=100
for($i=0;$i<$max;$i++) $ar[$i]=$i;
echo "<TABLE>";
for($i=$ctr=0;$i<$max;$i++)
{
  if($ctr=0) echo "<TR>";
  echo "<TD>".$ar[$i]."</TD>";
  $ctr++;
  if($ctr>=$cols) {
    $ctr=0;
    echo "</TR>"
  }
}
while($ctr && $ctr++<$cols) echo "<TD>&nbsp</TD>";
if($ctr) echo "</TR>";
echo "</TABLE>";

 

 

The other way having the info on per row is same concept, but our counters change as well as the when the display code changes

[code]
$cols=3;
$max=100
for($i=0;$i<$max;$i++) $ar[$i]=$i;
echo "<TABLE>";
for($i=$ctr=0;$i<$max;$i++)
{
  if($ctr=0) echo "<TR><TD>";
  echo $ar[$i]."<BR>";
  $ctr++;
  if($ctr>=$percol) {
    $ctr=0;
    echo "</TD></TR>"
  }
}
if($ctr) echo "</TD></TR>";
echo "</TABLE>";

 

I think that shud do it, but this code is just from scratch, but ya shud get the idea from both examples :)

 

Link to comment
https://forums.phpfreaks.com/topic/147224-three-column-layout/#findComment-773013
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.