dongping4901 2010-03-02 15:49
浏览 28
已采纳

按字母排序的帮助

I'm having an issue here, I am trying to get all the titles from my database and list them in alphabetial order with the first letter indicating where we are at, so to be more clear:

A:
 Animal
 Alex
B:
 Boo
C:
 Crap

Actually this is what I use, and it works perfect:

<?php

$conn = mysql_connect('localhost','user','pw') or die(mysql_error());
$db = mysql_select_db('dbname') or die(mysql_error());

$sql = "select * from games order by title";
$result = mysql_query($sql, $conn) or die(mysql_error());

while ($list = mysql_fetch_array($result)) {

$letter = strtoupper(substr($list['title'],0,1));

if ($letter != $prev_row) {
echo "<br><b><u>$letter</u></b><br>";
} 
echo '<li><a href="/play/id/' . $list['id'] . '/">'.$list['title'].'</a></li>';

$prev_row = $letter;
} // end while
?>

But I would like it so, when it reaches the end of my div, let's say 400px height, it starts on a new column like:

A:                             C: 

Alien                         Crap

B:                             D:

Boo                           Dododododo

I am really clueless at the moment, so any help would be really aprreciated!

Thanks alot

  • 写回答

2条回答 默认 最新

  • dongren4147 2010-03-02 16:00
    关注

    As columns are not yet broadly supported in HTML you'll have to count the lines printed and multiply them by your defined line height.

    <?php
    
    $conn = mysql_connect('localhost','user','pw') or die(mysql_error());
    $db = mysql_select_db('dbname') or die(mysql_error());
    
    $sql = "select * from games order by title";
    $result = mysql_query($sql, $conn) or die(mysql_error());
    echo "<div style='float:left;height:400px;width:150px;'>";
    $current_height = 0;
    $line_height = '12px'; // replace by your value
    
    while ($list = mysql_fetch_array($result)) {
    
    $letter = strtoupper(substr($list['title'],0,1));
    
    if ( $current_height >= (400 - 2 * (int) $line_height) ) {
    echo "</div><div style='float:left;height:400px;width:150px;'>";
    $current_height = 0;
    }
    if ($letter != $prev_row) {
    echo "<br><b><u>$letter</u></b><br>";
    $current_height += (int) $line_height;
    } 
    echo '<li><a href="/play/id/' . $list['id'] . '/">'.$list['title'].'</a></li>';
    $current_height += (int) $line_height;
    
    $prev_row = $letter;
    } // end while
    echo "</div>";
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)