在垂直行中显示HTML元素的行

时间:2013-04-20 01:54:56

标签: php html sorting

我想在我的Virtuemart网上商店中显示一个类别列表,其垂直排序方式与本演示中显示的相同: http://www.inkplant.com/code/mysql-vertical-sort.php

所以我借了代码:

    <?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);
?>

然后我尝试使用此结果在我的Virtuemart类别模板文件中修改它:

<?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);
?>

如果类别的数量可以分为3或者可以分为3 -1,它实际上在类别视图中完美显示。这意味着如果页面上有3个,5个,6个,8个,9个,11个等等......类别,它会正确显示。

如果类别数量等于可以按3 + 1分类的数字,那么它会以奇怪的方式显示..

以下是有关9个类别的显示方式示例:

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 |

我真的无法想出这个,所以我希望有人可以在这里帮我一点..

1 个答案:

答案 0 :(得分:2)

试试这个,当然你可以改变颜色和行。

<?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>";
}
?>