Reputation: 499
I want to show a list of categories in my Virtuemart webshop vertically sorted the same way as shown in this demonstration: http://www.inkplant.com/code/mysql-vertical-sort.php
So I borrowed the code:
<?php
$cols = 4; //number of columns, you can set this to any positive integer
$values = array();
$result = mysql_query("SELECT * FROM states ORDER BY name");
$numrows = mysql_num_rows($result);
$rows_per_col = ceil($numrows / $cols);
for ($c=1;$c<=$cols;$c++) { $values['col_'.$c] = array(); }
$c = 1;
$r = 1;
while ($row = mysql_fetch_assoc($result)) {
$values['col_'.$c][$r] = stripslashes($row['name']);
if ($r == $rows_per_col) { $c++; $r = 1; } else { $r++; }
}
echo "<table>" ;
for ($r=1;$r<=$rows_per_col;$r++) {
echo "<tr>" ;
for ($c=1;$c<=$cols;$c++) { echo "<td>".$values['col_'.$c][$r]."</td>" ; }
echo "</tr>" ;
}
echo "</table>" ;
unset($values);
?>
I then tried to modify it in my Virtuemart category template file with this result:
<?php
$cols = 3; //number of columns, you can set this to any positive integer
$values = array();
$numrows = $precounterdigit;
$rows_per_col = ceil($numrows / $cols);
for ($c=1;$c<=$cols;$c++) { $values['col_'.$c] = array(); }
$c = 1;
$r = 1;
foreach ( $this->category->children as $category ) {
$catname = $category->category_name;
$caturl = JRoute::_ ( 'index.php?option=com_virtuemart&view=category&virtuemart_category_id=' . $category->virtuemart_category_id );
$values['col_'.$c][$r] = '<div class="category floatleft'.$category_cellwidth.'">
<div class="spacer"><h2>
<a href="'.$caturl.'" title="'.$catname.'">
'.$catname.'<br /></a></h2>
</div></div>';
if ($r == $rows_per_col) { $c++; $r = 1; } else { $r++; }
}
echo '<div class="tablediv">' ;
for ($r=1;$r<=$rows_per_col;$r++) {
echo '<div class="row">' ;
for ($c=1;$c<=$cols;$c++) { echo $values['col_'.$c][$r]; }
echo '</div>' ;
}
echo '</div>' ;
unset($values);
?>
It actually shows perfectly in the category view if the number of categories are dividable by 3 or dividable by 3 -1. Meaning that it shows correctly if there are 3, 5, 6, 8, 9, 11, 12 etc... categories on the page.
If the number of categories equals a number that is dividable by 3 +1 then it shows in a weird way..
Here is an example of how it shows when there are 9 categories:
Cat1 | Cat4 | Cat7
Cat2 | Cat5 | Cat8
Cat3 | Cat6 | Cat9
Here is an example of how it shows when there are 8 categories:
Cat1 | Cat4 | Cat7
Cat2 | Cat5 | Cat8
Cat3 | Cat6 |
And here is an example of how it shows when there are 7 categories:
Cat1 | Cat4 | Cat7
Cat2 | Cat5 | Cat3
Cat6 |
I really cannot figure this one out, so I hope someone can help me a little bit here..
Upvotes: 0
Views: 1971
Reputation: 34
Just try this, Sure you can change cols and rows.
<?php //user210424
$cols = 3;
$rows = 3;
$j = 0;
$array = array("ONE","TWO","THREE","FOUR","FIVE","SIX","SEVEN","EIGHT","NINE");
for($i=1; $i<=$cols; $i++) {
echo "<div class='col' style='float:left;'>";
for($j; $j<$rows*$i; $j++) {
echo "<div class='row'>".$array[$j]."</div>";
}
echo "</div>";
}
?>
Upvotes: 2