2

我有要从数据库中获取的值列表。列表可能很长,所以如果它有超过 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

4 回答 4

0

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

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";
}
于 2012-11-16T09:49:36.430 回答
0

使用css3 多列属性

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

<div class="namecount">
Long Text
</div>
于 2012-11-16T09:52:27.920 回答
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>";

       }
          }
       }

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

于 2012-11-16T10:10:33.023 回答
0

我发现 CSS3 多列在浏览器之间非常不一致,并且非常有问题(带有float:left,white-space:nowrap等的元素会导致它中断)。

所以我写了以下保留键=>值对的蛮力整理器。

// 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>';
于 2014-03-10T11:12:15.730 回答