将列表分为三列

时间:2012-11-16 08:44:40

标签: php mysql list loops divide

我有要从数据库中获取的值列表。列表可能很长,所以我想分成两列,如果它有超过15项,如果它超过30则分为三列。如何在3列中分解它。例如..

  

01 | 12 | 23

     

02 | 13 | 24

     

03 | 14 | 25

     

04 | 15 | 26

     

05 | 16 | 27

     

06 | 17 | 28

     

07 | 18 | 29

     

08 | 19 | 30

     

09 | 20 | 31

     

10 | 21 |

     

11 | 22 |

现在我正在使用表格,并且在每个循环的开头都有巨大的if-nest

    $result = mysql_query("SELECT namecount FROM table WHERE name='$myvar'");

        if (!$result)
            echo 'MySQL Error: ' . mysql_error();
        else
        {
            while ($row = mysql_fetch_array($result))
            {
                $namecount= $row['namecount'];
                for($i=1;$i<=$namecount;$i++)
                {
                    if($namecount>15)
                    {
                        if($i==1)
                            echo "\n<table><tr><td width=\"200px\">";

                        if($namecount%2==0)
                        {
                            if($i==$namecount/2)
                                echo "</td>\n<td>"; 
                        }
                        else
                        {
                            if($i==($sailiot+3)/2)
                                echo "</td>\n<td>";
                        }
                    }

                //Print content here

                if($namecount>15)
                    {
                        if($namecount%2!=0 && $i==$namecount)
                            echo "<h3>&nbsp;</h3><p>&nbsp;<br>&nbsp;</p>"; //if last item and count is odd, print empty item

                        if($i==$namecount)
                            echo "\n</td></tr></table>\n";                  
                    }
                }
            }
        }

这个(有点)可以使用两列,但是有三列呢?

4 个答案:

答案 0 :(得分:0)

我会从内循环中提取<tr>, </tr>,这会节省额外的if。要拆分成多列,您可以计算列中的项目数,并引入第二个内部列变量来跟踪:

while ($row = mysql_fetch_array($result)) {
    $namecount = $row['namecount'];

    // calculate items per column
    $columns = 1;
    if ($namecount > 15)
        $columns = 2;

    if ($namecount > 30)
        $columns = 3;

    $items = $namecount / $columns;
    if ($namecount % $columns > 0)
        ++$items;

    echo "\n<table><tr><td width=\"200px\">";
    for ($i = 1, $j = 1; $i <= $namecount; $i++, $j++) {
        if ($j > $items) {
            echo "</td>\n<td>";
            $j = 1; // reset for new column
        }

        // Print content here
    }

    if ($namecount % 2 != 0) {
         // if count is odd, print empty item
        echo "<h3>&nbsp;</h3><p>&nbsp;<br>&nbsp;</p>";
    }

    echo "\n</td></tr></table>\n";
}

答案 1 :(得分:0)

使用css3 multi column property

<style>
.namecount {
  -moz-column-count:3; /* Firefox */
  -webkit-column-count:3; /* Safari and Chrome */
  column-count:3;
}
</style>

<div class="namecount">
Long Text
</div>

答案 2 :(得分:0)

你也许可以试试这个

  $result = mysql_query("SELECT namecount FROM table WHERE name='$myvar'");

    if (!$result)
        echo 'MySQL Error: ' . mysql_error();
    else
    {
        while ($row = mysql_fetch_array($result))
        {
         $namecount= $row['namecount'];

      for($i=1;$i<=$namecount;$i++)
            {
              if($namecount<=15){
                echo "<table><thead><tr><th>".$your_variable_data."</th></tr></thead>";}
   else if ($namecount >15 and $namecount <=30){
     echo "<thead><tr><th>".$your_variable_data."</th></tr></thead></table>";

       }
          }
       }

它没有经过测试,但是如果你想要更多只是添加其他if,它应该适用于3列。 这里是一个演示如何 http://jsfiddle.net/TCJjj/107/

答案 3 :(得分:0)

我发现CSS3多列在浏览器之间非常不一致,并且非常错误(带有float:leftwhite-space:nowrap的元素等导致它破坏)。

所以我编写了以下蛮力整理器,它保留了key =&gt;值对。

// vars

    $list = array('a','b','c','d','e','f','g','h','i','j','k','l','m',
                    'n','o','p','q','r','s','t','u','v','w','x','y','z');
    $columns = 3;

// sort it

    $count = count($list);
    $base = ceil($count/$columns);

    $keys = array_keys($list);
    for ($i=0;$i<$columns;$i++) {
        for ($j=0;$j<$base;$j++) {
            if (!empty($keys)) {
                $col[$i][] = array_shift( $keys );
            }
        }
    }

    $sorted = array();
    for ($j=0;$j<$base;$j++) {
        for ($i=0;$i<$columns;$i++) {
            if (isset($col[$i][$j])) {
                $sorted[$col[$i][$j]] = $list[$col[$i][$j]];
            }
        }
    }

// check output

    echo '<div style="float:left;margin-right:20px"><h3>ORIGINAL</h3>';
    foreach ($list as $k=>$v) echo $k .' = '. $v.'<br>';
    echo '</div>';

    echo '<div style="float:left"><h3>SORTED</h3>';
    foreach ($sorted as $k=>$v) echo $k .' = '. $v .'<br>';
    echo '</div>';
相关问题