在垂直行中显示HTML元素的行 [英] Displaying Rows of HTML Elements in Vertical Rows
问题描述
我想在我的Virtuemart网上商店中显示类别列表,并按照本演示中所示的相同方式进行垂直排序:
http://www.inkplant.com/code/mysql-vertical-sort.php
我借了下面的代码:
<?php
$ cols = 4; //列的数量,您可以将其设置为任何正整数
$ 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> ;
}
回显< / table> ;
unset($ values);
?>
然后我尝试在我的Virtuemart类别模板文件中修改此结果:
<?php
$ cols = 3; //列的数量,您可以将其设置为任何正整数
$ 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);
?>
如果类别的数量可以被3除或3除以3 -1。这意味着,如果页面上有3,5,6,8,9,11,12等类别,它就会正确显示。
如果类别的数量等于一个可以用3 + 1分割的数字,然后用奇怪的方式显示出来。
下面是一个当它有9个类别时如何显示的例子: p>
Cat1 | Cat4 | Cat7
Cat2 | Cat5 | Cat8
Cat3 | Cat6 | Cat9
下面是一个如何显示8种类别的例子:
Cat1 | Cat4 | Cat7
Cat2 | Cat5 | Cat8
Cat3 | Cat6 |
下面是一个如何显示7种类别的示例:
Cat1 | Cat4 | Cat7
Cat2 | Cat5 | Cat3
Cat6 |
我真的无法弄清楚这一点,所以我希望有人能帮助我一点。
解决方案 <?php // user210424
$ cols = 3;
$ rows = 3;
$ j = 0;
$ array = array(ONE,TWO,THREE,FOUR,FIVE,SIX,SEVEN,EIGHT,9);
$ b $ for($ i = 1; $ i <= $ cols; $ i ++){
echo< div class ='col'style ='float:left;'> ; ($ j; $ j< $ rows * $ i; $ j ++){
echo< div class ='row'>。$ array [$ j]。< / DIV>中;
}
echo< / div>;
}
?>
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..
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>";
}
?>
这篇关于在垂直行中显示HTML元素的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!