2011-07-22 75 views
-2

我有一個名單的列表,我想他們在這樣的2列。 如果第一列繼續在第二列,我想顯示第一個字母和這(繼續...)。列出2列

#        C (continued...) 
1        Congo 
2        Cook Islands 
3        Costa Rica 
5        Cote D'Ivoire 
6        Croatia 

A        D 
Afghanistan      Denmark 
Africa       Djibouti 
Albania       Dominica 
Algeria 
American Samoa 

B 
Bahamas, The 
Bahrain 
Bangladesh 
Barbados 
Bouvet Island 
Brazil 
Bulgaria 

C 
Cambodia 
Cameroon 
Canada 
Cape Verde 
Caribbean 
Chad 
Chile 
China 
Christmas Island 
Colombia 
Comoros 

這是代碼現在,但它不顯示Ç(續)第二列

$alphabet = null; 
while($row1 = mysql_fetch_array($locals)) { 
if($alphabet != substs($row1['RSTOWN'],0,1)) { 
echo strtoupper(substr($row1['RSTOWN'],0,1)); 
$alphabet = substr($row1['RSTOWN'],0,1); 
} 
echo '<li class="forward"> 
<a href="townpubs.php?RSTOWN='.$row1['RSTOWN'].'" rel="external">' 
.$row1['RSTOWN']. 
'<small class="listcounter">'.$row1['PubCount'].'</small> 
</a> 
</li>'; 
+1

爲什麼有捷克缺少什麼? :-)好吧,嚴重的是,我懷疑這是一個問題......如果你是程序員,爲什麼你只是不做你想要的程序?問題在哪裏? – TMS

回答

0

選擇的名字,說名= N的數量,計算halfn = N/2

存儲在陣列中,如果已存儲名字與索引halfn + 1,

檢查,如果以前的項具有相同的字母,並且如果如此存儲「繼續......」在列表中。

將列表從1打印到truncate(列表+ 1中的entrys數量)/ 2以計入奇數個條目。

每行打印[i]和[I + halfn]

0

,我不會讓你自己的代碼的例子。但我會解釋這些方法。你只需要實現你的SQL到他們。

活生生的例子:http://kopli.pri.ee/stackoverflow/6794308.php

代碼的完整版:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html> 
<head> 
    <title>List in 2 columns - Kalle H. Väravas</title> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
    <style> 
     html, body {margin: 0px; padding: 0px;} 
     .container {width: 200px; float: left;} 
    </style> 
</head> 
<body> 
<?php 

$countries = array(
    'Afghanistan', 
    'Africa', 
    'Albania', 
    'Algeria', 
    'American Samoa', 
    'Bahamas, The', 
    'Bahrain', 
    'Bangladesh', 
    'Barbados', 
    'Bouvet Island', 
    'Brazil', 
    'Bulgaria', 
    'Cambodia', 
    'Cameroon', 
    'Canada', 
    'Cape Verde', 
    'Caribbean', 
    'Chad', 
    'Chile', 
    'China', 
    'Christmas Island', 
    'Colombia', 
    'Comoros', 
    'Congo', 
    'Cook Islands', 
    'Costa Rica', 
    'Cote D\'Ivoire', 
    'Croatia', 
    'Denmark', 
    'Djibouti', 
    'Dominica' 
); 

$countries_count = count($countries); 
$breaking_point = round($countries_count/2); 
$foreach_counter = 0; 
$current_letter = ''; 

echo '<div class="container">'; 
foreach ($countries as $country) { 
    $foreach_counter++; 
    if ($country[0] != $current_letter) { 
     echo '<b>' . ($current_letter = $country[0]) . '</b><br />'; 
    } 
    echo $country . '<br />'; 
    if ($foreach_counter == $breaking_point) { 
     echo '</div><div class="container">'; 
    } 
} 
echo '</div>'; 

?> 
</body> 
</html>