title: Displaying Rows of HTML Elements in Vertical Rows
body: <p>I want to show a list of categories in my Virtuemart webshop vertically sorted the same way as shown in this demonstration:
  <a href="http://www.inkplant.com/code/mysql-vertical-sort.php" rel="nofollow">http://www.inkplant.com/code/mysql-vertical-sort.php</a></p>
  
  <p>So I borrowed the code:</p>
  
  <pre><code>    &lt;?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&lt;=$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 "&lt;table&gt;" ;
  for ($r=1;$r&lt;=$rows_per_col;$r++) {
      echo "&lt;tr&gt;" ;
      for ($c=1;$c&lt;=$cols;$c++) { echo "&lt;td&gt;".$values['col_'.$c][$r]."&lt;/td&gt;" ; }
      echo "&lt;/tr&gt;" ;
  }
  echo "&lt;/table&gt;" ;
  unset($values);
  ?&gt;
  </code></pre>
  
  <p>I then tried to modify it in my Virtuemart category template file with this result:</p>
  
  <pre><code>&lt;?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&lt;=$cols;$c++) { $values['col_'.$c] = array(); }
  $c = 1;
  $r = 1;
  foreach ( $this-&gt;category-&gt;children as $category ) {
  $catname = $category-&gt;category_name;
  $caturl = JRoute::_ ( 'index.php?option=com_virtuemart&amp;view=category&amp;virtuemart_category_id=' . $category-&gt;virtuemart_category_id );
  $values['col_'.$c][$r] = '&lt;div class="category floatleft'.$category_cellwidth.'"&gt;
  &lt;div class="spacer"&gt;&lt;h2&gt;
                              &lt;a href="'.$caturl.'" title="'.$catname.'"&gt;
                              '.$catname.'&lt;br /&gt;&lt;/a&gt;&lt;/h2&gt;
                                      &lt;/div&gt;&lt;/div&gt;';  
  
      if ($r == $rows_per_col) { $c++; $r = 1; } else { $r++; }
  }
  echo '&lt;div class="tablediv"&gt;' ;
  for ($r=1;$r&lt;=$rows_per_col;$r++) {
      echo '&lt;div class="row"&gt;' ;
      for ($c=1;$c&lt;=$cols;$c++) { echo $values['col_'.$c][$r]; }
      echo '&lt;/div&gt;' ;
  }
  echo '&lt;/div&gt;' ;
  unset($values);
  ?&gt;
  </code></pre>
  
  <p>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.</p>
  
  <p>If the number of categories equals a number that is dividable by 3 +1 then it shows in a weird way..</p>
  
  <p>Here is an example of how it shows when there are 9 categories:</p>
  
  <p>Cat1 | Cat4 | Cat7 <br>
  Cat2 | Cat5 | Cat8 <br>
  Cat3 | Cat6 | Cat9 <br>
  <br>
  Here is an example of how it shows when there are 8 categories:</p>
  
  <p>Cat1 | Cat4 | Cat7 <br>
  Cat2 | Cat5 | Cat8 <br>
  Cat3 | Cat6 | <br></p>
  
  <p><br>
  And here is an example of how it shows when there are 7 categories:<br><br>
  Cat1 | Cat4 | Cat7 <br>
  Cat2 | Cat5 | Cat3 <br>
  Cat6 | <br></p>
  
  <p>I really cannot figure this one out, so I hope someone can help me a little bit here..</p>