Jump to content

Results in a 3 column table


jodancer

Recommended Posts

Hi,

I'm quite new to this and I'm trying to get this to line up in a table with 3 columns (unlimited rows)

I have searched and tried but I'm not having any luck.

 

Can any one help?

Thank you

 

<?php 
echo '<div class="resultados_sub_cat">';

foreach($this->subcats as $key => $subcat) {

$subcat->link = JRoute::_('index.php?option=classcliff&view=list&catid='.$subcat->id."&Itemid=".$this->Itemid);
if ($key != 0)
	echo ' - ';
echo '<a href="'.$subcat->link.'">'.$subcat->name.'</a>';

} 
?>

Link to comment
https://forums.phpfreaks.com/topic/261028-results-in-a-3-column-table/
Share on other sites

untested, but should be close:

<?php 
echo '<div class="resultados_sub_cat">';
$colCount = 0;
echo "<table>";
foreach($this->subcats as $key => $subcat) {
if($colCount == 3){
          echo "</tr>";
          $colCount = 0
         }
        if($colCount == 0){echo "<tr><td>";}
        else {echo "<td>";}
$subcat->link = JRoute::_('index.php?option=classcliff&view=list&catid='.$subcat->id."&Itemid=".$this->Itemid);
if ($key != 0)
	echo ' - ';
echo '<a href="'.$subcat->link.'">'.$subcat->name.'</a></td>';
     $colCount = $colCount++;	
} 
echo "</table>";

reivewed, I missed a ;

<?php 
echo '<div class="resultados_sub_cat">';
$colCount = 0;
echo "<table>";
foreach($this->subcats as $key => $subcat) {
  if($colCount == 3){
    echo "</tr>";
    $colCount = 0;
  }
  if($colCount == 0){
    echo "<tr><td>";
  }
  else {
    echo "<td>";
  }
  $subcat->link = JRoute::_('index.php?option=classcliff&view=list&catid='.$subcat->id."&Itemid=".$this->Itemid);
  if ($key != 0){
    echo ' - ';
    echo '<a href="'.$subcat->link.'">'.$subcat->name.'</a></td>';
    $colCount = $colCount++;
  }
} 
echo "</table>";

<?php

$num_col = 3;
$col = 0;

echo '<div class="resultados_sub_cat">';
echo "<table>";

foreach($this->subcats as $key => $subcat) {
// start a new row
if($col == 0){
	echo "<tr>";
}

$subcat->link = JRoute::_('index.php?option=classcliff&view=list&catid='.$subcat->id."&Itemid=".$this->Itemid);
//if ($key != 0)
	//echo ' - ';
echo '<td><a href="'.$subcat->link.'">'.$subcat->name.'</a></td>';

$col++; // count the column you just output
if($col >= $num_col){
	echo "</tr>\n"; // end the row
	$col = 0; // reset counter
}
}
// finish any partial last row
if($col !== 0){
while($col < $num_col){
	echo "<td> </td>";
	$col++;
}
echo "</tr>";
}
echo "</table>";
?>

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.