将记录拆分为两列

Bha*_*mar 4 php mysql

我的数据库中有一个"学生"表,有大约5,000条记录.我想在两个 div中显示这些记录.如何在不执行两次查询的情况下执行此操作; 只使用一个查询?

显示示例http://www.freeimagehosting.net/uploads/f1c6bb41eb.gif

Anu*_*rag 8

只需使用CSS3.不确定支持的范围有多广,但可以省去很多麻烦,并且在进行更改时功能更强大.

使用column-count,并column-width控制每列的​​列数和宽度.这里有一些示例代码和一些非常令人印象深刻的结果.前缀-webkit和-moz现在直到它在所有浏览器中标准化.

.multi-column {
    /* Standard */
    column-count: 2;
    column-width: 150px;
    /* Webkit-based */
    -webkit-column-count: 2;
    -webkit-column-width: 150px;
    /* Gecko-based */
    -moz-column-count: 2;
    -moz-column-width: 150px;
}
Run Code Online (Sandbox Code Playgroud)

适用于此 <div>

<div class="multi-column">
    Ethelred of Wessex
    Louis XII of France
    George Frideric Handel
    George Washington
    Charles Deslandes
    Andrew Jackson
    Alfred Vail 
    William McKinley
    Woodrow Wilson
    Abdul-Aziz ibn Saud
    Fidel Castro
    Charles de Gaulle
    Leonardo da Vinci
</div>
Run Code Online (Sandbox Code Playgroud)

难道你不想看到这些辛苦工作后的样子吗?

替代文字http://i49.tinypic.com/e00m02.png


但如果有3列呢?没问题.

替代文字http://i46.tinypic.com/2gy3fo6.png


但是它无法处理你说的4列:

alt text http://i50.tinypic.com/2w3dez8.png


足够!我现在要停止添加这些

替代文字http://i50.tinypic.com/2mn3p5s.png


上帝请让它停止!

替代文字http://i49.tinypic.com/311pguq.png

  • 有时我觉得很幸运没有能够在IE中测试的奢侈:) (2认同)

nai*_*sts 7

只需找到"中间"的位置,然后输出div标签的结束标记和第二个div的开始标记:

<? 
$rowcount = mysql_num_rows($recordset);
echo "<div id='div1'>";
$i = 0;
while ($d = mysql_fetch_object($recordset)) {
  echo $d->somefield;
  $i++;

  if ($i == floor($rowcount / 2)) {
      //we have reached the mid-point, let's close the first DIV
      echo "</div><div id='div2'>";
  }
}
echo "</div>";
?>
Run Code Online (Sandbox Code Playgroud)