jodancer Posted April 16, 2012 Share Posted April 16, 2012 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 More sharing options...
Muddy_Funster Posted April 16, 2012 Share Posted April 16, 2012 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>"; Link to comment https://forums.phpfreaks.com/topic/261028-results-in-a-3-column-table/#findComment-1337770 Share on other sites More sharing options...
jodancer Posted April 16, 2012 Author Share Posted April 16, 2012 Thank you for the fast reply! It's giving and error and I can't find out why! I'll keep trying Link to comment https://forums.phpfreaks.com/topic/261028-results-in-a-3-column-table/#findComment-1337777 Share on other sites More sharing options...
Muddy_Funster Posted April 16, 2012 Share Posted April 16, 2012 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>"; Link to comment https://forums.phpfreaks.com/topic/261028-results-in-a-3-column-table/#findComment-1337780 Share on other sites More sharing options...
jodancer Posted April 16, 2012 Author Share Posted April 16, 2012 Thanks! No error now but I only have one column ? Link to comment https://forums.phpfreaks.com/topic/261028-results-in-a-3-column-table/#findComment-1337782 Share on other sites More sharing options...
Muddy_Funster Posted April 16, 2012 Share Posted April 16, 2012 try moving this: $colCount = $colCount++; down a line so it is after that } which is directly under it just now Link to comment https://forums.phpfreaks.com/topic/261028-results-in-a-3-column-table/#findComment-1337785 Share on other sites More sharing options...
jodancer Posted April 16, 2012 Author Share Posted April 16, 2012 No luck unfortunately Link to comment https://forums.phpfreaks.com/topic/261028-results-in-a-3-column-table/#findComment-1337789 Share on other sites More sharing options...
jodancer Posted April 17, 2012 Author Share Posted April 17, 2012 Many hours later and I still can't get it into 3 columns, just one long column :'( Can any of you experts help? Thanks Link to comment https://forums.phpfreaks.com/topic/261028-results-in-a-3-column-table/#findComment-1338074 Share on other sites More sharing options...
PFMaBiSmAd Posted April 17, 2012 Share Posted April 17, 2012 Posting your current code would help. Edit: BTW - you need logic after the end of your loop that completes any partial last row. Link to comment https://forums.phpfreaks.com/topic/261028-results-in-a-3-column-table/#findComment-1338077 Share on other sites More sharing options...
PFMaBiSmAd Posted April 17, 2012 Share Posted April 17, 2012 <?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>"; ?> Link to comment https://forums.phpfreaks.com/topic/261028-results-in-a-3-column-table/#findComment-1338081 Share on other sites More sharing options...
jodancer Posted April 17, 2012 Author Share Posted April 17, 2012 Thank you so much! Worked perfectly Have a great day Link to comment https://forums.phpfreaks.com/topic/261028-results-in-a-3-column-table/#findComment-1338091 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.